Skip to content

Commit 9128578

Browse files
committed
Refactor and update flag names
1 parent 340e10c commit 9128578

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var RunTestsCmd = &cobra.Command{
3030
testPackagesArg, _ := cmd.Flags().GetStringSlice("test-packages")
3131
testCmdStrings, _ := cmd.Flags().GetStringArray("test-cmd")
3232
runCount, _ := cmd.Flags().GetInt("run-count")
33+
rerunFailedCount, _ := cmd.Flags().GetInt("rerun-failed-count")
3334
timeout, _ := cmd.Flags().GetDuration("timeout")
3435
tags, _ := cmd.Flags().GetStringArray("tags")
3536
useRace, _ := cmd.Flags().GetBool("race")
@@ -45,7 +46,6 @@ var RunTestsCmd = &cobra.Command{
4546
shuffleSeed, _ := cmd.Flags().GetString("shuffle-seed")
4647
omitOutputsOnSuccess, _ := cmd.Flags().GetBool("omit-test-outputs-on-success")
4748
ignoreParentFailuresOnSubtests, _ := cmd.Flags().GetBool("ignore-parent-failures-on-subtests")
48-
rerunFailed, _ := cmd.Flags().GetInt("rerun-failed")
4949
failFast, _ := cmd.Flags().GetBool("fail-fast")
5050

5151
// Handle the compatibility between min/max pass ratio
@@ -99,7 +99,7 @@ var RunTestsCmd = &cobra.Command{
9999
OmitOutputsOnSuccess: omitOutputsOnSuccess,
100100
MaxPassRatio: passRatioThreshold, // Use the calculated threshold
101101
IgnoreParentFailuresOnSubtests: ignoreParentFailuresOnSubtests,
102-
RerunFailed: rerunFailed,
102+
RerunCount: rerunFailedCount,
103103
FailFast: failFast,
104104
}
105105

@@ -143,7 +143,7 @@ var RunTestsCmd = &cobra.Command{
143143
fmt.Println()
144144

145145
// Rerun failed tests
146-
if rerunFailed > 0 {
146+
if rerunFailedCount > 0 {
147147
rerunReport, err = testRunner.RerunFailedTests(mainReport.Results)
148148
if err != nil {
149149
log.Fatal().Err(err).Msg("Error rerunning failed tests")
@@ -170,7 +170,7 @@ var RunTestsCmd = &cobra.Command{
170170
if len(failedAfterRerun) > 0 {
171171
log.Error().
172172
Int("tests", len(failedAfterRerun)).
173-
Int("reruns", rerunFailed).
173+
Int("reruns", rerunFailedCount).
174174
Msg("Tests still failing after reruns with 0 successes")
175175
os.Exit(ErrorExitCode)
176176
} else {
@@ -227,7 +227,7 @@ func init() {
227227
RunTestsCmd.Flags().Bool("ignore-parent-failures-on-subtests", false, "Ignore failures in parent tests when only subtests fail")
228228

229229
// Add rerun failed tests flag
230-
RunTestsCmd.Flags().Int("rerun-failed", 0, "Number of times to rerun failed tests (0 disables reruns)")
230+
RunTestsCmd.Flags().Int("rerun-failed-count", 0, "Number of times to rerun failed tests (0 disables reruns)")
231231
}
232232

233233
func checkDependencies(projectPath string) error {

tools/flakeguard/runner/runner.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ type Runner struct {
3131
prettyProjectPath string // Go project package path, formatted for pretty printing.
3232
Verbose bool // If true, provides detailed logging.
3333
RunCount int // Number of times to run the tests.
34+
RerunCount int // Number of additional runs for tests that initially fail.
3435
UseRace bool // Enable race detector.
3536
Timeout time.Duration // Test timeout
3637
Tags []string // Build tags.
3738
UseShuffle bool // Enable test shuffling. -shuffle=on flag.
3839
ShuffleSeed string // Set seed for test shuffling -shuffle={seed} flag. Must be used with UseShuffle.
3940
FailFast bool // Stop on first test failure.
40-
RerunFailed int // Number of additional runs for tests that initially fail.
4141
SkipTests []string // Test names to exclude.
4242
SelectTests []string // Test names to include.
4343
CollectRawOutput bool // Set to true to collect test output for later inspection.
@@ -77,22 +77,11 @@ func (r *Runner) RunTestPackages(packages []string) (*reports.TestReport, error)
7777
}
7878

7979
// Parse initial results.
80-
results, err := r.parseTestResults(jsonFilePaths, "run")
80+
results, err := r.parseTestResults(jsonFilePaths, "run", r.RunCount)
8181
if err != nil {
8282
return nil, fmt.Errorf("failed to parse test results: %w", err)
8383
}
8484

85-
// Rerun failing tests (only the unique tests).
86-
// if r.RerunFailed > 0 {
87-
// rerunResults, err := r.rerunFailedTests(results)
88-
// if err != nil {
89-
// return nil, fmt.Errorf("failed to rerun failing tests: %w", err)
90-
// }
91-
92-
// // Merge rerun results with initial results.
93-
// mergeTestResults(&results, rerunResults)
94-
// }
95-
9685
report := &reports.TestReport{
9786
GoProject: r.prettyProjectPath,
9887
RaceDetection: r.UseRace,
@@ -124,7 +113,7 @@ func (r *Runner) RunTestCmd(testCmd []string) (*reports.TestReport, error) {
124113
}
125114
}
126115

127-
results, err := r.parseTestResults(jsonFilePaths, "run")
116+
results, err := r.parseTestResults(jsonFilePaths, "run", r.RunCount)
128117
if err != nil {
129118
return nil, fmt.Errorf("failed to parse test results: %w", err)
130119
}
@@ -229,10 +218,6 @@ func (r *Runner) runCmd(testCmd []string, runIndex int) (tempFilePath string, pa
229218
}
230219
defer tmpFile.Close()
231220

232-
if r.Verbose {
233-
log.Info().Msgf("Running custom test command (%d/%d): %s", runIndex+1, r.RunCount, strings.Join(testCmd, " "))
234-
}
235-
236221
cmd := exec.Command(testCmd[0], testCmd[1:]...)
237222
cmd.Dir = r.ProjectPath
238223

@@ -300,7 +285,7 @@ func (e entry) String() string {
300285
// panics and failures at that point.
301286
// Subtests add more complexity, as panics in subtests are only reported in their parent's output,
302287
// and cannot be accurately attributed to the subtest that caused them.
303-
func (r *Runner) parseTestResults(filePaths []string, runPrefix string) ([]reports.TestResult, error) {
288+
func (r *Runner) parseTestResults(filePaths []string, runPrefix string, runCount int) ([]reports.TestResult, error) {
304289
// If the option is enabled, transform each JSON output file before parsing.
305290
if r.IgnoreParentFailuresOnSubtests {
306291
transformedPaths, err := r.transformTestOutputFiles(filePaths)
@@ -319,7 +304,7 @@ func (r *Runner) parseTestResults(filePaths []string, runPrefix string) ([]repor
319304
panicDetectionMode = false
320305
raceDetectionMode = false
321306
detectedEntries = []entry{} // race or panic entries
322-
expectedRuns = r.RunCount
307+
expectedRuns = runCount
323308
)
324309

325310
runNumber := 0
@@ -755,7 +740,7 @@ func (r *Runner) RerunFailedTests(results []reports.TestResult) (*reports.TestRe
755740
var rerunJsonFilePaths []string
756741

757742
// Rerun each failing test package up to RerunFailed times
758-
for i := range r.RerunFailed {
743+
for i := range r.RerunCount {
759744
for pkg, tests := range failingTestsByPackage {
760745
// Build regex pattern to match all failing tests in this package
761746
testPattern := fmt.Sprintf("^(%s)$", strings.Join(tests, "|"))
@@ -794,7 +779,7 @@ func (r *Runner) RerunFailedTests(results []reports.TestResult) (*reports.TestRe
794779
}
795780

796781
// Parse all rerun results at once with a consistent prefix
797-
rerunResults, err := r.parseTestResults(rerunJsonFilePaths, "rerun")
782+
rerunResults, err := r.parseTestResults(rerunJsonFilePaths, "rerun", r.RerunCount)
798783
if err != nil {
799784
return nil, fmt.Errorf("failed to parse rerun results: %w", err)
800785
}

tools/flakeguard/runner/runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ func TestRerunFailed(t *testing.T) {
545545
ProjectPath: "./",
546546
Verbose: true,
547547
RunCount: 2, // Run tests twice initially
548-
RerunFailed: 3, // Rerun failing tests 3 more times
548+
RerunCount: 3, // Rerun failing tests 3 more times
549549
SelectTests: []string{"TestFail"}, // This test is known to always fail
550550
CollectRawOutput: true,
551551
}

0 commit comments

Comments
 (0)