Skip to content

Commit

Permalink
test2json: use indentation to determine source of test output
Browse files Browse the repository at this point in the history
If a test prints output after a subtest has started it was
being attributed to the last subtest. This change uses the identation
level of the output to determine which test the output comes from.
  • Loading branch information
dnephin committed Apr 11, 2020
1 parent 264aed2 commit 03b1b73
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/cmd/internal/test2json/test2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,18 @@ func (c *converter) handleInputLine(line []byte) {
}
}

// Not a special test output line.
if !ok {
// Not a special test output line.
// Lookup the name of the test which produced the output using the
// indentation of the output as an index into the stack of the current
// subtests.
// If the indentation is greater than the number of current subtests
// then the output must have included extra indentation. We can't
// determine which subtest produced this output, so we default to the
// old behaviour of assuming the most recently run subtest produced it.
if indent > 0 && indent <= len(c.report) {
c.testName = c.report[indent-1].Test
}
c.output.write(origLine)
return
}
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/internal/test2json/testdata/issue29755.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{"Action":"run","Test":"TestOutputWithSubtest"}
{"Action":"output","Test":"TestOutputWithSubtest","Output":"=== RUN TestOutputWithSubtest\n"}
{"Action":"run","Test":"TestOutputWithSubtest/sub_test"}
{"Action":"output","Test":"TestOutputWithSubtest/sub_test","Output":"=== RUN TestOutputWithSubtest/sub_test\n"}
{"Action":"output","Test":"TestOutputWithSubtest","Output":"--- FAIL: TestOutputWithSubtest (0.00s)\n"}
{"Action":"output","Test":"TestOutputWithSubtest","Output":" foo_test.go:6: output before sub tests\n"}
{"Action":"output","Test":"TestOutputWithSubtest/sub_test","Output":" --- PASS: TestOutputWithSubtest/sub_test (0.00s)\n"}
{"Action":"output","Test":"TestOutputWithSubtest/sub_test","Output":" foo_test.go:9: output from sub test\n"}
{"Action":"output","Test":"TestOutputWithSubtest","Output":" foo_test.go:12: output after sub test\n"}
{"Action":"pass","Test":"TestOutputWithSubtest/sub_test"}
{"Action":"fail","Test":"TestOutputWithSubtest"}
{"Action":"output","Output":"FAIL\n"}
{"Action":"output","Output":"FAIL gotest.tools/gotestsum/foo 0.001s\n"}
{"Action":"output","Output":"FAIL\n"}
{"Action":"fail"}
10 changes: 10 additions & 0 deletions src/cmd/internal/test2json/testdata/issue29755.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== RUN TestOutputWithSubtest
=== RUN TestOutputWithSubtest/sub_test
--- FAIL: TestOutputWithSubtest (0.00s)
foo_test.go:6: output before sub tests
--- PASS: TestOutputWithSubtest/sub_test (0.00s)
foo_test.go:9: output from sub test
foo_test.go:12: output after sub test
FAIL
FAIL gotest.tools/gotestsum/foo 0.001s
FAIL

0 comments on commit 03b1b73

Please sign in to comment.