File tree Expand file tree Collapse file tree 2 files changed +2
-50
lines changed
Expand file tree Collapse file tree 2 files changed +2
-50
lines changed Original file line number Diff line number Diff line change @@ -125,17 +125,6 @@ func TestWithCustomStdout(t *testing.T) {
125125 assertEqualWithLineBreak (t , "stderr\n stdout" , c .Combined ())
126126}
127127
128- func TestWithStandardStreams (t * testing.T ) {
129- out , err := CaptureStandardOutput (func () interface {} {
130- c := NewCommand (">&2 echo stderr; sleep 0.01; echo stdout;" , WithStandardStreams )
131- err := c .Execute ()
132- return err
133- })
134-
135- assertEqualWithLineBreak (t , "stderr\n stdout" , out )
136- assert .Nil (t , err )
137- }
138-
139128func TestWithEnvironmentVariables (t * testing.T ) {
140129 c := NewCommand ("echo $env" , WithEnvironmentVariables (map [string ]string {"env" : "value" }))
141130 c .Execute ()
Original file line number Diff line number Diff line change 11package cmd
22
33import (
4- "bytes"
5- "github.com/stretchr/testify/assert"
6- "io"
7- "log"
8- "os"
94 "runtime"
10- "sync"
115 "testing"
12- )
136
14- // CaptureStandardOutput allows to capture the output which will be written
15- // to os.Stdout and os.Stderr.
16- // It returns the captured output and the return value of the called function
17- func CaptureStandardOutput (f func () interface {}) (string , interface {}) {
18- reader , writer , err := os .Pipe ()
19- if err != nil {
20- panic (err )
21- }
22- stdout := os .Stdout
23- stderr := os .Stderr
24- defer func () {
25- os .Stdout = stdout
26- os .Stderr = stderr
27- log .SetOutput (os .Stderr )
28- }()
29- os .Stdout = writer
30- os .Stderr = writer
31- log .SetOutput (writer )
32- out := make (chan string )
33- wg := new (sync.WaitGroup )
34- wg .Add (1 )
35- go func () {
36- var buf bytes.Buffer
37- wg .Done ()
38- io .Copy (& buf , reader )
39- out <- buf .String ()
40- }()
41- wg .Wait ()
42- result := f ()
43- writer .Close ()
44- return <- out , result
45- }
7+ "github.com/stretchr/testify/assert"
8+ )
469
4710func assertEqualWithLineBreak (t * testing.T , expected string , actual string ) {
4811 if runtime .GOOS == "windows" {
You can’t perform that action at this time.
0 commit comments