1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
package testutil
import (
"bytes"
"fmt"
"io"
"os"
)
// CaptureStdout redirects stdout while running fn and returns the output as a string.
// If there's an error during capture, it returns the error, otherwise it returns the error
// returned by fn.
func CaptureStdout(fn func() error) (string, error) {
r, w, err := os.Pipe()
if err != nil {
return "", fmt.Errorf("capture stdout: %v", err)
}
origOut := os.Stdout
defer func() {
os.Stdout = origOut
}()
buf := &bytes.Buffer{}
os.Stdout = w
copyDone := make(chan struct{})
var copyErr error
go func() {
defer close(copyDone)
copied, err := io.Copy(buf, r)
if err != nil {
copyErr = fmt.Errorf("capture stdout: %v, copied: %d\n", err, copied)
return
}
}()
err = fn()
if copyErr != nil {
return "", copyErr
}
if err := w.Close(); err != nil {
return "", fmt.Errorf("capture stdout close pipe reader: %v", err)
}
<-copyDone
return buf.String(), err
}
|