1 Star 0 Fork 0

powerpaas/machine

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
stdout_capture.go 690 Bytes
Copy Edit Raw Blame History
David Gageot authored 2015-11-25 20:41 +08:00 . Add more command tests
package commandstest
import (
"bytes"
"io"
"os"
)
var (
stdout *os.File
)
func init() {
stdout = os.Stdout
}
type StdoutGetter interface {
Output() string
Stop()
}
type stdoutCapturer struct {
stdout *os.File
output chan string
}
func NewStdoutGetter() StdoutGetter {
r, w, _ := os.Pipe()
os.Stdout = w
output := make(chan string)
go func() {
var testOutput bytes.Buffer
io.Copy(&testOutput, r)
output <- testOutput.String()
}()
return &stdoutCapturer{
stdout: w,
output: output,
}
}
func (c *stdoutCapturer) Output() string {
c.stdout.Close()
text := <-c.output
close(c.output)
return text
}
func (c *stdoutCapturer) Stop() {
os.Stdout = stdout
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/machine.git
git@gitee.com:powerpaas/machine.git
powerpaas
machine
machine
v0.13.0

Search