Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions cmd/interactive_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
8 changes: 5 additions & 3 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
}
Loading