Skip to content

Commit 077b079

Browse files
author
Gabriel Monroy
committed
fix(tests): use streamOutput for test runs
1 parent c0d7b20 commit 077b079

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

tests/utils/utils.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package utils
44

55
import (
6+
"bufio"
67
"bytes"
78
"crypto/rand"
89
"fmt"
@@ -62,11 +63,16 @@ func HostAddress() string {
6263
// We infer the hostname because we don't necessarily know how to log in.
6364
func Hostname() string {
6465
switch HostAddress() {
65-
case "172.17.8.100": return "deis-1"
66-
case "172.17.8.101": return "deis-2"
67-
case "172.17.8.102": return "deis-3"
68-
case "172.21.12.100": return "docker-registry"
69-
default: return "boot2docker"
66+
case "172.17.8.100":
67+
return "deis-1"
68+
case "172.17.8.101":
69+
return "deis-2"
70+
case "172.17.8.102":
71+
return "deis-3"
72+
case "172.21.12.100":
73+
return "docker-registry"
74+
default:
75+
return "boot2docker"
7076
}
7177
}
7278

@@ -99,6 +105,34 @@ func Rmdir(app string) error {
99105
return err
100106
}
101107

108+
// streamOutput from a source to a destination buffer while also printing
109+
func streamOutput(src io.Reader, dst *bytes.Buffer, out io.Writer) error {
110+
111+
s := bufio.NewReader(src)
112+
113+
for {
114+
var line []byte
115+
line, err := s.ReadSlice('\n')
116+
if err == io.EOF && len(line) == 0 {
117+
break // done
118+
}
119+
if err == io.EOF {
120+
return fmt.Errorf("Improper termination: %v", line)
121+
}
122+
if err != nil {
123+
return err
124+
}
125+
126+
// append to the buffer
127+
dst.Write(line)
128+
129+
// write to stdout/stderr also
130+
out.Write(line)
131+
}
132+
133+
return nil
134+
}
135+
102136
// RunCommandWithStdoutStderr execs a command and returns its output.
103137
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
104138
var stdout, stderr bytes.Buffer
@@ -116,12 +150,10 @@ func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, erro
116150
}
117151

118152
go func() {
119-
io.Copy(&stdout, stdoutPipe)
120-
fmt.Println(stdout.String())
153+
streamOutput(stdoutPipe, &stdout, os.Stdout)
121154
}()
122155
go func() {
123-
io.Copy(&stderr, stderrPipe)
124-
fmt.Println(stderr.String())
156+
streamOutput(stderrPipe, &stderr, os.Stderr)
125157
}()
126158
time.Sleep(2000 * time.Millisecond)
127159
err = cmd.Wait()

0 commit comments

Comments
 (0)