redirStdout is a helper function to return the standard output from f as a byte slice.
(f func())
| 107 | // redirStdout is a helper function to return the standard output from f as a |
| 108 | // byte slice. |
| 109 | func redirStdout(f func()) ([]byte, error) { |
| 110 | tempFile, err := ioutil.TempFile("", "ss-test") |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | fileName := tempFile.Name() |
| 115 | defer os.Remove(fileName) // Ignore error |
| 116 | |
| 117 | origStdout := os.Stdout |
| 118 | os.Stdout = tempFile |
| 119 | f() |
| 120 | os.Stdout = origStdout |
| 121 | tempFile.Close() |
| 122 | |
| 123 | return ioutil.ReadFile(fileName) |
| 124 | } |
| 125 | |
| 126 | func initSpewTests() { |
| 127 | // Config states with various settings. |