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

Commit

Permalink
Merge pull request #881 from erizocosmico/fix/cmd-combined-output
Browse files Browse the repository at this point in the history
make combinedOutput actually output both stdout and stderr
  • Loading branch information
sdboyer authored Jul 31, 2017
2 parents 857a410 + 94ceb20 commit b6765a5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
14 changes: 14 additions & 0 deletions internal/gps/_testdata/cmd/stdout_stderr/stdout_stderr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"os"
)

func main() {
os.Stdout.WriteString("stdout")
os.Stderr.WriteString("stderr")
}
4 changes: 2 additions & 2 deletions internal/gps/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func (c *monitoredCmd) hasTimedOut() bool {
}

func (c *monitoredCmd) combinedOutput(ctx context.Context) ([]byte, error) {
c.cmd.Stderr = c.stdout
if err := c.run(ctx); err != nil {
return c.stderr.Bytes(), err
return c.stdout.Bytes(), err
}

// FIXME(sdboyer) this is not actually combined output
return c.stdout.Bytes(), nil
}

Expand Down
35 changes: 34 additions & 1 deletion internal/gps/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)
Expand All @@ -26,7 +27,7 @@ func TestMonitoredCmd(t *testing.T) {
t.Skip("skipping test with sleeps on short")
}

err := exec.Command("go", "build", "./_testdata/cmd/echosleep.go").Run()
err := exec.Command("go", "build", "./_testdata/cmd/echosleep/echosleep.go").Run()
if err != nil {
t.Errorf("Unable to build echosleep binary: %s", err)
}
Expand Down Expand Up @@ -89,3 +90,35 @@ func TestMonitoredCmd(t *testing.T) {
}
})
}

func TestCombinedOutput(t *testing.T) {
// Compile makes this a bit slow
if testing.Short() {
t.Skip("skipping test with compilation on short")
}

err := exec.Command("go", "build", "./_testdata/cmd/stdout_stderr/stdout_stderr.go").Run()
if err != nil {
t.Errorf("Unable to build stdout_stderr binary: %s", err)
}
defer os.Remove("./stdout_stderr")

cmd := newMonitoredCmd(
exec.Command("./stdout_stderr"),
490*time.Millisecond,
)

out, err := cmd.combinedOutput(context.Background())
if err != nil {
t.Errorf("Unexpected error: %s", err)
}

output := string(out)
if !strings.Contains(output, "stdout") {
t.Errorf("Expecting to receive output from stdout")
}

if !strings.Contains(output, "stderr") {
t.Errorf("Expecting to receive output from stderr")
}
}

0 comments on commit b6765a5

Please sign in to comment.