Skip to content

Commit 875f294

Browse files
authored
Merge pull request #298 from gotestyourself/missing-panic-output
Print the test timeout panic as package output
2 parents 4b0ada1 + 2fe7769 commit 875f294

File tree

6 files changed

+113
-10
lines changed

6 files changed

+113
-10
lines changed

.project/golangci-lint.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
linters-settings:
2-
gocyclo:
3-
min-complexity: 12
42
goconst:
53
min-len: 2
64
min-occurrences: 4
@@ -33,7 +31,6 @@ linters:
3331
- errcheck
3432
- gocognit
3533
- goconst
36-
- gocyclo
3734
- gofmt
3835
- goimports
3936
- gosimple

testjson/execution.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ type Package struct {
109109
// shuffleSeed is the seed used to shuffle the tests. The value is set when
110110
// tests are run with -shuffle
111111
shuffleSeed string
112+
113+
// testTimeoutPanicInTest stores the name of a test that received the panic
114+
// output caused by a test timeout. This is necessary to work around a race
115+
// condition in test2json. See https://github.com/golang/go/issues/57305.
116+
testTimeoutPanicInTest string
112117
}
113118

114119
// Result returns if the package passed, failed, or was skipped because there
@@ -398,6 +403,14 @@ func (p *Package) addTestEvent(event TestEvent) {
398403

399404
switch event.Action {
400405
case ActionOutput, ActionBench:
406+
if strings.HasPrefix(event.Output, "panic: test timed out") {
407+
p.testTimeoutPanicInTest = event.Test
408+
}
409+
if p.testTimeoutPanicInTest == event.Test {
410+
p.addOutput(0, event.Output)
411+
return
412+
}
413+
401414
tc := p.running[event.Test]
402415
p.addOutput(tc.ID, event.Output)
403416
return
@@ -491,8 +504,10 @@ func (e *Execution) Failed() []TestCase {
491504
for _, name := range sortedKeys(e.packages) {
492505
pkg := e.packages[name]
493506

494-
// Add package-level failure output if there were no failed tests.
495-
if pkg.TestMainFailed() {
507+
// Add package-level failure output if there were no failed tests, or
508+
// if the test timeout was reached (because we now have to store that
509+
// output on the package).
510+
if pkg.TestMainFailed() || pkg.testTimeoutPanicInTest != "" {
496511
failed = append(failed, TestCase{Package: name})
497512
}
498513
failed = append(failed, pkg.Failed...)

testjson/format.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func testNameFormat(event TestEvent, exec *Execution) string {
6767
}
6868

6969
event.Elapsed = 0 // hide elapsed for now, for backwards compat
70-
return result + " " + packageLine(event, exec)
70+
return result + " " + packageLine(event, exec.Package(event.Package))
7171

7272
case event.Action == ActionFail:
7373
pkg := exec.Package(event.Package)
@@ -152,7 +152,7 @@ func shortFormatPackageEvent(opts FormatOptions, event TestEvent, exec *Executio
152152
}
153153

154154
fmtEvent := func(action string) string {
155-
return action + " " + packageLine(event, exec)
155+
return action + " " + packageLine(event, exec.Package(event.Package))
156156
}
157157
withColor := colorEvent(event)
158158
switch event.Action {
@@ -175,9 +175,7 @@ func shortFormatPackageEvent(opts FormatOptions, event TestEvent, exec *Executio
175175
return ""
176176
}
177177

178-
func packageLine(event TestEvent, exec *Execution) string {
179-
pkg := exec.Package(event.Package)
180-
178+
func packageLine(event TestEvent, pkg *Package) string {
181179
var buf strings.Builder
182180
buf.WriteString(RelativePackagePath(event.Package))
183181

testjson/summary_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99

10+
"gotest.tools/gotestsum/internal/text"
1011
"gotest.tools/v3/assert"
1112
"gotest.tools/v3/golden"
1213
)
@@ -319,3 +320,19 @@ func scanConfigFromGolden(filename string) func(t *testing.T) ScanConfig {
319320
return ScanConfig{Stdout: bytes.NewReader(golden.Get(t, filename))}
320321
}
321322
}
323+
324+
func TestSummary_TestTimeoutPanicRace(t *testing.T) {
325+
source := golden.Get(t, "input/go-test-json-panic-race.out")
326+
cfg := ScanConfig{
327+
Stdout: bytes.NewReader(source),
328+
Handler: &captureHandler{},
329+
}
330+
exec, err := ScanTestOutput(cfg)
331+
assert.NilError(t, err)
332+
333+
out := new(bytes.Buffer)
334+
PrintSummary(out, exec, SummarizeAll)
335+
336+
actual := text.ProcessLines(t, out, text.OpRemoveSummaryLineElapsedTime)
337+
golden.Assert(t, actual, "summary/test-timeout-panic-race")
338+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{"Time":"2022-12-14T09:49:01.562401799Z","Action":"start","Package":"github.com/mafredri/test"}
2+
{"Time":"2022-12-14T09:49:01.569546938Z","Action":"run","Package":"github.com/mafredri/test","Test":"TestHello"}
3+
{"Time":"2022-12-14T09:49:01.569700427Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"=== RUN TestHello\n"}
4+
{"Time":"2022-12-14T09:49:02.569759117Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":" main_test.go:11: Hello\n"}
5+
{"Time":"2022-12-14T09:49:02.56982657Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"--- PASS: TestHello (1.00s)\n"}
6+
{"Time":"2022-12-14T09:49:02.572963923Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"panic: test timed out after 1s\n"}
7+
{"Time":"2022-12-14T09:49:02.572982687Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"running tests:\n"}
8+
{"Time":"2022-12-14T09:49:02.572992095Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\tTestHello (1s)\n"}
9+
{"Time":"2022-12-14T09:49:02.573000907Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\n"}
10+
{"Time":"2022-12-14T09:49:02.573019868Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"goroutine 33 [running]:\n"}
11+
{"Time":"2022-12-14T09:49:02.573029067Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"testing.(*M).startAlarm.func1()\n"}
12+
{"Time":"2022-12-14T09:49:02.573038878Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:2240 +0x3b9\n"}
13+
{"Time":"2022-12-14T09:49:02.573064315Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"created by time.goFunc\n"}
14+
{"Time":"2022-12-14T09:49:02.573079975Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/time/sleep.go:176 +0x32\n"}
15+
{"Time":"2022-12-14T09:49:02.573097493Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\n"}
16+
{"Time":"2022-12-14T09:49:02.573119064Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"goroutine 1 [runnable]:\n"}
17+
{"Time":"2022-12-14T09:49:02.573141104Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"testing.(*T).Run(0xc000083040, {0x5be88c?, 0x4ce6c5?}, 0x6072a0)\n"}
18+
{"Time":"2022-12-14T09:49:02.573162696Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1629 +0x405\n"}
19+
{"Time":"2022-12-14T09:49:02.573178743Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"testing.runTests.func1(0x7438e0?)\n"}
20+
{"Time":"2022-12-14T09:49:02.573203585Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:2035 +0x45\n"}
21+
{"Time":"2022-12-14T09:49:02.57321895Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"testing.tRunner(0xc000083040, 0xc00025fc88)\n"}
22+
{"Time":"2022-12-14T09:49:02.573239542Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1575 +0x10b\n"}
23+
{"Time":"2022-12-14T09:49:02.573342015Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"testing.runTests(0xc0000c0500?, {0x739320, 0x2, 0x2}, {0x0?, 0x100c0000ab938?, 0x743080?})\n"}
24+
{"Time":"2022-12-14T09:49:02.573376752Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:2033 +0x489\n"}
25+
{"Time":"2022-12-14T09:49:02.573403856Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"testing.(*M).Run(0xc0000c0500)\n"}
26+
{"Time":"2022-12-14T09:49:02.573433691Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1905 +0x63a\n"}
27+
{"Time":"2022-12-14T09:49:02.573456763Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"main.main()\n"}
28+
{"Time":"2022-12-14T09:49:02.573483156Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t_testmain.go:49 +0x1aa\n"}
29+
{"Time":"2022-12-14T09:49:02.573503088Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\n"}
30+
{"Time":"2022-12-14T09:49:02.573520911Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"goroutine 20 [runnable]:\n"}
31+
{"Time":"2022-12-14T09:49:02.573539195Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"runtime.goexit1()\n"}
32+
{"Time":"2022-12-14T09:49:02.573576101Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/runtime/proc.go:3616 +0x54\n"}
33+
{"Time":"2022-12-14T09:49:02.573596375Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"runtime.goexit()\n"}
34+
{"Time":"2022-12-14T09:49:02.573620424Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/runtime/asm_amd64.s:1599 +0x6\n"}
35+
{"Time":"2022-12-14T09:49:02.573637148Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"created by testing.(*T).Run\n"}
36+
{"Time":"2022-12-14T09:49:02.573690092Z","Action":"output","Package":"github.com/mafredri/test","Test":"TestHello","Output":"\t/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1628 +0x3ea\n"}
37+
{"Time":"2022-12-14T09:49:02.574702109Z","Action":"pass","Package":"github.com/mafredri/test","Test":"TestHello","Elapsed":1}
38+
{"Time":"2022-12-14T09:49:02.57473959Z","Action":"output","Package":"github.com/mafredri/test","Output":"FAIL\tgithub.com/mafredri/test\t1.012s\n"}
39+
{"Time":"2022-12-14T09:49:02.574754586Z","Action":"fail","Package":"github.com/mafredri/test","Elapsed":1.012}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
=== Failed
3+
=== FAIL: github.com/mafredri/test (0.00s)
4+
panic: test timed out after 1s
5+
running tests:
6+
TestHello (1s)
7+
8+
goroutine 33 [running]:
9+
testing.(*M).startAlarm.func1()
10+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:2240 +0x3b9
11+
created by time.goFunc
12+
/home/mafredri/sdk/go1.20rc1/src/time/sleep.go:176 +0x32
13+
14+
goroutine 1 [runnable]:
15+
testing.(*T).Run(0xc000083040, {0x5be88c?, 0x4ce6c5?}, 0x6072a0)
16+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1629 +0x405
17+
testing.runTests.func1(0x7438e0?)
18+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:2035 +0x45
19+
testing.tRunner(0xc000083040, 0xc00025fc88)
20+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1575 +0x10b
21+
testing.runTests(0xc0000c0500?, {0x739320, 0x2, 0x2}, {0x0?, 0x100c0000ab938?, 0x743080?})
22+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:2033 +0x489
23+
testing.(*M).Run(0xc0000c0500)
24+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1905 +0x63a
25+
main.main()
26+
_testmain.go:49 +0x1aa
27+
28+
goroutine 20 [runnable]:
29+
runtime.goexit1()
30+
/home/mafredri/sdk/go1.20rc1/src/runtime/proc.go:3616 +0x54
31+
runtime.goexit()
32+
/home/mafredri/sdk/go1.20rc1/src/runtime/asm_amd64.s:1599 +0x6
33+
created by testing.(*T).Run
34+
/home/mafredri/sdk/go1.20rc1/src/testing/testing.go:1628 +0x3ea
35+
FAIL github.com/mafredri/test 1.012s
36+
37+
DONE 1 tests, 1 failure

0 commit comments

Comments
 (0)