mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 02:44:02 +03:00
FILE / ScuroNeko/mtg
testlib/capture_output.go
Исходный файл и его история в репозитории.
43 lines
639 B
Go
43 lines
639 B
Go
package testlib
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func CaptureStdout(callback func()) string {
|
|
return captureOutput(&os.Stdout, callback)
|
|
}
|
|
|
|
func CaptureStderr(callback func()) string {
|
|
return captureOutput(&os.Stderr, callback)
|
|
}
|
|
|
|
func captureOutput(filefp **os.File, callback func()) string {
|
|
oldFp := *filefp
|
|
|
|
defer func() {
|
|
*filefp = oldFp
|
|
}()
|
|
|
|
reader, writer, _ := os.Pipe()
|
|
buf := &bytes.Buffer{}
|
|
closeChan := make(chan bool)
|
|
|
|
go func() {
|
|
io.Copy(buf, reader) // nolint: errcheck
|
|
close(closeChan)
|
|
}()
|
|
|
|
*filefp = writer
|
|
|
|
callback()
|
|
|
|
writer.Close()
|
|
<-closeChan
|
|
|
|
return strings.TrimSpace(buf.String())
|
|
}
|