Skip to content

Commit

Permalink
Improve the test for input from stdin
Browse files Browse the repository at this point in the history
The test would not fail without the change because it was not making any
assertions about the input. Adding a check for "DONE 1" causes it to
fail properly when the support is removed.

Also add an example to the docs of how to use stdin.
  • Loading branch information
dnephin committed Apr 9, 2022
1 parent ce158b4 commit ccf51de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 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
30 changes: 14 additions & 16 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"os/exec"
"strings"
"testing"
"time"

"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 @@ -402,43 +403,40 @@ func TestRun_RerunFails_PanicPreventsRerun(t *testing.T) {
assert.ErrorContains(t, err, "rerun aborted because previous run had a suspected panic", out.String())
}

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

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

os.Stdin = r

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

e := json.NewEncoder(w)
for _, event := range []struct {
Time *time.Time `json:",omitempty"`
Action string
Package string `json:",omitempty"`
Test string `json:",omitempty"`
Elapsed float64 `json:",omitempty"`
Output string `json:",omitempty"`
}{
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.NilError(t, e.Encode(event))
assert.Check(t, e.Encode(event))
}
}()

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

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

0 comments on commit ccf51de

Please sign in to comment.