Files
yetone 9298d2a7c3 cli: prompts read the stdin Windows' console init reopens, not the one package init saw
tom047784 (Discord, Windows desktop build): `magpie plugin login` in a
terminal failed with "read /dev/stdin: The handle is invalid". The
desktop build is a GUI-subsystem binary; console_windows.go's init
attaches the parent console and reopens CONIN$/CONOUT$ into os.Stdin,
os.Stdout and os.Stderr. But `var stdin = bufio.NewReader(os.Stdin)` in
backup_cmd.go is a package variable, set before any init runs, so it
kept the invalid handle.

That one reader serves every CLI prompt, so all of them broke the same
way: plugin login (askPrompt, askNumber, the code and pasted callback
lines), backup/WebDAV passphrases and passwords piped in (secret), the
library and quota confirmations. stdin is now a func that makes the
reader over os.Stdin as it is when read, and remakes it if os.Stdin
changed; one reader still serves a run, so a second piped secret is
still the second line.

Sibling of the same class: lipgloss's default renderer (termenv's
default output) is made over os.Stdout at its package init, before the
reattach, so the desktop build's CLI printed every style without colour.
The init now points that renderer at CONOUT$ in place; the styles made
at package init hold it. A grep for package-level os.Stdin/os.Stdout/
os.Stderr found no other one in the repo (writers like fmt.Println and
json.NewEncoder(os.Stdout) inside functions resolve at use time).

webdav_cli_test's piped() swapped both os.Stdin and the reader, which is
why no test caught this; it now swaps os.Stdin only.

Run (at this tree, worktree off origin/main 4e3078ee4):
- go test -count=1 -timeout 45m ./internal/cli/: ok (52s)
- go test -race -count=3 TestPromptsReadStdinSetAfterInit|TestAskPrompt|TestWebdav: ok
- gofmt -l internal/cli: empty; go vet ./internal/cli, GOOS=linux go vet
  -tags nogui ./internal/cli, GOOS=windows go vet ./internal/cli: ok
Broken by hand: TestPromptsReadStdinSetAfterInit on the old backup_cmd,
plugins_cli, library_cli and quota_cli (new test file only) is red:
"askPrompt = "", EOF; want blue from the stdin set after init".
Not tried on Windows: the Windows box (10.0.0.132) timed out on ssh, so
the real desktop build's `magpie plugin login` and the lipgloss colour
change were not run there. Not run: go test ./... (only internal/cli
changed).
2026-10-26 22:22:27 +08:00

56 lines
1.7 KiB
Go

package cli
import (
"os"
"testing"
"github.com/yetone/magpie/internal/plugin"
)
// swapStdin sets os.Stdin to a pipe holding text, after package init, and
// touches nothing else: it is what the Windows desktop build's init does
// when it reopens the terminal's CONIN$ (console_windows.go).
func swapStdin(t *testing.T, text string) {
t.Helper()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
w.WriteString(text)
w.Close()
old := os.Stdin
os.Stdin = r
t.Cleanup(func() {
os.Stdin = old
r.Close()
})
}
// TestPromptsReadStdinSetAfterInit: every CLI prompt reads os.Stdin as it
// is when asked, not as it was when the package's variables were set. On
// Windows a reader made at package init held the GUI binary's invalid
// handle, and `magpie plugin login` failed with "read /dev/stdin: The
// handle is invalid" (tom047784).
func TestPromptsReadStdinSetAfterInit(t *testing.T) {
// plugin login's question, as a line
swapStdin(t, "blue\n")
if v, err := askPrompt(&plugin.Prompt{Type: "text", Key: "team", Message: "Your team"}); err != nil || v != "blue" {
t.Fatalf("askPrompt = %q, %v; want blue from the stdin set after init", v, err)
}
// plugin login's choice of method or option
swapStdin(t, "2\n")
if i, err := askNumber(3); err != nil || i != 1 {
t.Fatalf("askNumber = %d, %v; want 1 from the stdin set after init", i, err)
}
// backup's and WebDAV's passphrase and password, piped: a line each,
// both from the one reader
swapStdin(t, "first\nsecond\n")
for _, want := range []string{"first", "second"} {
if v, err := secret("passphrase", "Passphrase: ", false); err != nil || v != want {
t.Fatalf("secret = %q, %v; want %s from the stdin set after init", v, err, want)
}
}
}