Skip to content

Commit 7badae8

Browse files
committed
cmd/go: guarantee all test output is on stdout
In past releases, whether test output appears on stdout or stderr has varied depending on exactly how go test was invoked and also (indefensibly) on the number of CPUs available. Standardize on standard output for all test output. This is easy to explain and makes go test | go tool test2json work nicely. Change-Id: I605641213fbc6c7ff49e1fd38a0f732045a8383d Reviewed-on: https://go-review.googlesource.com/76871 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 3231d4e commit 7badae8

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

src/cmd/go/internal/test/test.go

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,21 @@ and even 'go test .').
8282
8383
In local directory mode, go test compiles and tests the package sources
8484
found in the current directory and then runs the resulting test binary.
85-
In this mode, the test binary runs with standard output and standard error
86-
connected directly to the go command's own standard output and standard
87-
error, and test result caching (discussed below) is disabled.
88-
After the package test finishes, go test prints to standard output a
89-
summary line showing the test status ('ok' or 'FAIL'), package name,
90-
and elapsed time.
85+
In this mode, caching (discussed below) is disabled. After the package test
86+
finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'),
87+
package name, and elapsed time.
9188
9289
In package list mode, go test compiles and tests each of the packages
9390
listed on the command line. If a package test passes, go test prints only
9491
the final 'ok' summary line. If a package test fails, go test prints the
9592
full test output. If invoked with the -bench or -v flag, go test prints
9693
the full output even for passing package tests, in order to display the
97-
requested benchmark results or verbose logging. In package list mode,
98-
go test prints all test output and summary lines to standard output.
94+
requested benchmark results or verbose logging.
95+
96+
All test output and summary lines are printed to the go command's standard
97+
output, even if the test printed them to its own standard error.
98+
(The go command's standard error is reserved for printing errors building
99+
the tests.)
99100
100101
In package list mode, go test also caches successful package test results.
101102
If go test has cached a previous test run using the same test binary and
@@ -1208,36 +1209,17 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
12081209
if len(pkgArgs) == 0 || testBench {
12091210
// Stream test output (no buffering) when no package has
12101211
// been given on the command line (implicit current directory)
1211-
// or when benchmarking. Allowing stderr to pass through to
1212-
// stderr here is a bit of an historical mistake, but now a
1213-
// documented one. Except in this case, all output is merged
1214-
// to one stream written to stdout.
1212+
// or when benchmarking.
12151213
cmd.Stdout = os.Stdout
1216-
cmd.Stderr = os.Stderr
12171214
} else {
1218-
// If we're only running a single package under test
1219-
// or if parallelism is set to 1, and if we're displaying
1220-
// all output (testShowPass), we can hurry the output along,
1221-
// echoing it as soon as it comes in. We still have to copy
1222-
// to &buf for caching the result. This special case was
1223-
// introduced in Go 1.5 and is intentionally undocumented:
1224-
// the rationale is that the exact details of output buffering
1225-
// are up to the go command and subject to change.
1226-
// NOTE(rsc): Originally this special case also had the effect of
1227-
// allowing stderr to pass through to os.Stderr, unlike
1228-
// the normal buffering that merges stdout and stderr into stdout.
1229-
// This had the truly mysterious result that on a multiprocessor,
1230-
// "go test -v math" could print to stderr,
1231-
// "go test -v math strings" could not, and
1232-
// "go test -p=1 -v math strings" could once again.
1233-
// While I'm willing to let the buffer flush timing
1234-
// fluctuate based on minor details like this,
1235-
// allowing the file descriptor to which output is sent
1236-
// to change as well seems like a serious mistake.
1237-
// Go 1.10 changed this code to allow the less aggressive
1238-
// buffering but still merge all output to standard output.
1239-
// I'd really like to remove this special case entirely,
1240-
// but it is surely very helpful to see progress being made
1215+
// If we're only running a single package under test or if parallelism is
1216+
// set to 1, and if we're displaying all output (testShowPass), we can
1217+
// hurry the output along, echoing it as soon as it comes in.
1218+
// We still have to copy to &buf for caching the result. This special
1219+
// case was introduced in Go 1.5 and is intentionally undocumented:
1220+
// the exact details of output buffering are up to the go command and
1221+
// subject to change. It would be nice to remove this special case
1222+
// entirely, but it is surely very helpful to see progress being made
12411223
// when tests are run on slow single-CPU ARM systems.
12421224
if testShowPass && (len(pkgs) == 1 || cfg.BuildP == 1) {
12431225
// Write both to stdout and buf, for possible saving
@@ -1246,8 +1228,8 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
12461228
} else {
12471229
cmd.Stdout = &buf
12481230
}
1249-
cmd.Stderr = cmd.Stdout
12501231
}
1232+
cmd.Stderr = cmd.Stdout
12511233

12521234
// If there are any local SWIG dependencies, we want to load
12531235
// the shared library from the build directory.

0 commit comments

Comments
 (0)