Skip to content

Commit 9da2004

Browse files
authored
Flakeguard add more logs and fix parsing stderr (#1343)
1 parent 897bca3 commit 9da2004

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

tools/flakeguard/runner/runner.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type exitCoder interface {
5555
// runTests runs the tests for a given package and returns the path to the output file.
5656
func (r *Runner) runTests(packageName string) (string, bool, error) {
5757
args := []string{"test", packageName, "-json", "-count=1"} // Enable JSON output
58+
args = append(args, "2>/dev/null") // Redirect stderr to null
5859
if r.UseRace {
5960
args = append(args, "-race")
6061
}
@@ -107,7 +108,18 @@ func parseTestResults(filePaths []string) ([]reports.TestResult, error) {
107108
defer file.Close()
108109

109110
scanner := bufio.NewScanner(file)
111+
var precedingLines []string // Store preceding lines for context
112+
var followingLines []string // To collect lines after an error
113+
110114
for scanner.Scan() {
115+
line := scanner.Text()
116+
precedingLines = append(precedingLines, line)
117+
118+
// Limit precedingLines to the last 15 lines
119+
if len(precedingLines) > 15 {
120+
precedingLines = precedingLines[1:]
121+
}
122+
111123
var entry struct {
112124
Action string `json:"Action"`
113125
Test string `json:"Test"`
@@ -116,7 +128,14 @@ func parseTestResults(filePaths []string) ([]reports.TestResult, error) {
116128
Elapsed float64 `json:"Elapsed"`
117129
}
118130
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)
131+
// Collect 15 lines after the error for more context
132+
for scanner.Scan() && len(followingLines) < 15 {
133+
followingLines = append(followingLines, scanner.Text())
134+
}
135+
136+
// Combine precedingLines and followingLines to provide 15 lines before and after
137+
context := append(precedingLines, followingLines...)
138+
return nil, fmt.Errorf("failed to parse json test output near lines:\n%s\nerror: %w", strings.Join(context, "\n"), err)
120139
}
121140

122141
// Only create TestResult for test-level entries

0 commit comments

Comments
 (0)