MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / captureStdout

Function captureStdout

internal/cli/cli_helpers_test.go:22–43  ·  view source on GitHub ↗

captureStdout replaces os.Stdout for the duration of fn and returns what was written.

(t *testing.T, fn func())

Source from the content-addressed store, hash-verified

20
21// captureStdout replaces os.Stdout for the duration of fn and returns what was written.
22func captureStdout(t *testing.T, fn func()) string {
23 t.Helper()
24
25 r, w, err := os.Pipe()
26 if err != nil {
27 t.Fatalf("os.Pipe: %v", err)
28 }
29
30 old := os.Stdout
31 os.Stdout = w
32
33 fn()
34
35 _ = w.Close()
36 os.Stdout = old
37
38 var buf bytes.Buffer
39
40 _, _ = io.Copy(&buf, r)
41
42 return buf.String()
43}
44
45// captureStderr replaces os.Stderr for the duration of fn and returns what was written.
46func captureStderr(t *testing.T, fn func()) string {

Callers 2

TestPrintJSONFunction · 0.85
TestPrintTableFunction · 0.85

Calls 2

CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected