18
19
// parseTestTimeouts parses the stdout of a test run and returns the stacktrace and names of tests that timed out.
21
>
lines := strings.Split(strings.ReplaceAll(stdout, "\r\n", "\n"), "\n")
22
>
for i := 0; i < len(lines); i++ {
23
>
line := lines[i]
24
>
if strings.HasPrefix(line, "FAIL") {
25
>
// ignore
26
>
} else if strings.HasPrefix(line, "panic: test timed out after") {
27
>
// parse names of tests that timed out
28
>
for {
29
>
i++
30
>
line = strings.TrimSpace(lines[i])
31
>
if strings.HasPrefix(line, "Test") {
32
>
timedoutTests = append(timedoutTests, strings.Split(line, " ")[0])
33
>
}
34
>
if line == "" {
35
>
break
36
}
37
}
39
>
// collect stracktrace
40
>
stacktrace += line + "\n"
41
>
}
42
}
43
45
>
len(timedoutTests), strings.Join(timedoutTests, "\n\t"), testOnlyStacktrace(stacktrace))
46
>
return
47
}
48
49
// testOnlyStacktrace removes all but the test stacktraces from the full stacktrace.
51
>
var res string
52
>
snap, _, err := stack.ScanSnapshot(strings.NewReader(stacktrace), io.Discard, stack.DefaultOpts())
53
>
if err != nil && err != io.EOF {
54
return fmt.Sprintf("failed to parse stacktrace: %v", err)
55
}
57
return "failed to find a stacktrace"
58
}
60
>
for _, goroutine := range snap.Goroutines {
61
>
shouldPrint := slices.ContainsFunc(goroutine.Stack.Calls, func(call stack.Call) bool {
62
>
return strings.HasSuffix(call.RemoteSrcPath, "_test.go")
63
>
})
64
>
if shouldPrint {
65
>
res += fmt.Sprintf("\tgoroutine %d [%v]:\n", goroutine.ID, goroutine.State)
66
>
for _, call := range goroutine.Stack.Calls {
67
>
file := call.RemoteSrcPath
68
>
res += fmt.Sprintf("\t\t%s:%d\n", file, call.Line)
69
>
}
70
>
res += "\n"
71
}
72
}
74
}
75