2020-01-06 15:22:46 +01:00
|
|
|
package capture
|
2018-11-17 13:57:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-01-12 14:25:58 +01:00
|
|
|
// Output returns output to stdout and stderr for testing.
|
2020-01-06 15:22:46 +01:00
|
|
|
func Output(f func()) string {
|
2018-11-17 13:57:19 +01:00
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := os.Stdout
|
|
|
|
os.Stdout = w
|
|
|
|
defer func() {
|
|
|
|
os.Stdout = stdout
|
|
|
|
}()
|
|
|
|
|
|
|
|
stderr := os.Stderr
|
|
|
|
os.Stderr = w
|
|
|
|
defer func() {
|
|
|
|
os.Stderr = stderr
|
|
|
|
}()
|
|
|
|
|
|
|
|
f()
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, r)
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|