Skip to content

Commit cdc77d3

Browse files
author
Bryan C. Mills
committed
testing: treat PAUSE lines as changing the active test name
We could instead fix cmd/test2json to treat PAUSE lines as *not* changing the active test name, but that seems like it would be more confusing to humans, and also wouldn't fix tools that parse output using existing builds of cmd/test2json. Fixes #40657 Change-Id: I937611778f5b1e7dd1d6e9f44424d7e725a589ed Reviewed-on: https://go-review.googlesource.com/c/go/+/248727 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Jean de Klerk <[email protected]>
1 parent 93eeb81 commit cdc77d3

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Regression test for https://golang.org/issue/40657: output from the main test
2+
# function should be attributed correctly even if interleaved with the PAUSE
3+
# line for a new parallel subtest.
4+
5+
[short] skip
6+
7+
go test -json
8+
stdout '"Test":"TestWeirdTiming","Output":"[^"]* logging to outer again\\n"'
9+
10+
-- go.mod --
11+
module example.com
12+
go 1.15
13+
-- main_test.go --
14+
package main
15+
16+
import (
17+
"testing"
18+
)
19+
20+
func TestWeirdTiming(outer *testing.T) {
21+
outer.Run("pauser", func(pauser *testing.T) {
22+
outer.Logf("logging to outer")
23+
pauser.Parallel()
24+
})
25+
26+
outer.Logf("logging to outer again")
27+
}

src/testing/testing.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,19 @@ func (p *testPrinter) Fprint(w io.Writer, testName, out string) {
357357
defer p.lastNameMu.Unlock()
358358

359359
if !p.chatty ||
360-
strings.HasPrefix(out, "--- PASS") ||
361-
strings.HasPrefix(out, "--- FAIL") ||
362-
strings.HasPrefix(out, "=== CONT") ||
363-
strings.HasPrefix(out, "=== RUN") {
360+
strings.HasPrefix(out, "--- PASS: ") ||
361+
strings.HasPrefix(out, "--- FAIL: ") ||
362+
strings.HasPrefix(out, "--- SKIP: ") ||
363+
strings.HasPrefix(out, "=== RUN ") ||
364+
strings.HasPrefix(out, "=== CONT ") ||
365+
strings.HasPrefix(out, "=== PAUSE ") {
366+
// If we're buffering test output (!p.chatty), we don't really care which
367+
// test is emitting which line so long as they are serialized.
368+
//
369+
// If the message already implies an association with a specific new test,
370+
// we don't need to check what the old test name was or log an extra CONT
371+
// line for it. (We're updating it anyway, and the current message already
372+
// includes the test name.)
364373
p.lastName = testName
365374
fmt.Fprint(w, out)
366375
return
@@ -976,7 +985,13 @@ func (t *T) Parallel() {
976985
for ; root.parent != nil; root = root.parent {
977986
}
978987
root.mu.Lock()
979-
fmt.Fprintf(root.w, "=== PAUSE %s\n", t.name)
988+
// Unfortunately, even though PAUSE indicates that the named test is *no
989+
// longer* running, cmd/test2json interprets it as changing the active test
990+
// for the purpose of log parsing. We could fix cmd/test2json, but that
991+
// won't fix existing deployments of third-party tools that already shell
992+
// out to older builds of cmd/test2json — so merely fixing cmd/test2json
993+
// isn't enough for now.
994+
printer.Fprint(root.w, t.name, fmt.Sprintf("=== PAUSE %s\n", t.name))
980995
root.mu.Unlock()
981996
}
982997

0 commit comments

Comments
 (0)