MCPcopy Create free account
hub / github.com/checkmake/checkmake / captureOutput

Function captureOutput

cmd/checkmake/main_test.go:18–37  ·  view source on GitHub ↗

captureOutput temporarily redirects os.Stdout during f(), returning what was printed. Useful for testing formatters that write directly to os.Stdout instead of Cobra's writer.

(f func())

Source from the content-addressed store, hash-verified

16// captureOutput temporarily redirects os.Stdout during f(), returning what was printed.
17// Useful for testing formatters that write directly to os.Stdout instead of Cobra's writer.
18func captureOutput(f func()) string {
19 var buf bytes.Buffer
20 stdout := os.Stdout
21 r, w, _ := os.Pipe()
22 os.Stdout = w
23
24 done := make(chan struct{})
25 go func() {
26 _, _ = io.Copy(&buf, r)
27 close(done)
28 }()
29
30 f()
31
32 w.Close()
33 <-done
34 os.Stdout = stdout
35
36 return buf.String()
37}
38
39func TestCheckmake_NoArgsShowsHelp(t *testing.T) {
40 out := captureOutput(func() {

Calls 1

CloseMethod · 0.80

Tested by

no test coverage detected