Skip to content

Commit db384f4

Browse files
committed
Add go-test-count flag to test runner
1 parent 69c8081 commit db384f4

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ var RunTestsCmd = &cobra.Command{
5757
ignoreParentFailuresOnSubtests, _ := cmd.Flags().GetBool("ignore-parent-failures-on-subtests")
5858
failFast, _ := cmd.Flags().GetBool("fail-fast")
5959

60+
// Retrieve go-test-count flag as a pointer if explicitly provided.
61+
var goTestCount *int
62+
if cmd.Flags().Changed("go-test-count") {
63+
v, err := cmd.Flags().GetInt("go-test-count")
64+
if err != nil {
65+
log.Error().Err(err).Msg("Error retrieving flag go-test-count")
66+
flushSummaryAndExit(ErrorExitCode)
67+
}
68+
goTestCount = &v
69+
}
70+
6071
// Handle the compatibility between min/max pass ratio
6172
passRatioThreshold := minPassRatio
6273
if maxPassRatioSpecified && maxPassRatio != 1.0 {
@@ -100,7 +111,8 @@ var RunTestsCmd = &cobra.Command{
100111
RunCount: runCount,
101112
Timeout: timeout,
102113
Tags: tags,
103-
UseRace: useRace,
114+
GoTestCountFlag: goTestCount,
115+
GoTestRaceFlag: useRace,
104116
SkipTests: skipTests,
105117
SelectTests: selectTests,
106118
UseShuffle: useShuffle,
@@ -243,6 +255,7 @@ func init() {
243255
RunTestsCmd.Flags().IntP("run-count", "c", 1, "Number of times to run the tests")
244256
RunTestsCmd.Flags().Duration("timeout", 0, "Passed on to the 'go test' command as the -timeout flag")
245257
RunTestsCmd.Flags().StringArray("tags", nil, "Passed on to the 'go test' command as the -tags flag")
258+
RunTestsCmd.Flags().Int("go-test-count", -1, "go test -count flag value. By default -count flag is not passed to go test")
246259
RunTestsCmd.Flags().Bool("race", false, "Enable the race detector")
247260
RunTestsCmd.Flags().Bool("shuffle", false, "Enable test shuffling")
248261
RunTestsCmd.Flags().String("shuffle-seed", "", "Set seed for test shuffling. Must be used with --shuffle")

tools/flakeguard/runner/runner.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ type Runner struct {
3232
Verbose bool // If true, provides detailed logging.
3333
RunCount int // Number of times to run the tests.
3434
RerunCount int // Number of additional runs for tests that initially fail.
35-
UseRace bool // Enable race detector.
35+
GoTestCountFlag *int // Run go test with -count flag.
36+
GoTestRaceFlag bool // Run go test with -race flag.
3637
Timeout time.Duration // Test timeout
3738
Tags []string // Build tags.
3839
UseShuffle bool // Enable test shuffling. -shuffle=on flag.
@@ -84,7 +85,7 @@ func (r *Runner) RunTestPackages(packages []string) (*reports.TestReport, error)
8485

8586
report := &reports.TestReport{
8687
GoProject: r.prettyProjectPath,
87-
RaceDetection: r.UseRace,
88+
RaceDetection: r.GoTestRaceFlag,
8889
ExcludedTests: r.SkipTests,
8990
SelectedTests: r.SelectTests,
9091
Results: results,
@@ -120,7 +121,7 @@ func (r *Runner) RunTestCmd(testCmd []string) (*reports.TestReport, error) {
120121

121122
report := &reports.TestReport{
122123
GoProject: r.prettyProjectPath,
123-
RaceDetection: r.UseRace,
124+
RaceDetection: r.GoTestRaceFlag,
124125
ExcludedTests: r.SkipTests,
125126
SelectedTests: r.SelectTests,
126127
Results: results,
@@ -143,8 +144,11 @@ type exitCoder interface {
143144

144145
// runTestPackage runs the tests for a given package and returns the path to the output file.
145146
func (r *Runner) runTestPackage(packageName string) (string, bool, error) {
146-
args := []string{"test", packageName, "-json", "-count=1"}
147-
if r.UseRace {
147+
args := []string{"test", packageName, "-json"}
148+
if r.GoTestCountFlag != nil {
149+
args = append(args, fmt.Sprintf("-count=%d", *r.GoTestCountFlag))
150+
}
151+
if r.GoTestRaceFlag {
148152
args = append(args, "-race")
149153
}
150154
if r.Timeout > 0 {
@@ -748,7 +752,7 @@ func (r *Runner) RerunFailedTests(failedTests []reports.TestResult) (*reports.Te
748752
}
749753

750754
// Add other test flags
751-
if r.UseRace {
755+
if r.GoTestRaceFlag {
752756
cmd = append(cmd, "-race")
753757
}
754758
if r.Timeout > 0 {
@@ -779,7 +783,7 @@ func (r *Runner) RerunFailedTests(failedTests []reports.TestResult) (*reports.Te
779783

780784
report := &reports.TestReport{
781785
GoProject: r.prettyProjectPath,
782-
RaceDetection: r.UseRace,
786+
RaceDetection: r.GoTestRaceFlag,
783787
ExcludedTests: r.SkipTests,
784788
SelectedTests: r.SelectTests,
785789
Results: rerunResults,

tools/flakeguard/runner/runner_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestRun(t *testing.T) {
6565
ProjectPath: "./",
6666
Verbose: true,
6767
RunCount: defaultTestRunCount,
68-
UseRace: false,
68+
GoTestRaceFlag: false,
6969
SkipTests: []string{"TestPanic", "TestFlakyPanic", "TestSubTestsSomePanic", "TestTimeout"},
7070
FailFast: false,
7171
CollectRawOutput: true,
@@ -163,7 +163,7 @@ func TestRun(t *testing.T) {
163163
ProjectPath: "./",
164164
Verbose: true,
165165
RunCount: defaultTestRunCount,
166-
UseRace: false,
166+
GoTestRaceFlag: false,
167167
SkipTests: []string{},
168168
SelectTests: []string{"TestPanic"},
169169
FailFast: false,
@@ -183,7 +183,7 @@ func TestRun(t *testing.T) {
183183
ProjectPath: "./",
184184
Verbose: true,
185185
RunCount: defaultTestRunCount,
186-
UseRace: false,
186+
GoTestRaceFlag: false,
187187
SkipTests: []string{},
188188
SelectTests: []string{"TestFlakyPanic"},
189189
FailFast: false,
@@ -203,7 +203,7 @@ func TestRun(t *testing.T) {
203203
ProjectPath: "./",
204204
Verbose: true,
205205
RunCount: defaultTestRunCount,
206-
UseRace: false,
206+
GoTestRaceFlag: false,
207207
SkipTests: []string{},
208208
SelectTests: []string{"TestSubTestsSomePanic"},
209209
FailFast: false,
@@ -233,7 +233,7 @@ func TestRun(t *testing.T) {
233233
ProjectPath: "./",
234234
Verbose: true,
235235
RunCount: defaultTestRunCount,
236-
UseRace: false,
236+
GoTestRaceFlag: false,
237237
SkipTests: []string{},
238238
SelectTests: []string{"TestFail", "TestPass"},
239239
FailFast: true,
@@ -293,7 +293,7 @@ func TestRun(t *testing.T) {
293293
require.Equal(t, tc.runner.RunCount, testReport.SummaryData.TestRunCount, "unexpected number of test runs")
294294
}
295295

296-
require.Equal(t, tc.runner.UseRace, testReport.RaceDetection, "unexpected race usage")
296+
require.Equal(t, tc.runner.GoTestRaceFlag, testReport.RaceDetection, "unexpected race usage")
297297

298298
assert.Equal(t, len(tc.expectedTests), len(testReport.Results), "unexpected number of test results")
299299
for _, result := range testReport.Results {

0 commit comments

Comments
 (0)