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
6 changes: 3 additions & 3 deletions cmd/calendar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions cmd/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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{})
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/raw_runners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions cmd/slides_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"})
Expand Down Expand Up @@ -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"})
Expand Down
4 changes: 2 additions & 2 deletions cmd/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading