captureStdout replaces os.Stdout for the duration of fn and returns what was written.
(t *testing.T, fn func())
| 20 | |
| 21 | // captureStdout replaces os.Stdout for the duration of fn and returns what was written. |
| 22 | func 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. |
| 46 | func captureStderr(t *testing.T, fn func()) string { |
no test coverage detected