36 lines
427 B
Go
36 lines
427 B
Go
|
package test
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"io"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
// Returns output to stdout and stderr for testing
|
||
|
func Capture(f func()) string {
|
||
|
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()
|
||
|
}
|