Skip to content

Commit

Permalink
interp: avoid pty.Start in the terminal tests
Browse files Browse the repository at this point in the history
See the added comment for the reason.
Before the fix below, the following command would fail within 2s:

    go test -short -race -count=100 -failfast

After the fix below, the command seems to always succeed.

I'll file a bug in creack/pty following this commit.

Fixes #513.
  • Loading branch information
mvdan committed Sep 27, 2021
1 parent ea77358 commit f7684ec
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions interp/unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,20 @@ func TestRunnerTerminalExec(t *testing.T) {
return out
}, "end\n"},
{"Pseudo", func(t *testing.T, cmd *exec.Cmd) io.Reader {
primary, err := pty.Start(cmd)
// Note that we avoid pty.Start,
// as it closes the secondary terminal via a defer,
// possibly before the command has finished.
// That can lead to "signal: hangup" flakes.
primary, secondary, err := pty.Open()
if err != nil {
t.Fatal(err)
}
cmd.Stdin = secondary
cmd.Stdout = secondary
cmd.Stderr = secondary
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
return primary
}, "012end\r\n"},
}
Expand All @@ -121,15 +131,20 @@ func TestRunnerTerminalExec(t *testing.T) {

cmd := exec.Command(os.Getenv("GOSH_PROG"),
"for n in 0 1 2 3; do if [[ -t $n ]]; then echo -n $n; fi; done; echo end")
out := test.start(t, cmd)
primary := test.start(t, cmd)

got, err := bufio.NewReader(out).ReadString('\n')
got, err := bufio.NewReader(primary).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if got != test.want {
t.Fatalf("\nwant: %q\ngot: %q", test.want, got)
}
if closer, ok := primary.(io.Closer); ok {
if err := closer.Close(); err != nil {
t.Fatal(err)
}
}
if err := cmd.Wait(); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f7684ec

Please sign in to comment.