captureStderr replaces os.Stderr for the duration of fn and returns what was written.
(t *testing.T, fn func())
| 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 { |
| 47 | t.Helper() |
| 48 | |
| 49 | r, w, err := os.Pipe() |
| 50 | if err != nil { |
| 51 | t.Fatalf("os.Pipe: %v", err) |
| 52 | } |
| 53 | |
| 54 | old := os.Stderr |
| 55 | os.Stderr = w |
| 56 | |
| 57 | fn() |
| 58 | |
| 59 | _ = w.Close() |
| 60 | os.Stderr = old |
| 61 | |
| 62 | var buf bytes.Buffer |
| 63 | |
| 64 | _, _ = io.Copy(&buf, r) |
| 65 | |
| 66 | return buf.String() |
| 67 | } |
| 68 | |
| 69 | func TestPrintJSON(t *testing.T) { |
| 70 | type payload struct { |
no test coverage detected