Skip to content

Commit

Permalink
Merge pull request #234 from uhthomas/exec-stdin
Browse files Browse the repository at this point in the history
Pass stdin to go test
  • Loading branch information
dnephin authored Apr 9, 2022
2 parents 0fa88f3 + ac4bc6f commit cc0c1a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ stdout and stderr output:
stderr, not the `test2json` stdout). Any stderr produced by tests is not
considered an error (it will be in the `test2json` stdout).

**Example: accept intput from stdin**
```
cat out.json | gotestsum --raw-command -- cat
```

**Example: run tests with profiling enabled**

Using a `profile.sh` script like this:
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func startGoTest(ctx context.Context, args []string) (*proc, error) {
}

cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Stdin = os.Stdin
p := proc{cmd: cmd}
log.Debugf("exec: %s", cmd.Args)
var err error
Expand Down
41 changes: 41 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package cmd

import (
"bytes"
"encoding/json"
"os"
"os/exec"
"strings"
"testing"

"gotest.tools/gotestsum/testjson"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/env"
"gotest.tools/v3/golden"
)
Expand Down Expand Up @@ -399,3 +402,41 @@ func TestRun_RerunFails_PanicPreventsRerun(t *testing.T) {
err := run(opts)
assert.ErrorContains(t, err, "rerun aborted because previous run had a suspected panic", out.String())
}

func TestRun_InputFromStdin(t *testing.T) {
stdin := os.Stdin
t.Cleanup(func() { os.Stdin = stdin })

r, w, err := os.Pipe()
assert.NilError(t, err)
t.Cleanup(func() { _ = r.Close() })

os.Stdin = r

go func() {
defer func() { _ = w.Close() }()

e := json.NewEncoder(w)
for _, event := range []testjson.TestEvent{
{Action: "run", Package: "pkg"},
{Action: "run", Package: "pkg", Test: "TestOne"},
{Action: "fail", Package: "pkg", Test: "TestOne"},
{Action: "fail", Package: "pkg"},
} {
assert.Check(t, e.Encode(event))
}
}()

stdout := new(bytes.Buffer)
err = run(&options{
args: []string{"cat"},
format: "testname",
hideSummary: newHideSummaryValue(),
rawCommand: true,

stdout: stdout,
stderr: os.Stderr,
})
assert.NilError(t, err)
assert.Assert(t, cmp.Contains(stdout.String(), "DONE 1"))
}

0 comments on commit cc0c1a1

Please sign in to comment.