2019-05-06 23:18:10 +02:00
|
|
|
package util
|
2018-11-17 13:57:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Returns output to stdout and stderr for testing
|
2019-05-03 18:57:28 +02:00
|
|
|
func CaptureOutput(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()
|
|
|
|
}
|