Skip to content

Commit

Permalink
Improve rerun flags
Browse files Browse the repository at this point in the history
Rename the rerun-fails flag, and improve the help text. Also add a test for options.Validate
  • Loading branch information
dnephin committed Jun 11, 2020
1 parent d1c4fe0 commit 8440739
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
flags.Var(opts.junitTestCaseClassnameFormat, "junitfile-testcase-classname",
"format the testcase classname field as: "+junitFieldFormatValues)

flags.IntVar(&opts.rerunFailsMaxAttempts, "rerun-fails-max-attempts", 0,
"rerun failed tests until each one passes once, or attempts exceeds max")
flags.IntVar(&opts.rerunFailsMaxAttempts, "rerun-fails", 0,
"rerun failed tests until they all pass, or attempts exceeds maximum. Defaults to max 2 reruns when enabled.")
flags.Lookup("rerun-fails").NoOptDefVal = "2"
flags.IntVar(&opts.rerunFailsMaxInitialFailures, "rerun-fails-max-failures", 10,
"do not rerun any tests if the initial run has more than this number of failures")
flags.Var((*stringSlice)(&opts.packages), "packages",
Expand Down
52 changes: 52 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,58 @@ func TestUsage_WithFlagsFromSetupFlags(t *testing.T) {
golden.Assert(t, buf.String(), "gotestsum-help-text")
}

func TestOptions_Validate_FromFlags(t *testing.T) {
type testCase struct {
name string
args []string
expected string
}
fn := func(t *testing.T, tc testCase) {
flags, opts := setupFlags("gotestsum")
err := flags.Parse(tc.args)
assert.NilError(t, err)
opts.args = flags.Args()

err = opts.Validate()
if tc.expected == "" {
assert.NilError(t, err)
return
}
assert.ErrorContains(t, err, tc.expected, "opts: %#v", opts)
}
var testCases = []testCase{
{
name: "no flags",
},
{
name: "rerun flag, raw command",
args: []string{"--rerun-fails", "--raw-command", "--", "./test-all"},
},
{
name: "rerun flag, no go-test args",
args: []string{"--rerun-fails", "--"},
},
{
name: "rerun flag, go-test args, no packages flag",
args: []string{"--rerun-fails", "--", "./..."},
expected: "the list of packages to test must be specified by the --packages flag",
},
{
name: "rerun flag, go-test args, with packages flag",
args: []string{"--rerun-fails", "--packages", "./...", "--", "--foo"},
},
{
name: "rerun flag, no go-test args, with packages flag",
args: []string{"--rerun-fails", "--packages", "./..."},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fn(t, tc)
})
}
}

func TestGoTestCmdArgs(t *testing.T) {
type testCase struct {
opts *options
Expand Down
4 changes: 2 additions & 2 deletions testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Flags:
--packages list space separated list of package to test
--post-run-command command command to run after the tests have completed
--raw-command don't prepend 'go test -json' to the 'go test' command
--rerun-fails-max-attempts int rerun failed tests until each one passes once, or attempts exceeds max
--rerun-fails-max-failures int avoid re-run if initial run had more than this number of failures (default 10)
--rerun-fails int[=2] rerun failed tests until they all pass, or attempts exceeds maximum. Defaults to max 2 reruns when enabled.
--rerun-fails-max-failures int do not rerun any tests if the initial run has more than this number of failures (default 10)
--version show version and exit

Formats:
Expand Down

0 comments on commit 8440739

Please sign in to comment.