Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 94ceb20

Browse files
committed
make combinedOutput actually output both stdout and stderr
Signed-off-by: Miguel Molina <[email protected]>
1 parent 0a63c47 commit 94ceb20

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"os"
9+
)
10+
11+
func main() {
12+
os.Stdout.WriteString("stdout")
13+
os.Stderr.WriteString("stderr")
14+
}

internal/gps/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ func (c *monitoredCmd) hasTimedOut() bool {
126126
}
127127

128128
func (c *monitoredCmd) combinedOutput(ctx context.Context) ([]byte, error) {
129+
c.cmd.Stderr = c.stdout
129130
if err := c.run(ctx); err != nil {
130-
return c.stderr.Bytes(), err
131+
return c.stdout.Bytes(), err
131132
}
132133

133-
// FIXME(sdboyer) this is not actually combined output
134134
return c.stdout.Bytes(), nil
135135
}
136136

internal/gps/cmd_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"os/exec"
12+
"strings"
1213
"testing"
1314
"time"
1415
)
@@ -26,7 +27,7 @@ func TestMonitoredCmd(t *testing.T) {
2627
t.Skip("skipping test with sleeps on short")
2728
}
2829

29-
err := exec.Command("go", "build", "./_testdata/cmd/echosleep.go").Run()
30+
err := exec.Command("go", "build", "./_testdata/cmd/echosleep/echosleep.go").Run()
3031
if err != nil {
3132
t.Errorf("Unable to build echosleep binary: %s", err)
3233
}
@@ -89,3 +90,35 @@ func TestMonitoredCmd(t *testing.T) {
8990
}
9091
})
9192
}
93+
94+
func TestCombinedOutput(t *testing.T) {
95+
// Compile makes this a bit slow
96+
if testing.Short() {
97+
t.Skip("skipping test with compilation on short")
98+
}
99+
100+
err := exec.Command("go", "build", "./_testdata/cmd/stdout_stderr/stdout_stderr.go").Run()
101+
if err != nil {
102+
t.Errorf("Unable to build stdout_stderr binary: %s", err)
103+
}
104+
defer os.Remove("./stdout_stderr")
105+
106+
cmd := newMonitoredCmd(
107+
exec.Command("./stdout_stderr"),
108+
490*time.Millisecond,
109+
)
110+
111+
out, err := cmd.combinedOutput(context.Background())
112+
if err != nil {
113+
t.Errorf("Unexpected error: %s", err)
114+
}
115+
116+
output := string(out)
117+
if !strings.Contains(output, "stdout") {
118+
t.Errorf("Expecting to receive output from stdout")
119+
}
120+
121+
if !strings.Contains(output, "stderr") {
122+
t.Errorf("Expecting to receive output from stderr")
123+
}
124+
}

0 commit comments

Comments
 (0)