From 414e0d3afa263e62e959b511ae2b823bd1791bab Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Fri, 27 Sep 2024 11:42:05 -0700 Subject: [PATCH] Run CI on supported versions of Go --- .github/workflows/ci.yml | 19 +++++++++++---- app_test.go | 34 +++++++++++++++++++++----- args_test.go | 15 ++++++++---- cmd_test.go | 27 ++++++++++++++++----- completions_test.go | 5 ++++ flags_test.go | 52 ++++++++++++++++++++++++++++++---------- parser_test.go | 17 ++++++++----- usage_test.go | 9 ++++++- values_test.go | 13 ++++++++-- 9 files changed, 148 insertions(+), 43 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 22bb4f4..dd1a28c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,15 +3,24 @@ on: branches: - master pull_request: + name: CI jobs: test: name: Test - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + go-version: ["oldstable", "stable"] steps: - - name: Checkout code + - name: Checkout Repository uses: actions/checkout@v4 - - name: Init Hermit - run: ./bin/hermit env -r >> $GITHUB_ENV + + - name: Setup Golang Environment + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + - name: Test - run: go test ./... + run: go test ./... -race -shuffle=on -v diff --git a/app_test.go b/app_test.go index 27c95e5..3c519a4 100644 --- a/app_test.go +++ b/app_test.go @@ -2,14 +2,13 @@ package kingpin import ( "errors" - "io/ioutil" - - "github.com/stretchr/testify/assert" - + "io" "sort" "strings" "testing" "time" + + "github.com/stretchr/testify/assert" ) func newTestApp() *Application { @@ -17,6 +16,7 @@ func newTestApp() *Application { } func TestCommander(t *testing.T) { + t.Parallel() c := newTestApp() ping := c.Command("ping", "Ping an IP address.") pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration() @@ -33,6 +33,7 @@ func TestCommander(t *testing.T) { } func TestRequiredFlags(t *testing.T) { + t.Parallel() c := newTestApp() c.Flag("a", "a").String() c.Flag("b", "b").Required().String() @@ -44,6 +45,7 @@ func TestRequiredFlags(t *testing.T) { } func TestRepeatableFlags(t *testing.T) { + t.Parallel() c := newTestApp() c.Flag("a", "a").String() c.Flag("b", "b").Strings() @@ -54,6 +56,7 @@ func TestRepeatableFlags(t *testing.T) { } func TestInvalidDefaultFlagValueErrors(t *testing.T) { + t.Parallel() c := newTestApp() c.Flag("foo", "foo").Default("a").Int() _, err := c.Parse([]string{}) @@ -61,6 +64,7 @@ func TestInvalidDefaultFlagValueErrors(t *testing.T) { } func TestInvalidDefaultArgValueErrors(t *testing.T) { + t.Parallel() c := newTestApp() cmd := c.Command("cmd", "cmd") cmd.Arg("arg", "arg").Default("one").Int() @@ -69,6 +73,7 @@ func TestInvalidDefaultArgValueErrors(t *testing.T) { } func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) { + t.Parallel() c := newTestApp() cmd := c.Command("cmd", "") cmd.Arg("a", "a").String() @@ -78,7 +83,8 @@ func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) { } func TestArgsMultipleRequiredThenNonRequired(t *testing.T) { - c := newTestApp().Writer(ioutil.Discard) + t.Parallel() + c := newTestApp().Writer(io.Discard) cmd := c.Command("cmd", "") cmd.Arg("a", "a").Required().String() cmd.Arg("b", "b").Required().String() @@ -91,6 +97,7 @@ func TestArgsMultipleRequiredThenNonRequired(t *testing.T) { } func TestDispatchCallbackIsCalled(t *testing.T) { + t.Parallel() dispatched := false c := newTestApp() c.Command("cmd", "").Action(func(*ParseContext) error { @@ -104,6 +111,7 @@ func TestDispatchCallbackIsCalled(t *testing.T) { } func TestTopLevelArgWorks(t *testing.T) { + t.Parallel() c := newTestApp() s := c.Arg("arg", "help").String() _, err := c.Parse([]string{"foo"}) @@ -112,6 +120,7 @@ func TestTopLevelArgWorks(t *testing.T) { } func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) { + t.Parallel() c := newTestApp() c.Arg("arg", "help").String() c.Command("cmd", "help") @@ -120,6 +129,7 @@ func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) { } func TestTooManyArgs(t *testing.T) { + t.Parallel() a := newTestApp() a.Arg("a", "").String() _, err := a.Parse([]string{"a", "b"}) @@ -127,6 +137,7 @@ func TestTooManyArgs(t *testing.T) { } func TestTooManyArgsAfterCommand(t *testing.T) { + t.Parallel() a := newTestApp() a.Command("a", "") assert.NoError(t, a.init()) @@ -135,6 +146,7 @@ func TestTooManyArgsAfterCommand(t *testing.T) { } func TestArgsLooksLikeFlagsWithConsumeRemainder(t *testing.T) { + t.Parallel() a := newTestApp() a.Arg("opts", "").Required().Strings() _, err := a.Parse([]string{"hello", "-world"}) @@ -142,6 +154,7 @@ func TestArgsLooksLikeFlagsWithConsumeRemainder(t *testing.T) { } func TestCommandParseDoesNotResetFlagsToDefault(t *testing.T) { + t.Parallel() app := newTestApp() flag := app.Flag("flag", "").Default("default").String() app.Command("cmd", "") @@ -152,6 +165,7 @@ func TestCommandParseDoesNotResetFlagsToDefault(t *testing.T) { } func TestCommandParseDoesNotFailRequired(t *testing.T) { + t.Parallel() app := newTestApp() flag := app.Flag("flag", "").Required().String() app.Command("cmd", "") @@ -162,6 +176,7 @@ func TestCommandParseDoesNotFailRequired(t *testing.T) { } func TestSelectedCommand(t *testing.T) { + t.Parallel() app := newTestApp() c0 := app.Command("c0", "") c0.Command("c1", "") @@ -171,6 +186,7 @@ func TestSelectedCommand(t *testing.T) { } func TestSubCommandRequired(t *testing.T) { + t.Parallel() app := newTestApp() c0 := app.Command("c0", "") c0.Command("c1", "") @@ -179,6 +195,7 @@ func TestSubCommandRequired(t *testing.T) { } func TestInterspersedFalse(t *testing.T) { + t.Parallel() app := newTestApp().Interspersed(false) a1 := app.Arg("a1", "").String() a2 := app.Arg("a2", "").String() @@ -192,6 +209,7 @@ func TestInterspersedFalse(t *testing.T) { } func TestInterspersedTrue(t *testing.T) { + t.Parallel() // test once with the default value and once with explicit true for i := 0; i < 2; i++ { app := newTestApp() @@ -214,6 +232,7 @@ func TestInterspersedTrue(t *testing.T) { } func TestDefaultEnvars(t *testing.T) { + t.Parallel() a := New("some-app", "").Terminate(nil).DefaultEnvars() f0 := a.Flag("some-flag", "") f0.Bool() @@ -229,6 +248,7 @@ func TestDefaultEnvars(t *testing.T) { } func TestBashCompletionOptionsWithEmptyApp(t *testing.T) { + t.Parallel() a := newTestApp() context, err := a.ParseContext([]string{"--completion-bash"}) if err != nil { @@ -239,6 +259,7 @@ func TestBashCompletionOptionsWithEmptyApp(t *testing.T) { } func TestBashCompletionOptions(t *testing.T) { + t.Parallel() a := newTestApp() a.Command("one", "") a.Flag("flag-0", "").String() @@ -411,10 +432,10 @@ func TestBashCompletionOptions(t *testing.T) { assert.Equal(t, c.ExpectedOptions, args, "Expected != Actual: [%v] != [%v]. \nInput was: [%v]", c.ExpectedOptions, args, c.Args) } - } func TestCmdValidation(t *testing.T) { + t.Parallel() c := newTestApp() cmd := c.Command("cmd", "") @@ -436,6 +457,7 @@ func TestCmdValidation(t *testing.T) { } func TestVersion(t *testing.T) { + t.Parallel() c := newTestApp() c.Flag("config", "path to config file").Default("config.yaml").ExistingFile() c.Version("1.0.0") diff --git a/args_test.go b/args_test.go index 695b208..0207960 100644 --- a/args_test.go +++ b/args_test.go @@ -1,14 +1,14 @@ package kingpin import ( - "io/ioutil" - "os" + "io" "testing" "github.com/stretchr/testify/assert" ) func TestArgRemainder(t *testing.T) { + t.Parallel() app := New("test", "") v := app.Arg("test", "").Strings() args := []string{"hello", "world"} @@ -18,6 +18,7 @@ func TestArgRemainder(t *testing.T) { } func TestArgRemainderErrorsWhenNotLast(t *testing.T) { + t.Parallel() a := newArgGroup() a.Arg("test", "").Strings() a.Arg("test2", "").String() @@ -25,9 +26,10 @@ func TestArgRemainderErrorsWhenNotLast(t *testing.T) { } func TestArgMultipleRequired(t *testing.T) { + t.Parallel() terminated := false app := New("test", "") - app.Version("0.0.0").Writer(ioutil.Discard) + app.Version("0.0.0").Writer(io.Discard) app.Arg("a", "").Required().String() app.Arg("b", "").Required().String() app.Terminate(func(int) { terminated = true }) @@ -43,6 +45,7 @@ func TestArgMultipleRequired(t *testing.T) { } func TestInvalidArgsDefaultCanBeOverridden(t *testing.T) { + t.Parallel() app := New("test", "") app.Arg("a", "").Default("invalid").Bool() _, err := app.Parse([]string{}) @@ -50,6 +53,7 @@ func TestInvalidArgsDefaultCanBeOverridden(t *testing.T) { } func TestArgMultipleValuesDefault(t *testing.T) { + t.Parallel() app := New("test", "") a := app.Arg("a", "").Default("default1", "default2").Strings() _, err := app.Parse([]string{}) @@ -58,6 +62,7 @@ func TestArgMultipleValuesDefault(t *testing.T) { } func TestRequiredArgWithEnvarMissingErrors(t *testing.T) { + t.Parallel() app := newTestApp() app.Arg("t", "").Envar("TEST_ARG_ENVAR").Required().Int() _, err := app.Parse([]string{}) @@ -65,7 +70,7 @@ func TestRequiredArgWithEnvarMissingErrors(t *testing.T) { } func TestArgRequiredWithEnvar(t *testing.T) { - os.Setenv("TEST_ARG_ENVAR", "123") + t.Setenv("TEST_ARG_ENVAR", "123") app := newTestApp() flag := app.Arg("t", "").Envar("TEST_ARG_ENVAR").Required().Int() _, err := app.Parse([]string{}) @@ -74,7 +79,7 @@ func TestArgRequiredWithEnvar(t *testing.T) { } func TestSubcommandArgRequiredWithEnvar(t *testing.T) { - os.Setenv("TEST_ARG_ENVAR", "123") + t.Setenv("TEST_ARG_ENVAR", "123") app := newTestApp() cmd := app.Command("command", "") flag := cmd.Arg("t", "").Envar("TEST_ARG_ENVAR").Required().Int() diff --git a/cmd_test.go b/cmd_test.go index 8f307a0..10e0942 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -3,10 +3,9 @@ package kingpin import ( "sort" "strings" + "testing" "github.com/stretchr/testify/assert" - - "testing" ) func parseAndExecute(app *Application, context *ParseContext) (string, error) { @@ -23,6 +22,7 @@ func parseAndExecute(app *Application, context *ParseContext) (string, error) { } func complete(t *testing.T, app *Application, args ...string) []string { + t.Helper() context, err := app.ParseContext(args) assert.NoError(t, err) if err != nil { @@ -36,6 +36,7 @@ func complete(t *testing.T, app *Application, args ...string) []string { } func TestNestedCommands(t *testing.T) { + t.Parallel() app := New("app", "") sub1 := app.Command("sub1", "") sub1.Flag("sub1", "") @@ -54,6 +55,7 @@ func TestNestedCommands(t *testing.T) { } func TestNestedCommandsWithArgs(t *testing.T) { + t.Parallel() app := New("app", "") cmd := app.Command("a", "").Command("b", "") a := cmd.Arg("a", "").String() @@ -68,6 +70,7 @@ func TestNestedCommandsWithArgs(t *testing.T) { } func TestNestedCommandsWithFlags(t *testing.T) { + t.Parallel() app := New("app", "") cmd := app.Command("a", "").Command("b", "") a := cmd.Flag("aaa", "").Short('a').String() @@ -84,6 +87,7 @@ func TestNestedCommandsWithFlags(t *testing.T) { } func TestNestedCommandWithMergedFlags(t *testing.T) { + t.Parallel() app := New("app", "") cmd0 := app.Command("a", "") cmd0f0 := cmd0.Flag("aflag", "").Bool() @@ -102,6 +106,7 @@ func TestNestedCommandWithMergedFlags(t *testing.T) { } func TestNestedCommandWithDuplicateFlagErrors(t *testing.T) { + t.Parallel() app := New("app", "") app.Flag("test", "").Bool() app.Command("cmd0", "").Flag("test", "").Bool() @@ -110,6 +115,7 @@ func TestNestedCommandWithDuplicateFlagErrors(t *testing.T) { } func TestNestedCommandWithArgAndMergedFlags(t *testing.T) { + t.Parallel() app := New("app", "") cmd0 := app.Command("a", "") cmd0f0 := cmd0.Flag("aflag", "").Bool() @@ -130,6 +136,7 @@ func TestNestedCommandWithArgAndMergedFlags(t *testing.T) { } func TestDefaultSubcommandEOL(t *testing.T) { + t.Parallel() app := newTestApp() c0 := app.Command("c0", "").Default() c0.Command("c01", "").Default() @@ -141,6 +148,7 @@ func TestDefaultSubcommandEOL(t *testing.T) { } func TestDefaultSubcommandWithArg(t *testing.T) { + t.Parallel() app := newTestApp() c0 := app.Command("c0", "").Default() c01 := c0.Command("c01", "").Default() @@ -155,6 +163,7 @@ func TestDefaultSubcommandWithArg(t *testing.T) { } func TestDefaultSubcommandWithFlags(t *testing.T) { + t.Parallel() app := newTestApp() c0 := app.Command("c0", "").Default() _ = c0.Flag("f0", "").Int() @@ -169,6 +178,7 @@ func TestDefaultSubcommandWithFlags(t *testing.T) { } func TestMultipleDefaultCommands(t *testing.T) { + t.Parallel() app := newTestApp() app.Command("c0", "").Default() app.Command("c1", "").Default() @@ -177,6 +187,7 @@ func TestMultipleDefaultCommands(t *testing.T) { } func TestAliasedCommand(t *testing.T) { + t.Parallel() app := newTestApp() app.Command("one", "").Alias("two") selected, _ := app.Parse([]string{"one"}) @@ -188,6 +199,7 @@ func TestAliasedCommand(t *testing.T) { } func TestDuplicateAlias(t *testing.T) { + t.Parallel() app := newTestApp() app.Command("one", "") app.Command("two", "").Alias("one") @@ -196,6 +208,7 @@ func TestDuplicateAlias(t *testing.T) { } func TestFlagCompletion(t *testing.T) { + t.Parallel() app := newTestApp() app.Command("one", "") two := app.Command("two", "") @@ -204,12 +217,12 @@ func TestFlagCompletion(t *testing.T) { two.Flag("flag-3", "") cases := []struct { - target cmdMixin flagName string flagValue string + target cmdMixin + expectedFlags []string expectedFlagMatch bool expectedOptionMatch bool - expectedFlags []string }{ { // Test top level flags @@ -275,10 +288,10 @@ func TestFlagCompletion(t *testing.T) { assert.Equal(t, c.expectedFlagMatch, flagMatch, "Test case %d: expectedFlagMatch != flagMatch", i+1) assert.Equal(t, c.expectedOptionMatch, optionMatch, "Test case %d: expectedOptionMatch != optionMatch", i+1) } - } func TestCmdCompletion(t *testing.T) { + t.Parallel() app := newTestApp() app.Command("one", "") two := app.Command("two", "") @@ -290,6 +303,7 @@ func TestCmdCompletion(t *testing.T) { } func TestHiddenCmdCompletion(t *testing.T) { + t.Parallel() app := newTestApp() // top level visible & hidden cmds, with no sub-cmds @@ -317,6 +331,7 @@ func TestHiddenCmdCompletion(t *testing.T) { } func TestDefaultCmdCompletion(t *testing.T) { + t.Parallel() app := newTestApp() cmd1 := app.Command("cmd1", "") @@ -375,6 +390,7 @@ func TestDefaultCmdCompletion(t *testing.T) { } func TestPartialCmdCompletion(t *testing.T) { + t.Parallel() app := newTestApp() cmd1 := app.Command("cmd1", "") @@ -388,7 +404,6 @@ func TestPartialCmdCompletion(t *testing.T) { cmd4.Arg("cmd4-arg2", "").HintOptions("cmd4-arg2").String() cmd4.Arg("cmd4-arg3", "").HintOptions("cmd4-arg3").String() - // partial matches assert.Equal(t, []string{"cmd1-arg1-opt1", "cmd1-arg1-opt2", "cmd1-arg1-opt3"}, complete(t, app, "cmd1", "cmd1-arg1-opt")) assert.Equal(t, []string{"cmd2-123456", "cmd2-123789", "cmd2-456789"}, complete(t, app, "cmd2", "cmd2-")) diff --git a/completions_test.go b/completions_test.go index 7da9c06..06eea77 100644 --- a/completions_test.go +++ b/completions_test.go @@ -7,6 +7,7 @@ import ( ) func TestResolveWithBuiltin(t *testing.T) { + t.Parallel() a := completionsMixin{} hintAction1 := func() []string { @@ -23,6 +24,7 @@ func TestResolveWithBuiltin(t *testing.T) { } func TestResolveWithUser(t *testing.T) { + t.Parallel() a := completionsMixin{} hintAction1 := func() []string { return []string{"opt1", "opt2"} @@ -38,6 +40,7 @@ func TestResolveWithUser(t *testing.T) { } func TestResolveWithCombination(t *testing.T) { + t.Parallel() a := completionsMixin{} builtin := func() []string { return []string{"opt1", "opt2"} @@ -55,6 +58,7 @@ func TestResolveWithCombination(t *testing.T) { } func TestAddHintAction(t *testing.T) { + t.Parallel() a := completionsMixin{} hintFunc := func() []string { return []string{"opt1", "opt2"} @@ -66,6 +70,7 @@ func TestAddHintAction(t *testing.T) { } func TestAddHintActionBuiltin(t *testing.T) { + t.Parallel() a := completionsMixin{} hintFunc := func() []string { return []string{"opt1", "opt2"} diff --git a/flags_test.go b/flags_test.go index 1d28e14..f08b3bf 100644 --- a/flags_test.go +++ b/flags_test.go @@ -1,15 +1,14 @@ package kingpin import ( - "io/ioutil" - "os" + "io" + "testing" "github.com/stretchr/testify/assert" - - "testing" ) func TestBool(t *testing.T) { + t.Parallel() app := newTestApp() b := app.Flag("b", "").Bool() _, err := app.Parse([]string{"--b"}) @@ -18,6 +17,7 @@ func TestBool(t *testing.T) { } func TestNoBool(t *testing.T) { + t.Parallel() fg := newFlagGroup() f := fg.Flag("b", "").Default("true") b := f.Bool() @@ -29,6 +29,7 @@ func TestNoBool(t *testing.T) { } func TestNegateNonBool(t *testing.T) { + t.Parallel() fg := newFlagGroup() f := fg.Flag("b", "") f.Int() @@ -39,6 +40,7 @@ func TestNegateNonBool(t *testing.T) { } func TestNegativePrefixLongFlag(t *testing.T) { + t.Parallel() fg := newFlagGroup() f := fg.Flag("no-comment", "") b := f.Bool() @@ -50,6 +52,7 @@ func TestNegativePrefixLongFlag(t *testing.T) { } func TestInvalidFlagDefaultCanBeOverridden(t *testing.T) { + t.Parallel() app := newTestApp() app.Flag("a", "").Default("invalid").Bool() _, err := app.Parse([]string{}) @@ -57,8 +60,9 @@ func TestInvalidFlagDefaultCanBeOverridden(t *testing.T) { } func TestRequiredFlag(t *testing.T) { + t.Parallel() app := newTestApp() - app.Version("0.0.0").Writer(ioutil.Discard) + app.Version("0.0.0").Writer(io.Discard) exits := 0 app.Terminate(func(int) { exits++ }) app.Flag("a", "").Required().Bool() @@ -71,6 +75,7 @@ func TestRequiredFlag(t *testing.T) { } func TestShortFlag(t *testing.T) { + t.Parallel() app := newTestApp() f := app.Flag("long", "").Short('s').Bool() _, err := app.Parse([]string{"-s"}) @@ -79,6 +84,7 @@ func TestShortFlag(t *testing.T) { } func TestUnicodeShortFlag(t *testing.T) { + t.Parallel() app := newTestApp() f := app.Flag("aaa", "").Short('ä').Bool() _, err := app.Parse([]string{"-ä"}) @@ -87,6 +93,7 @@ func TestUnicodeShortFlag(t *testing.T) { } func TestCombinedShortFlags(t *testing.T) { + t.Parallel() app := newTestApp() a := app.Flag("short0", "").Short('0').Bool() b := app.Flag("short1", "").Short('1').Bool() @@ -99,6 +106,7 @@ func TestCombinedShortFlags(t *testing.T) { } func TestCombinedUnicodeShortFlags(t *testing.T) { + t.Parallel() app := newTestApp() a := app.Flag("short0", "").Short('0').Bool() b := app.Flag("short1", "").Short('1').Bool() @@ -113,6 +121,7 @@ func TestCombinedUnicodeShortFlags(t *testing.T) { } func TestCombinedShortFlagArg(t *testing.T) { + t.Parallel() a := newTestApp() n := a.Flag("short", "").Short('s').Int() _, err := a.Parse([]string{"-s10"}) @@ -121,6 +130,7 @@ func TestCombinedShortFlagArg(t *testing.T) { } func TestCombinedUnicodeShortFlagArg(t *testing.T) { + t.Parallel() app := newTestApp() a := app.Flag("short", "").Short('ä').Int() _, err := app.Parse([]string{"-ä10"}) @@ -129,6 +139,7 @@ func TestCombinedUnicodeShortFlagArg(t *testing.T) { } func TestCombinedUnicodeShortFlagUnicodeArg(t *testing.T) { + t.Parallel() app := newTestApp() a := app.Flag("short", "").Short('ä').String() _, err := app.Parse([]string{"-äöö"}) @@ -137,11 +148,13 @@ func TestCombinedUnicodeShortFlagUnicodeArg(t *testing.T) { } func TestEmptyShortFlagIsAnError(t *testing.T) { + t.Parallel() _, err := newTestApp().Parse([]string{"-"}) assert.Error(t, err) } func TestRequiredWithEnvarMissingErrors(t *testing.T) { + t.Parallel() app := newTestApp() app.Flag("t", "").Envar("TEST_ENVAR").Required().Int() _, err := app.Parse([]string{}) @@ -149,7 +162,7 @@ func TestRequiredWithEnvarMissingErrors(t *testing.T) { } func TestRequiredWithEnvar(t *testing.T) { - os.Setenv("TEST_ENVAR", "123") + t.Setenv("TEST_ENVAR", "123") app := newTestApp() flag := app.Flag("t", "").Envar("TEST_ENVAR").Required().Int() _, err := app.Parse([]string{}) @@ -158,7 +171,7 @@ func TestRequiredWithEnvar(t *testing.T) { } func TestSubcommandFlagRequiredWithEnvar(t *testing.T) { - os.Setenv("TEST_ENVAR", "123") + t.Setenv("TEST_ENVAR", "123") app := newTestApp() cmd := app.Command("command", "") flag := cmd.Flag("t", "").Envar("TEST_ENVAR").Required().Int() @@ -168,6 +181,7 @@ func TestSubcommandFlagRequiredWithEnvar(t *testing.T) { } func TestRegexp(t *testing.T) { + t.Parallel() app := newTestApp() flag := app.Flag("reg", "").Regexp() _, err := app.Parse([]string{"--reg", "^abc$"}) @@ -179,6 +193,7 @@ func TestRegexp(t *testing.T) { } func TestDuplicateShortFlag(t *testing.T) { + t.Parallel() app := newTestApp() app.Flag("a", "").Short('a').String() app.Flag("b", "").Short('a').String() @@ -187,6 +202,7 @@ func TestDuplicateShortFlag(t *testing.T) { } func TestDuplicateLongFlag(t *testing.T) { + t.Parallel() app := newTestApp() app.Flag("a", "").String() app.Flag("a", "").String() @@ -195,6 +211,7 @@ func TestDuplicateLongFlag(t *testing.T) { } func TestGetFlagAndOverrideDefault(t *testing.T) { + t.Parallel() app := newTestApp() a := app.Flag("a", "").Default("default").String() _, err := app.Parse([]string{}) @@ -207,7 +224,7 @@ func TestGetFlagAndOverrideDefault(t *testing.T) { } func TestEnvarOverrideDefault(t *testing.T) { - os.Setenv("TEST_ENVAR", "123") + t.Setenv("TEST_ENVAR", "123") app := newTestApp() flag := app.Flag("t", "").Default("default").Envar("TEST_ENVAR").String() _, err := app.Parse([]string{}) @@ -216,6 +233,7 @@ func TestEnvarOverrideDefault(t *testing.T) { } func TestFlagMultipleValuesDefault(t *testing.T) { + t.Parallel() app := newTestApp() a := app.Flag("a", "").Default("default1", "default2").Strings() _, err := app.Parse([]string{}) @@ -224,6 +242,7 @@ func TestFlagMultipleValuesDefault(t *testing.T) { } func TestFlagMultipleValuesDefaultNonRepeatable(t *testing.T) { + t.Parallel() c := newTestApp() c.Flag("foo", "foo").Default("a", "b").String() _, err := c.Parse([]string{}) @@ -233,7 +252,7 @@ func TestFlagMultipleValuesDefaultNonRepeatable(t *testing.T) { func TestFlagMultipleValuesDefaultEnvarUnix(t *testing.T) { app := newTestApp() a := app.Flag("a", "").Envar("TEST_MULTIPLE_VALUES").Strings() - os.Setenv("TEST_MULTIPLE_VALUES", "123\n456\n") + t.Setenv("TEST_MULTIPLE_VALUES", "123\n456\n") _, err := app.Parse([]string{}) assert.NoError(t, err) assert.Equal(t, []string{"123", "456"}, *a) @@ -242,7 +261,7 @@ func TestFlagMultipleValuesDefaultEnvarUnix(t *testing.T) { func TestFlagMultipleValuesDefaultEnvarWindows(t *testing.T) { app := newTestApp() a := app.Flag("a", "").Envar("TEST_MULTIPLE_VALUES").Strings() - os.Setenv("TEST_MULTIPLE_VALUES", "123\r\n456\r\n") + t.Setenv("TEST_MULTIPLE_VALUES", "123\r\n456\r\n") _, err := app.Parse([]string{}) assert.NoError(t, err) assert.Equal(t, []string{"123", "456"}, *a) @@ -251,13 +270,14 @@ func TestFlagMultipleValuesDefaultEnvarWindows(t *testing.T) { func TestFlagMultipleValuesDefaultEnvarNonRepeatable(t *testing.T) { c := newTestApp() a := c.Flag("foo", "foo").Envar("TEST_MULTIPLE_VALUES_NON_REPEATABLE").String() - os.Setenv("TEST_MULTIPLE_VALUES_NON_REPEATABLE", "123\n456") + t.Setenv("TEST_MULTIPLE_VALUES_NON_REPEATABLE", "123\n456") _, err := c.Parse([]string{}) assert.NoError(t, err) assert.Equal(t, "123\n456", *a) } func TestFlagHintAction(t *testing.T) { + t.Parallel() c := newTestApp() action := func() []string { @@ -270,6 +290,7 @@ func TestFlagHintAction(t *testing.T) { } func TestFlagHintOptions(t *testing.T) { + t.Parallel() c := newTestApp() a := c.Flag("foo", "foo").HintOptions("opt1", "opt2") @@ -278,6 +299,7 @@ func TestFlagHintOptions(t *testing.T) { } func TestFlagEnumVar(t *testing.T) { + t.Parallel() c := newTestApp() var bar string @@ -294,13 +316,16 @@ func TestFlagEnumVar(t *testing.T) { } func TestMultiHintOptions(t *testing.T) { + t.Parallel() c := newTestApp() a := c.Flag("foo", "foo").HintOptions("opt1").HintOptions("opt2") args := a.resolveCompletions() assert.Equal(t, []string{"opt1", "opt2"}, args) } + func TestMultiHintActions(t *testing.T) { + t.Parallel() c := newTestApp() a := c.Flag("foo", "foo"). @@ -315,6 +340,7 @@ func TestMultiHintActions(t *testing.T) { } func TestCombinationHintActionsOptions(t *testing.T) { + t.Parallel() c := newTestApp() a := c.Flag("foo", "foo").HintAction(func() []string { @@ -325,6 +351,7 @@ func TestCombinationHintActionsOptions(t *testing.T) { } func TestCombinationEnumActions(t *testing.T) { + t.Parallel() c := newTestApp() var foo string @@ -349,6 +376,7 @@ func TestCombinationEnumActions(t *testing.T) { } func TestCombinationEnumOptions(t *testing.T) { + t.Parallel() c := newTestApp() var foo string @@ -364,10 +392,10 @@ func TestCombinationEnumOptions(t *testing.T) { args = b.resolveCompletions() assert.Equal(t, []string{"opt5", "opt6"}, args) - } func TestIsSetByUser(t *testing.T) { + t.Parallel() app := newTestApp() var isSet bool b := app.Flag("b", "").IsSetByUser(&isSet).Bool() diff --git a/parser_test.go b/parser_test.go index 43dfde9..c72cc2c 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,7 +1,6 @@ package kingpin import ( - "io/ioutil" "os" "testing" @@ -9,7 +8,8 @@ import ( ) func TestParserExpandFromFile(t *testing.T) { - f, err := ioutil.TempFile("", "") + t.Parallel() + f, err := os.CreateTemp("", "") assert.NoError(t, err) defer os.Remove(f.Name()) f.WriteString("hello\nworld\n") @@ -26,7 +26,8 @@ func TestParserExpandFromFile(t *testing.T) { } func TestParserExpandFromFileLeadingArg(t *testing.T) { - f, err := ioutil.TempFile("", "") + t.Parallel() + f, err := os.CreateTemp("", "") assert.NoError(t, err) defer os.Remove(f.Name()) f.WriteString("hello\nworld\n") @@ -45,7 +46,8 @@ func TestParserExpandFromFileLeadingArg(t *testing.T) { } func TestParserExpandFromFileTrailingArg(t *testing.T) { - f, err := ioutil.TempFile("", "") + t.Parallel() + f, err := os.CreateTemp("", "") assert.NoError(t, err) defer os.Remove(f.Name()) f.WriteString("hello\nworld\n") @@ -64,7 +66,8 @@ func TestParserExpandFromFileTrailingArg(t *testing.T) { } func TestParserExpandFromFileMultipleSurroundingArgs(t *testing.T) { - f, err := ioutil.TempFile("", "") + t.Parallel() + f, err := os.CreateTemp("", "") assert.NoError(t, err) defer os.Remove(f.Name()) f.WriteString("hello\nworld\n") @@ -85,7 +88,8 @@ func TestParserExpandFromFileMultipleSurroundingArgs(t *testing.T) { } func TestParserExpandFromFileMultipleFlags(t *testing.T) { - f, err := ioutil.TempFile("", "") + t.Parallel() + f, err := os.CreateTemp("", "") assert.NoError(t, err) defer os.Remove(f.Name()) f.WriteString("--flag1=f1\n--flag2=f2\n") @@ -106,6 +110,7 @@ func TestParserExpandFromFileMultipleFlags(t *testing.T) { } func TestParseContextPush(t *testing.T) { + t.Parallel() app := New("test", "") app.Command("foo", "").Command("bar", "") c := tokenize([]string{"foo", "bar"}, false) diff --git a/usage_test.go b/usage_test.go index a438748..725e987 100644 --- a/usage_test.go +++ b/usage_test.go @@ -10,6 +10,7 @@ import ( ) func TestFormatTwoColumns(t *testing.T) { + t.Parallel() buf := bytes.NewBuffer(nil) formatTwoColumns(buf, 2, 2, 20, [][2]string{ {"--hello", "Hello world help with something that is cool."}, @@ -25,9 +26,11 @@ func TestFormatTwoColumns(t *testing.T) { } func TestFormatTwoColumnsWide(t *testing.T) { + t.Parallel() samples := [][2]string{ {strings.Repeat("x", 29), "29 chars"}, - {strings.Repeat("x", 30), "30 chars"}} + {strings.Repeat("x", 30), "30 chars"}, + } buf := bytes.NewBuffer(nil) formatTwoColumns(buf, 0, 0, 200, samples) expected := `xxxxxxxxxxxxxxxxxxxxxxxxxxxxx29 chars @@ -38,6 +41,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx } func TestHiddenCommand(t *testing.T) { + t.Parallel() templates := []struct{ name, template string }{ {"default", DefaultUsageTemplate}, {"Compact", CompactUsageTemplate}, @@ -66,6 +70,7 @@ func TestHiddenCommand(t *testing.T) { } func TestUsageFuncs(t *testing.T) { + t.Parallel() var buf bytes.Buffer a := New("test", "Test").Writer(&buf).Terminate(nil) tpl := `{{ add 2 1 }}` @@ -79,6 +84,7 @@ func TestUsageFuncs(t *testing.T) { } func TestCmdClause_HelpLong(t *testing.T) { + t.Parallel() var buf bytes.Buffer tpl := `{{define "FormatUsage"}}{{.HelpLong}}{{end -}} {{template "FormatUsage" .Context.SelectedCommand}}` @@ -93,6 +99,7 @@ func TestCmdClause_HelpLong(t *testing.T) { } func TestArgEnvVar(t *testing.T) { + t.Parallel() var buf bytes.Buffer a := New("test", "Test").Writer(&buf).Terminate(nil) diff --git a/values_test.go b/values_test.go index e16ee2a..043b37d 100644 --- a/values_test.go +++ b/values_test.go @@ -2,13 +2,13 @@ package kingpin import ( "net" + "testing" "github.com/stretchr/testify/assert" - - "testing" ) func TestAccumulatorStrings(t *testing.T) { + t.Parallel() target := []string{} acc := newAccumulator(&target, func(v interface{}) Value { return newStringValue(v.(*string)) }) acc.Set("a") @@ -18,6 +18,7 @@ func TestAccumulatorStrings(t *testing.T) { } func TestStrings(t *testing.T) { + t.Parallel() app := New("", "") app.Arg("a", "").Required().String() app.Arg("b", "").Required().String() @@ -27,6 +28,7 @@ func TestStrings(t *testing.T) { } func TestEnum(t *testing.T) { + t.Parallel() app := New("", "") a := app.Arg("a", "").Enum("one", "two", "three") _, err := app.Parse([]string{"moo"}) @@ -37,6 +39,7 @@ func TestEnum(t *testing.T) { } func TestEnumVar(t *testing.T) { + t.Parallel() app := New("", "") var a string app.Arg("a", "").EnumVar(&a, "one", "two", "three") @@ -48,6 +51,7 @@ func TestEnumVar(t *testing.T) { } func TestCounter(t *testing.T) { + t.Parallel() app := New("", "") c := app.Flag("f", "").Counter() _, err := app.Parse([]string{"--f", "--f", "--f"}) @@ -56,6 +60,7 @@ func TestCounter(t *testing.T) { } func TestIPv4Addr(t *testing.T) { + t.Parallel() app := newTestApp() flag := app.Flag("addr", "").ResolvedIP() _, err := app.Parse([]string{"--addr", net.IPv4(1, 2, 3, 4).String()}) @@ -65,6 +70,7 @@ func TestIPv4Addr(t *testing.T) { } func TestInvalidIPv4Addr(t *testing.T) { + t.Parallel() app := newTestApp() app.Flag("addr", "").ResolvedIP() _, err := app.Parse([]string{"--addr", "1.2.3.256"}) @@ -72,6 +78,7 @@ func TestInvalidIPv4Addr(t *testing.T) { } func TestIPv6Addr(t *testing.T) { + t.Parallel() app := newTestApp() flag := app.Flag("addr", "").ResolvedIP() _, err := app.Parse([]string{"--addr", net.IPv6interfacelocalallnodes.String()}) @@ -81,6 +88,7 @@ func TestIPv6Addr(t *testing.T) { } func TestHexBytes(t *testing.T) { + t.Parallel() app := newTestApp() actual := app.Arg("bytes", "").HexBytes() _, err := app.Parse([]string{"01020aff"}) @@ -89,6 +97,7 @@ func TestHexBytes(t *testing.T) { } func TestSetValueDoesNotReset(t *testing.T) { + t.Parallel() app := newTestApp() mapping := map[string]string{ "key": "value",