diff --git a/cmd/calendar_test.go b/cmd/calendar_test.go index a9319988..9d258da8 100644 --- a/cmd/calendar_test.go +++ b/cmd/calendar_test.go @@ -2163,7 +2163,7 @@ func TestCalendarCreate_AddsSelfByDefault(t *testing.T) { _ = cmd.Flags().Set("attendees", "someone-else@example.com") oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w if err := cmd.RunE(cmd, []string{}); err != nil { os.Stdout = oldStdout @@ -2249,7 +2249,7 @@ func TestCalendarCreate_PrimaryLookupFailureDefaultPath(t *testing.T) { // Note: --add-self left at default true and NOT explicitly set. oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w if err := cmd.RunE(cmd, []string{}); err != nil { os.Stdout = oldStdout @@ -2316,7 +2316,7 @@ func TestCalendarCreate_RespectsAddSelfFalse(t *testing.T) { _ = cmd.Flags().Set("add-self", "false") oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w if err := cmd.RunE(cmd, []string{}); err != nil { os.Stdout = oldStdout diff --git a/cmd/chat_test.go b/cmd/chat_test.go index 9f73bf9e..69a9d774 100644 --- a/cmd/chat_test.go +++ b/cmd/chat_test.go @@ -3344,7 +3344,7 @@ func TestChatFindGroup_CommandE2E(t *testing.T) { // Capture stdout oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w err := cmd.RunE(cmd, []string{}) @@ -3408,7 +3408,7 @@ func TestChatFindGroup_ErrorOnEmptyMembers(t *testing.T) { // Capture stderr (PrintError writes error JSON to stderr per #190) oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w cmd.RunE(cmd, []string{}) @@ -3558,7 +3558,7 @@ func TestChatFindSpace_MatchesByDisplayName(t *testing.T) { cmd.Flags().Set("name", "sales") oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w cmd.RunE(cmd, []string{}) w.Close() @@ -3597,7 +3597,7 @@ func TestChatFindSpace_TypeFilter(t *testing.T) { cmd.Flags().Set("type", "SPACE") oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w cmd.RunE(cmd, []string{}) w.Close() @@ -3626,7 +3626,7 @@ func TestChatFindSpace_InvalidType(t *testing.T) { cmd.Flags().Set("type", "BOGUS") oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w cmd.RunE(cmd, []string{}) w.Close() @@ -3695,7 +3695,7 @@ func TestChatFindSpace_RefreshTypeScoped(t *testing.T) { _ = cmd.Flags().Set("refresh", "true") oldStdout := os.Stdout - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stdout = w if err := cmd.RunE(cmd, []string{}); err != nil { os.Stdout = oldStdout @@ -3764,7 +3764,7 @@ func TestChatFindSpace_NoCache(t *testing.T) { cmd.Flags().Set("name", "sales") oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w cmd.RunE(cmd, []string{}) w.Close() @@ -3791,7 +3791,7 @@ func TestChatFindGroup_NoCache(t *testing.T) { cmd.Flags().Set("members", "alice@example.com") oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w cmd.RunE(cmd, []string{}) @@ -3981,9 +3981,9 @@ func TestChatRecent_CommandPathFiltersSortsAndCaps(t *testing.T) { }() originalStdout := os.Stdout - reader, writer, err := os.Pipe() + reader, writer, err := captureFile(t) if err != nil { - t.Fatalf("failed to create stdout pipe: %v", err) + t.Fatalf("failed to create stdout capture file: %v", err) } os.Stdout = writer defer func() { diff --git a/cmd/hooks_test.go b/cmd/hooks_test.go index 9ec7801a..b6dd7bee 100644 --- a/cmd/hooks_test.go +++ b/cmd/hooks_test.go @@ -251,7 +251,7 @@ func captureHookCommandOutput(t *testing.T, outputFormat string, quietValue bool t.Fatal("root --format flag is not registered") } oldFormat, oldQuiet, oldStdout := formatFlag.Value.String(), quiet, os.Stdout - reader, writer, err := os.Pipe() + reader, writer, err := captureFile(t) if err != nil { t.Fatal(err) } diff --git a/cmd/raw_runners_test.go b/cmd/raw_runners_test.go index d8635a24..94dbe498 100644 --- a/cmd/raw_runners_test.go +++ b/cmd/raw_runners_test.go @@ -14,6 +14,7 @@ import ( "net/http/httptest" "net/url" "os" + "path/filepath" "strings" "sync" "testing" @@ -48,6 +49,7 @@ func captureStdout(t *testing.T, fn func() error) (string, error) { runErr := fn() _ = w.Close() wg.Wait() + _ = r.Close() os.Stdout = orig return buf.String(), runErr } @@ -517,6 +519,7 @@ func captureStderrStr(t *testing.T, fn func()) string { fn() _ = w.Close() wg.Wait() + _ = r.Close() os.Stderr = orig return buf.String() } @@ -574,3 +577,34 @@ func TestChatListRaw_ClampsPageSizeToMax(t *testing.T) { t.Errorf("expected verbatim nextPageToken positioned after item 5, got %q", got.NextPageToken) } } + +// captureFile returns a file to write captured output to and a second +// handle to read it back from, as a drop-in for os.Pipe() in tests that +// run a command first and read its output afterwards. +// +// A pipe cannot be used that way: nothing reads while the command writes, +// so the write blocks for good once the output exceeds the pipe's buffer, +// and that buffer is not a fixed size (macOS shrinks it to 512 bytes when +// a process holds many pipes). A file has no such limit. Both handles are +// closed when the test ends. The error is always nil. +func captureFile(t *testing.T) (r, w *os.File, err error) { + t.Helper() + // Setup failures end the test here, so a caller that ignores the + // returned error (kept only to match os.Pipe's signature) can never + // go on to install a nil file as os.Stdout or os.Stderr. + path := filepath.Join(t.TempDir(), "captured-output") + w, err = os.Create(path) + if err != nil { + t.Fatalf("failed to create capture file: %v", err) + } + r, err = os.Open(path) + if err != nil { + _ = w.Close() + t.Fatalf("failed to open capture file: %v", err) + } + t.Cleanup(func() { + _ = r.Close() + _ = w.Close() + }) + return r, w, nil +} diff --git a/cmd/slides_test.go b/cmd/slides_test.go index 11086bee..28839866 100644 --- a/cmd/slides_test.go +++ b/cmd/slides_test.go @@ -832,7 +832,7 @@ func TestSlidesAddTextCommand_ValidationErrors(t *testing.T) { // Capture os.Stderr since PrintError writes to stderr (#190) oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w // Execute the command with a dummy presentation ID @@ -1069,7 +1069,7 @@ func TestSlidesAddTextCommand_NotesValidation(t *testing.T) { } oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w _ = cmd.RunE(cmd, []string{"test-presentation-id"}) @@ -1137,7 +1137,7 @@ func TestSlidesDeleteTextCommand_NotesValidation(t *testing.T) { } oldStderr := os.Stderr - r, w, _ := os.Pipe() + r, w, _ := captureFile(t) os.Stderr = w _ = cmd.RunE(cmd, []string{"test-presentation-id"}) diff --git a/cmd/validation_test.go b/cmd/validation_test.go index c9b83e1a..f77c9bc8 100644 --- a/cmd/validation_test.go +++ b/cmd/validation_test.go @@ -13,9 +13,9 @@ import ( func captureOutput(t *testing.T, fn func()) string { t.Helper() old := os.Stderr - r, w, err := os.Pipe() + r, w, err := captureFile(t) if err != nil { - t.Fatalf("failed to create pipe: %v", err) + t.Fatalf("failed to create capture file: %v", err) } os.Stderr = w