Skip to content

Commit 43cc104

Browse files
committed
Save rerun test report
1 parent 21f02c1 commit 43cc104

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var RunTestsCmd = &cobra.Command{
3333
timeout, _ := cmd.Flags().GetDuration("timeout")
3434
tags, _ := cmd.Flags().GetStringArray("tags")
3535
useRace, _ := cmd.Flags().GetBool("race")
36-
outputPath, _ := cmd.Flags().GetString("output-json")
36+
mainReportPath, _ := cmd.Flags().GetString("main-report-path")
37+
rerunReportPath, _ := cmd.Flags().GetString("rerun-report-path")
3738
minPassRatio, _ := cmd.Flags().GetFloat64("min-pass-ratio")
3839
// For backward compatibility, check if max-pass-ratio was used
3940
maxPassRatio, _ := cmd.Flags().GetFloat64("max-pass-ratio")
@@ -102,51 +103,47 @@ var RunTestsCmd = &cobra.Command{
102103
FailFast: failFast,
103104
}
104105

105-
// Run the tests
106106
var (
107-
firstReport *reports.TestReport
107+
mainReport *reports.TestReport // Main test report
108+
rerunReport *reports.TestReport // Test report after rerunning failed tests
108109
)
109110

111+
// Run the tests
110112
var err error
111113
if len(testCmdStrings) > 0 {
112-
firstReport, err = testRunner.RunTestCmd(testCmdStrings)
114+
mainReport, err = testRunner.RunTestCmd(testCmdStrings)
113115
if err != nil {
114116
log.Fatal().Err(err).Msg("Error running custom test command")
115117
os.Exit(ErrorExitCode)
116118
}
117119
} else {
118-
firstReport, err = testRunner.RunTestPackages(testPackages)
120+
mainReport, err = testRunner.RunTestPackages(testPackages)
119121
if err != nil {
120122
log.Fatal().Err(err).Msg("Error running test packages")
121123
os.Exit(ErrorExitCode)
122124
}
123125
}
124126

125-
// Save the test results in JSON format
126-
if outputPath != "" && len(firstReport.Results) > 0 {
127-
jsonData, err := json.MarshalIndent(firstReport, "", " ")
128-
if err != nil {
129-
log.Error().Err(err).Msg("Error marshaling test results to JSON")
130-
os.Exit(ErrorExitCode)
131-
}
132-
if err := os.WriteFile(outputPath, jsonData, 0600); err != nil {
133-
log.Error().Err(err).Msg("Error writing test results to file")
127+
// Save the main test report to file
128+
if mainReportPath != "" && len(mainReport.Results) > 0 {
129+
if err := mainReport.SaveToFile(mainReportPath); err != nil {
130+
log.Error().Err(err).Msg("Error saving test results to file")
134131
os.Exit(ErrorExitCode)
135132
}
136-
log.Info().Str("path", outputPath).Msg("Test results saved")
133+
log.Info().Str("path", mainReportPath).Msg("Main test report saved")
137134
}
138135

139-
if len(firstReport.Results) == 0 {
136+
if len(mainReport.Results) == 0 {
140137
log.Warn().Msg("No tests were run for the specified packages")
141138
return
142139
}
143140

144141
fmt.Printf("\nFlakeguard Initial Summary:\n")
145-
reports.RenderResults(os.Stdout, firstReport, false, false)
142+
reports.RenderResults(os.Stdout, mainReport, false, false)
146143

147144
// Rerun failed tests
148145
if rerunFailed > 0 {
149-
rerunReport, err := testRunner.RerunFailedTests(firstReport.Results)
146+
rerunReport, err = testRunner.RerunFailedTests(mainReport.Results)
150147
if err != nil {
151148
log.Fatal().Err(err).Msg("Error rerunning failed tests")
152149
os.Exit(ErrorExitCode)
@@ -159,6 +156,15 @@ var RunTestsCmd = &cobra.Command{
159156
fmt.Printf("\nFlakeguard Rerun Summary:\n")
160157
reports.RenderResults(os.Stdout, rerunReport, false, false)
161158

159+
// Save the rerun test report to file
160+
if rerunReportPath != "" && len(mainReport.Results) > 0 {
161+
if err := rerunReport.SaveToFile(rerunReportPath); err != nil {
162+
log.Error().Err(err).Msg("Error saving test results to file")
163+
os.Exit(ErrorExitCode)
164+
}
165+
log.Info().Str("path", rerunReportPath).Msg("Main test report saved")
166+
}
167+
162168
if len(failedAfterRerun) > 0 {
163169
log.Error().
164170
Int("tests", len(failedAfterRerun)).
@@ -169,23 +175,21 @@ var RunTestsCmd = &cobra.Command{
169175
log.Info().Msg("Failed tests passed at least once after reruns")
170176
os.Exit(0)
171177
}
172-
173-
// TODO: save rerun test report to JSON file
174-
}
175-
176-
// Filter flaky tests using FilterTests
177-
flakyTests := reports.FilterTests(firstReport.Results, func(tr reports.TestResult) bool {
178-
return !tr.Skipped && tr.PassRatio < passRatioThreshold
179-
})
180-
181-
if len(flakyTests) > 0 {
182-
log.Info().
183-
Int("count", len(flakyTests)).
184-
Str("stability threshold", fmt.Sprintf("%.0f%%", passRatioThreshold*100)).
185-
Msg("Found flaky tests")
186-
os.Exit(FlakyTestsExitCode)
187178
} else {
188-
log.Info().Msg("All tests passed stability requirements")
179+
// Filter flaky tests using FilterTests
180+
flakyTests := reports.FilterTests(mainReport.Results, func(tr reports.TestResult) bool {
181+
return !tr.Skipped && tr.PassRatio < passRatioThreshold
182+
})
183+
184+
if len(flakyTests) > 0 {
185+
log.Info().
186+
Int("count", len(flakyTests)).
187+
Str("stability threshold", fmt.Sprintf("%.0f%%", passRatioThreshold*100)).
188+
Msg("Found flaky tests")
189+
os.Exit(FlakyTestsExitCode)
190+
} else {
191+
log.Info().Msg("All tests passed stability requirements")
192+
}
189193
}
190194
},
191195
}
@@ -205,7 +209,8 @@ func init() {
205209
RunTestsCmd.Flags().Bool("shuffle", false, "Enable test shuffling")
206210
RunTestsCmd.Flags().String("shuffle-seed", "", "Set seed for test shuffling. Must be used with --shuffle")
207211
RunTestsCmd.Flags().Bool("fail-fast", false, "Stop on the first test failure")
208-
RunTestsCmd.Flags().String("output-json", "", "Path to output the test results in JSON format")
212+
RunTestsCmd.Flags().String("main-report-path", "", "Path to the main test report in JSON format")
213+
RunTestsCmd.Flags().String("rerun-report-path", "", "Path to the rerun test report in JSON format")
209214
RunTestsCmd.Flags().StringSlice("skip-tests", nil, "Comma-separated list of test names to skip from running")
210215
RunTestsCmd.Flags().StringSlice("select-tests", nil, "Comma-separated list of test names to specifically run")
211216

tools/flakeguard/reports/data.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package reports
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57
"sort"
68
"strings"
79
"time"
@@ -27,6 +29,21 @@ type TestReport struct {
2729
MaxPassRatio float64 `json:"max_pass_ratio,omitempty"`
2830
}
2931

32+
// SaveToFile saves the test report to a JSON file at the given path.
33+
// It returns an error if there's any issue with marshaling the report or writing to the file.
34+
func (testReport *TestReport) SaveToFile(outputPath string) error {
35+
jsonData, err := json.MarshalIndent(testReport, "", " ")
36+
if err != nil {
37+
return fmt.Errorf("error marshaling test results to JSON: %w", err)
38+
}
39+
40+
if err := os.WriteFile(outputPath, jsonData, 0600); err != nil {
41+
return fmt.Errorf("error writing test results to file: %w", err)
42+
}
43+
44+
return nil
45+
}
46+
3047
// GenerateSummaryData generates a summary of a report's test results
3148
func (testReport *TestReport) GenerateSummaryData() {
3249
var runs, testRunCount, passes, fails, skips, panickedTests, racedTests, flakyTests int

0 commit comments

Comments
 (0)