Skip to content

Commit

Permalink
Pass stdin to go test
Browse files Browse the repository at this point in the history
  • Loading branch information
uhthomas committed Feb 22, 2022
1 parent 661b091 commit 8ab5def
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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
43 changes: 43 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

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

"gotest.tools/v3/assert"
"gotest.tools/v3/env"
Expand Down Expand Up @@ -399,3 +401,44 @@ 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 TestExecStdin(t *testing.T) {
stdin := os.Stdin
t.Cleanup(func() { os.Stdin = stdin })

r, w, err := os.Pipe()
assert.NilError(t, err)
defer r.Close()

os.Stdin = r

go func() {
defer 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"`
}{
{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.NilError(t, run(&options{
args: []string{"cat"},
format: "testname",
hideSummary: newHideSummaryValue(),
rawCommand: true,

stdout: os.Stdout,
stderr: os.Stderr,
}))
}

0 comments on commit 8ab5def

Please sign in to comment.