| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"internal/testenv" |
| 10 |
"internal/testpty" |
| 11 |
"io" |
| 12 |
"os" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"golang.org/x/term" |
| 16 |
) |
| 17 |
|
| 18 |
func TestTerminalPassthrough(t *testing.T) { |
| 19 |
// Check that if 'go test' is run with a terminal connected to stdin/stdout, |
| 20 |
// then the go command passes that terminal down to the test binary |
| 21 |
// invocation (rather than, e.g., putting a pipe in the way). |
| 22 |
// |
| 23 |
// See issue 18153. |
| 24 |
testenv.MustHaveGoBuild(t) |
| 25 |
|
| 26 |
// Start with a "self test" to make sure that if we *don't* pass in a |
| 27 |
// terminal, the test can correctly detect that. (cmd/go doesn't guarantee |
| 28 |
// that it won't add a terminal in the middle, but that would be pretty weird.) |
| 29 |
t.Run("pipe", func(t *testing.T) { |
| 30 |
r, w, err := os.Pipe() |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("pipe failed: %s", err) |
| 33 |
} |
| 34 |
defer r.Close() |
| 35 |
defer w.Close() |
| 36 |
stdout, stderr := runTerminalPassthrough(t, r, w) |
| 37 |
if stdout { |
| 38 |
t.Errorf("stdout is unexpectedly a terminal") |
| 39 |
} |
| 40 |
if stderr { |
| 41 |
t.Errorf("stderr is unexpectedly a terminal") |
| 42 |
} |
| 43 |
}) |
| 44 |
|
| 45 |
// Now test with a read PTY. |
| 46 |
t.Run("pty", func(t *testing.T) { |
| 47 |
r, processTTY, err := testpty.Open() |
| 48 |
if errors.Is(err, testpty.ErrNotSupported) { |
| 49 |
t.Skipf("%s", err) |
| 50 |
} else if err != nil { |
| 51 |
t.Fatalf("failed to open test PTY: %s", err) |
| 52 |
} |
| 53 |
defer r.Close() |
| 54 |
w, err := os.OpenFile(processTTY, os.O_RDWR, 0) |
| 55 |
if err != nil { |
| 56 |
t.Fatal(err) |
| 57 |
} |
| 58 |
defer w.Close() |
| 59 |
stdout, stderr := runTerminalPassthrough(t, r, w) |
| 60 |
if !stdout { |
| 61 |
t.Errorf("stdout is not a terminal") |
| 62 |
} |
| 63 |
if !stderr { |
| 64 |
t.Errorf("stderr is not a terminal") |
| 65 |
} |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
func runTerminalPassthrough(t *testing.T, r, w *os.File) (stdout, stderr bool) { |
| 70 |
cmd := testenv.Command(t, testGo, "test", "-run=^$") |
| 71 |
cmd.Env = append(cmd.Environ(), "GO_TEST_TERMINAL_PASSTHROUGH=1") |
| 72 |
cmd.Stdout = w |
| 73 |
cmd.Stderr = w |
| 74 |
|
| 75 |
// The behavior of reading from a PTY after the child closes it is very |
| 76 |
// strange: on Linux, Read returns EIO, and on at least some versions of |
| 77 |
// macOS, unread output may be discarded (see https://go.dev/issue/57141). |
| 78 |
// |
| 79 |
// To avoid that situation, we keep the child process running until the |
| 80 |
// parent has finished reading from the PTY, at which point we unblock the |
| 81 |
// child by closing its stdin pipe. |
| 82 |
stdin, err := cmd.StdinPipe() |
| 83 |
if err != nil { |
| 84 |
t.Fatal(err) |
| 85 |
} |
| 86 |
|
| 87 |
t.Logf("running %s", cmd) |
| 88 |
err = cmd.Start() |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("starting subprocess: %s", err) |
| 91 |
} |
| 92 |
w.Close() |
| 93 |
t.Cleanup(func() { |
| 94 |
stdin.Close() |
| 95 |
if err := cmd.Wait(); err != nil { |
| 96 |
t.Errorf("suprocess failed with: %s", err) |
| 97 |
} |
| 98 |
}) |
| 99 |
|
| 100 |
buf := make([]byte, 2) |
| 101 |
n, err := io.ReadFull(r, buf) |
| 102 |
if err != nil || !(buf[0] == '1' || buf[0] == 'X') || !(buf[1] == '2' || buf[1] == 'X') { |
| 103 |
t.Logf("read error: %v", err) |
| 104 |
t.Fatalf("expected 2 bytes matching `[1X][2X]`; got %q", buf[:n]) |
| 105 |
} |
| 106 |
return buf[0] == '1', buf[1] == '2' |
| 107 |
} |
| 108 |
|
| 109 |
func init() { |
| 110 |
if os.Getenv("GO_TEST_TERMINAL_PASSTHROUGH") == "" { |
| 111 |
return |
| 112 |
} |
| 113 |
|
| 114 |
if term.IsTerminal(1) { |
| 115 |
os.Stdout.WriteString("1") |
| 116 |
} else { |
| 117 |
os.Stdout.WriteString("X") |
| 118 |
} |
| 119 |
if term.IsTerminal(2) { |
| 120 |
os.Stdout.WriteString("2") |
| 121 |
} else { |
| 122 |
os.Stdout.WriteString("X") |
| 123 |
} |
| 124 |
|
| 125 |
// Before exiting, wait for the parent process to read the PTY output, |
| 126 |
// at which point it will close stdin. |
| 127 |
io.Copy(io.Discard, os.Stdin) |
| 128 |
|
| 129 |
os.Exit(0) |
| 130 |
} |