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
7 changes: 3 additions & 4 deletions internal/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())) }
Expand Down
15 changes: 15 additions & 0 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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() })
}
13 changes: 3 additions & 10 deletions internal/cmd/entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"os"
"testing"
)

Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 1 addition & 8 deletions internal/cmd/reports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"os"
"testing"
)

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
13 changes: 6 additions & 7 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions internal/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
Loading