From 0cd55875abfe20a4440380c5d1fd895116670619 Mon Sep 17 00:00:00 2001 From: Unay Santisteban Date: Mon, 30 Nov 2026 13:15:51 +1000 Subject: [PATCH] fix: #38 ask the os whether a file is a terminal, so /dev/null is not one --- internal/cmd/auth.go | 7 +++---- internal/cmd/cmd_test.go | 15 +++++++++++++++ internal/cmd/entries_test.go | 13 +++---------- internal/cmd/reports_test.go | 9 +-------- internal/cmd/ui.go | 4 ++-- internal/output/output.go | 13 ++++++------- internal/output/output_test.go | 23 ++++++++++++++++------- 7 files changed, 46 insertions(+), 38 deletions(-) diff --git a/internal/cmd/auth.go b/internal/cmd/auth.go index 5716e55..2d26912 100644 --- a/internal/cmd/auth.go +++ b/internal/cmd/auth.go @@ -14,10 +14,9 @@ import ( "golang.org/x/term" ) -// stdinIsTerminal asks the OS whether stdin is a TTY. output.IsTerminal is not -// enough here: /dev/null is a character device too, and has no prompt to type -// into. Tests swap both out, since CI has no terminal. -var stdinIsTerminal = func() bool { return term.IsTerminal(int(os.Stdin.Fd())) } +// stdinIsTerminal asks the OS whether stdin is a TTY: /dev/null is not, and +// has no prompt to type into. Tests swap it, since CI has no terminal. +var stdinIsTerminal = func() bool { return output.IsTerminal(os.Stdin) } // readPassword reads a line from the terminal without echoing it. var readPassword = func() ([]byte, error) { return term.ReadPassword(int(os.Stdin.Fd())) } diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go index 7936428..8e5a8c7 100644 --- a/internal/cmd/cmd_test.go +++ b/internal/cmd/cmd_test.go @@ -14,6 +14,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/othercodes/tmetric-cli/internal/app" "github.com/othercodes/tmetric-cli/internal/auth" + "github.com/othercodes/tmetric-cli/internal/output" "github.com/othercodes/tmetric-cli/internal/tmetric" "github.com/zalando/go-keyring" ) @@ -402,3 +403,17 @@ func TestUI(t *testing.T) { t.Setenv(tokenEnv, "") expect(t, []string{"ui"}, 1, nil, []string{"not signed in"}) } + +// stdoutOnTerminal points stdout at /dev/null and makes output.IsTerminal take +// it for a terminal, since CI has no real one. +func stdoutOnTerminal(t *testing.T) { + t.Helper() + tty, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) + if err != nil { + t.Fatal(err) + } + oldStdout, oldCheck := os.Stdout, output.IsTerminal + os.Stdout = tty + output.IsTerminal = func(f *os.File) bool { return f == tty } + t.Cleanup(func() { os.Stdout, output.IsTerminal = oldStdout, oldCheck; tty.Close() }) +} diff --git a/internal/cmd/entries_test.go b/internal/cmd/entries_test.go index d6c1f7c..b368b72 100644 --- a/internal/cmd/entries_test.go +++ b/internal/cmd/entries_test.go @@ -2,7 +2,6 @@ package cmd import ( "context" - "os" "testing" ) @@ -41,15 +40,9 @@ func TestReportWithoutEntries(t *testing.T) { func TestReportOnTerminal(t *testing.T) { timerAPI(t, map[string]string{entriesRoute: twoEntries}) - // /dev/null is a character device, which is what the table looks for; what - // it prints is checked in the output package, this only takes the branch. - tty, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) - if err != nil { - t.Fatal(err) - } - old := os.Stdout - os.Stdout = tty - defer func() { os.Stdout = old; tty.Close() }() + // What the table prints is checked in the output package; this only takes + // the branch. + stdoutOnTerminal(t) if code := run(context.Background(), []string{"today"}); code != 0 { t.Errorf("exit code = %d", code) } diff --git a/internal/cmd/reports_test.go b/internal/cmd/reports_test.go index 89c77ea..0cbe704 100644 --- a/internal/cmd/reports_test.go +++ b/internal/cmd/reports_test.go @@ -2,7 +2,6 @@ package cmd import ( "context" - "os" "testing" ) @@ -40,13 +39,7 @@ func TestReportsProjectsEmpty(t *testing.T) { func TestReportsProjectsOnTerminal(t *testing.T) { timerAPI(t, map[string]string{reportRoute: reportBody}) - tty, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) - if err != nil { - t.Fatal(err) - } - old := os.Stdout - os.Stdout = tty - defer func() { os.Stdout = old; tty.Close() }() + stdoutOnTerminal(t) if code := run(context.Background(), []string{"reports", "projects"}); code != 0 { t.Errorf("exit code = %d", code) } diff --git a/internal/cmd/ui.go b/internal/cmd/ui.go index 7dc7bfc..f3521ea 100644 --- a/internal/cmd/ui.go +++ b/internal/cmd/ui.go @@ -3,15 +3,15 @@ package cmd import ( "os" + "github.com/othercodes/tmetric-cli/internal/output" "github.com/othercodes/tmetric-cli/internal/tui" "github.com/spf13/cobra" - "golang.org/x/term" ) // uiOnTerminal asks the OS whether both ends are TTYs, and runTUI draws the // UI; tests swap both out, since CI has no terminal. var ( - uiOnTerminal = func() bool { return stdinIsTerminal() && term.IsTerminal(int(os.Stdout.Fd())) } + uiOnTerminal = func() bool { return stdinIsTerminal() && output.IsTerminal(os.Stdout) } runTUI = tui.Run ) diff --git a/internal/output/output.go b/internal/output/output.go index 4feb60e..6557362 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -13,15 +13,14 @@ import ( "strings" "text/tabwriter" "time" + + "golang.org/x/term" ) -// IsTerminal reports whether f is attached to a terminal. -func IsTerminal(f *os.File) bool { - info, err := f.Stat() - if err != nil { - return false - } - return info.Mode()&os.ModeCharDevice != 0 +// IsTerminal asks the OS whether f is attached to a terminal; a character +// device such as /dev/null is not one. Tests swap it, since CI has no terminal. +var IsTerminal = func(f *os.File) bool { + return term.IsTerminal(int(f.Fd())) } // ColorEnabled reports whether ANSI colors should be used on f, honoring diff --git a/internal/output/output_test.go b/internal/output/output_test.go index 5545029..a500302 100644 --- a/internal/output/output_test.go +++ b/internal/output/output_test.go @@ -14,8 +14,6 @@ type failingWriter struct{} func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("disk full") } -// devNull is a character device, which is what a terminal looks like to -// IsTerminal, without needing a real terminal in CI. func devNull(t *testing.T) *os.File { t.Helper() f, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) @@ -26,6 +24,17 @@ func devNull(t *testing.T) *os.File { return f } +// terminal is a file that IsTerminal takes for a terminal, since CI has no +// real one. +func terminal(t *testing.T) *os.File { + t.Helper() + f := devNull(t) + real := IsTerminal + IsTerminal = func(file *os.File) bool { return file == f } + t.Cleanup(func() { IsTerminal = real }) + return f +} + func pipe(t *testing.T) (*os.File, *os.File) { t.Helper() r, w, err := os.Pipe() @@ -37,8 +46,8 @@ func pipe(t *testing.T) (*os.File, *os.File) { } func TestIsTerminal(t *testing.T) { - if !IsTerminal(devNull(t)) { - t.Error("a character device should count as a terminal") + if IsTerminal(devNull(t)) { + t.Error("/dev/null is a character device, not a terminal") } _, w := pipe(t) if IsTerminal(w) { @@ -52,7 +61,7 @@ func TestIsTerminal(t *testing.T) { } func TestColorEnabled(t *testing.T) { - tty := devNull(t) + tty := terminal(t) t.Setenv("NO_COLOR", "") t.Setenv("TERM", "xterm") if !ColorEnabled(tty) { @@ -125,7 +134,7 @@ func TestTableOnTerminalWithoutRows(t *testing.T) { } func TestTableDetectsTerminal(t *testing.T) { - if err := Table(devNull(t), []string{"ID"}, [][]string{{"1"}}); err != nil { + if err := Table(terminal(t), []string{"ID"}, [][]string{{"1"}}); err != nil { t.Fatal(err) } } @@ -165,7 +174,7 @@ func TestKeyValues(t *testing.T) { if got := buf.String(); got != want { t.Errorf("got %q, want %q", got, want) } - if err := KeyValues(devNull(t), [][2]string{{"a", "b"}}); err != nil { + if err := KeyValues(terminal(t), [][2]string{{"a", "b"}}); err != nil { t.Fatal(err) } }