Skip to content

Commit e2e0350

Browse files
committed
Add more logs when parsing test results failed
1 parent 897bca3 commit e2e0350

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tools/flakeguard/runner/runner.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ func parseTestResults(filePaths []string) ([]reports.TestResult, error) {
107107
defer file.Close()
108108

109109
scanner := bufio.NewScanner(file)
110+
var precedingLines []string // Store preceding lines for context
111+
var followingLines []string // To collect lines after an error
112+
110113
for scanner.Scan() {
114+
line := scanner.Text()
115+
precedingLines = append(precedingLines, line)
116+
117+
// Limit precedingLines to the last 15 lines
118+
if len(precedingLines) > 15 {
119+
precedingLines = precedingLines[1:]
120+
}
121+
111122
var entry struct {
112123
Action string `json:"Action"`
113124
Test string `json:"Test"`
@@ -116,7 +127,14 @@ func parseTestResults(filePaths []string) ([]reports.TestResult, error) {
116127
Elapsed float64 `json:"Elapsed"`
117128
}
118129
if err := json.Unmarshal(scanner.Bytes(), &entry); err != nil {
119-
return nil, fmt.Errorf("failed to parse json test output: %s, err: %w", scanner.Text(), err)
130+
// Collect 15 lines after the error for more context
131+
for scanner.Scan() && len(followingLines) < 15 {
132+
followingLines = append(followingLines, scanner.Text())
133+
}
134+
135+
// Combine precedingLines and followingLines to provide 15 lines before and after
136+
context := append(precedingLines, followingLines...)
137+
return nil, fmt.Errorf("failed to parse json test output near lines:\n%s\nerror: %w", strings.Join(context, "\n"), err)
120138
}
121139

122140
// Only create TestResult for test-level entries

0 commit comments

Comments
 (0)