Skip to content

Commit 0d81b8b

Browse files
Copilotlpcox
andauthored
test: make stdout capture in completion tests non-blocking
Agent-Logs-Url: https://github.com/github/gh-aw-mcpg/sessions/0247f7a2-b758-496f-b765-effc1dd5b27f Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
1 parent 00111c5 commit 0d81b8b

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

internal/cmd/completion_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,30 @@ func captureStdoutDuring(t *testing.T, fn func()) string {
2323

2424
orig := os.Stdout
2525
os.Stdout = w
26-
// Safety net: restore if the function panics before we can restore manually.
27-
t.Cleanup(func() {
26+
27+
defer func() {
2828
if os.Stdout != orig {
2929
os.Stdout = orig
3030
}
31-
})
31+
_ = w.Close()
32+
_ = r.Close()
33+
}()
34+
35+
var buf bytes.Buffer
36+
copyDone := make(chan error, 1)
37+
go func() {
38+
_, copyErr := io.Copy(&buf, r)
39+
copyDone <- copyErr
40+
}()
3241

3342
fn()
3443

35-
w.Close()
3644
os.Stdout = orig // restore immediately so repeated calls in the same test work
37-
38-
var buf bytes.Buffer
39-
_, err = io.Copy(&buf, r)
40-
r.Close()
45+
err = w.Close()
46+
require.NoError(t, err)
47+
err = <-copyDone
48+
require.NoError(t, err)
49+
err = r.Close()
4150
require.NoError(t, err)
4251

4352
return buf.String()

0 commit comments

Comments
 (0)