Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func init() {
// maybeDefaultToSpeak injects the "speak" subcommand when the user calls `sag` like macOS `say`.
func maybeDefaultToSpeak() {
if len(os.Args) <= 1 {
// Still default to speak if stdin has piped data
if !isStdinTTY() {
os.Args = append(os.Args, "speak")
}
return
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,33 @@ func TestExecuteHelp(t *testing.T) {
t.Fatalf("unreachable")
}
}

func TestMaybeDefaultToSpeak_PipedStdin(t *testing.T) {
defer keepArgs(t)()

// Replace stdin with a pipe (non-TTY)
origStdin := os.Stdin
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
os.Stdin = r
defer func() {
os.Stdin = origStdin
r.Close()
w.Close()
}()

os.Args = []string{"sag"}
maybeDefaultToSpeak()

want := []string{"sag", "speak"}
if len(os.Args) != len(want) {
t.Fatalf("expected %v, got %v", want, os.Args)
}
for i := range want {
if os.Args[i] != want[i] {
t.Fatalf("args mismatch at %d: got %q want %q", i, os.Args[i], want[i])
}
}
}