Skip to content

Commit cc0c1a1

Browse files
authored
Merge pull request #234 from uhthomas/exec-stdin
Pass stdin to go test
2 parents 0fa88f3 + ac4bc6f commit cc0c1a1

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ stdout and stderr output:
232232
stderr, not the `test2json` stdout). Any stderr produced by tests is not
233233
considered an error (it will be in the `test2json` stdout).
234234

235+
**Example: accept intput from stdin**
236+
```
237+
cat out.json | gotestsum --raw-command -- cat
238+
```
239+
235240
**Example: run tests with profiling enabled**
236241

237242
Using a `profile.sh` script like this:

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ func startGoTest(ctx context.Context, args []string) (*proc, error) {
356356
}
357357

358358
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
359+
cmd.Stdin = os.Stdin
359360
p := proc{cmd: cmd}
360361
log.Debugf("exec: %s", cmd.Args)
361362
var err error

cmd/main_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package cmd
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"os"
67
"os/exec"
78
"strings"
89
"testing"
910

11+
"gotest.tools/gotestsum/testjson"
1012
"gotest.tools/v3/assert"
13+
"gotest.tools/v3/assert/cmp"
1114
"gotest.tools/v3/env"
1215
"gotest.tools/v3/golden"
1316
)
@@ -399,3 +402,41 @@ func TestRun_RerunFails_PanicPreventsRerun(t *testing.T) {
399402
err := run(opts)
400403
assert.ErrorContains(t, err, "rerun aborted because previous run had a suspected panic", out.String())
401404
}
405+
406+
func TestRun_InputFromStdin(t *testing.T) {
407+
stdin := os.Stdin
408+
t.Cleanup(func() { os.Stdin = stdin })
409+
410+
r, w, err := os.Pipe()
411+
assert.NilError(t, err)
412+
t.Cleanup(func() { _ = r.Close() })
413+
414+
os.Stdin = r
415+
416+
go func() {
417+
defer func() { _ = w.Close() }()
418+
419+
e := json.NewEncoder(w)
420+
for _, event := range []testjson.TestEvent{
421+
{Action: "run", Package: "pkg"},
422+
{Action: "run", Package: "pkg", Test: "TestOne"},
423+
{Action: "fail", Package: "pkg", Test: "TestOne"},
424+
{Action: "fail", Package: "pkg"},
425+
} {
426+
assert.Check(t, e.Encode(event))
427+
}
428+
}()
429+
430+
stdout := new(bytes.Buffer)
431+
err = run(&options{
432+
args: []string{"cat"},
433+
format: "testname",
434+
hideSummary: newHideSummaryValue(),
435+
rawCommand: true,
436+
437+
stdout: stdout,
438+
stderr: os.Stderr,
439+
})
440+
assert.NilError(t, err)
441+
assert.Assert(t, cmp.Contains(stdout.String(), "DONE 1"))
442+
}

0 commit comments

Comments
 (0)