From 473eb2b847ce43926a5a9b5c1ea220dafc03cceb Mon Sep 17 00:00:00 2001 From: Bruno Reis Date: Sun, 6 Aug 2023 16:10:45 -0700 Subject: [PATCH] tests: simplify, cleanup lint, use t.Helper, use t.Setenv --- example_test.go | 10 ++++++---- usage_test.go | 12 +++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/example_test.go b/example_test.go index 4bd7632..47ab26c 100644 --- a/example_test.go +++ b/example_test.go @@ -199,13 +199,13 @@ func Example_helpPlaceholder() { MustParse(&args) // output: - + // // Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]] - + // // Positional arguments: // SRC // DST - + // // Options: // --optimize LEVEL, -O LEVEL // optimization level @@ -501,7 +501,9 @@ func Example_envVarOnly() { os.Args = split("./example") _ = os.Setenv("AUTH_KEY", "my_key") - defer os.Unsetenv("AUTH_KEY") + defer func() { + _ = os.Unsetenv("AUTH_KEY") + }() var args struct { AuthKey string `arg:"--,env:AUTH_KEY"` diff --git a/usage_test.go b/usage_test.go index 1a64ad4..f32aa36 100644 --- a/usage_test.go +++ b/usage_test.go @@ -97,12 +97,12 @@ Environment variables: type MyEnum int -func (n *MyEnum) UnmarshalText(b []byte) error { +func (n *MyEnum) UnmarshalText(_ []byte) error { return nil } func (n *MyEnum) MarshalText() ([]byte, error) { - return nil, errors.New("There was a problem") + return nil, errors.New("there was a problem") } func TestUsageWithDefaults(t *testing.T) { @@ -142,7 +142,7 @@ func TestUsageCannotMarshalToString(t *testing.T) { v := MyEnum(42) args.Name = &v _, err := NewParser(Config{Program: "example"}, &args) - assert.EqualError(t, err, `args.Name: error marshaling default value to string: There was a problem`) + assert.EqualError(t, err, `args.Name: error marshaling default value to string: there was a problem`) } func TestUsageLongPositionalWithHelp_legacyForm(t *testing.T) { @@ -455,7 +455,8 @@ Global options: assert.Equal(t, expectedHelp[1:], help.String()) var help2 bytes.Buffer - p.WriteHelpForSubcommand(&help2, "child", "nested") + err = p.WriteHelpForSubcommand(&help2, "child", "nested") + require.NoError(t, err) assert.Equal(t, expectedHelp[1:], help2.String()) var usage bytes.Buffer @@ -463,7 +464,8 @@ Global options: assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) var usage2 bytes.Buffer - p.WriteUsageForSubcommand(&usage2, "child", "nested") + err = p.WriteUsageForSubcommand(&usage2, "child", "nested") + require.NoError(t, err) assert.Equal(t, expectedUsage, strings.TrimSpace(usage2.String())) }