From e8fab6781854bc8fef9907dbc0ab48c85e58f9a6 Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Wed, 14 Oct 2026 18:04:14 +0300 Subject: [PATCH] fix(new): treat redirected stdin as non-interactive isInteractive() checked os.ModeCharDevice, which is also set for /dev/null and NUL, so 'togo new --db postgres --- cmd/interactive_test.go | 53 +++++++++++++++++++++++++++++++++++++++++ cmd/new.go | 8 ++++--- 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 cmd/interactive_test.go diff --git a/cmd/interactive_test.go b/cmd/interactive_test.go new file mode 100644 index 0000000..59007d3 --- /dev/null +++ b/cmd/interactive_test.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "os" + "path/filepath" + "testing" +) + +// withStdin swaps os.Stdin for f during the test. +func withStdin(t *testing.T, f *os.File) { + t.Helper() + old := os.Stdin + os.Stdin = f + t.Cleanup(func() { os.Stdin = old }) +} + +// A redirected stdin (/dev/null, NUL, a file or a pipe) is not a terminal, so +// `togo new` must not prompt or start docker on its own (#4). +func TestIsInteractive_RedirectedStdin(t *testing.T) { + devnull, err := os.Open(os.DevNull) + if err != nil { + t.Fatal(err) + } + defer devnull.Close() + + regular := filepath.Join(t.TempDir(), "in.txt") + if err := os.WriteFile(regular, []byte("y\n"), 0o600); err != nil { + t.Fatal(err) + } + file, err := os.Open(regular) + if err != nil { + t.Fatal(err) + } + defer file.Close() + + pr, pw, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + defer pr.Close() + defer pw.Close() + + for name, f := range map[string]*os.File{ + os.DevNull: devnull, + "regular file": file, + "pipe": pr, + } { + withStdin(t, f) + if isInteractive() { + t.Errorf("isInteractive() = true for stdin %s; want false", name) + } + } +} diff --git a/cmd/new.go b/cmd/new.go index 1ee4024..3163ea5 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/spf13/cobra" + "golang.org/x/term" "github.com/togo-framework/cli/internal/config" "github.com/togo-framework/cli/internal/scaffold" @@ -346,8 +347,9 @@ func dirNotEmpty(path string) bool { return err == nil && len(entries) > 0 } -// isInteractive reports whether stdin is a terminal (so prompting won't block CI). +// isInteractive reports whether stdin is a real terminal (so prompting won't +// block CI). /dev/null and NUL are character devices but not terminals, so this +// uses term.IsTerminal, like internal/ui. func isInteractive() bool { - fi, err := os.Stdin.Stat() - return err == nil && (fi.Mode()&os.ModeCharDevice) != 0 + return term.IsTerminal(int(os.Stdin.Fd())) }