tests: simplify, cleanup lint, use t.Helper, use t.Setenv

This commit is contained in:
Bruno Reis 2023-08-06 16:10:45 -07:00
parent 134cfaeba0
commit 473eb2b847
2 changed files with 13 additions and 9 deletions

View File

@ -199,13 +199,13 @@ func Example_helpPlaceholder() {
MustParse(&args) MustParse(&args)
// output: // output:
//
// Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]] // Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]]
//
// Positional arguments: // Positional arguments:
// SRC // SRC
// DST // DST
//
// Options: // Options:
// --optimize LEVEL, -O LEVEL // --optimize LEVEL, -O LEVEL
// optimization level // optimization level
@ -501,7 +501,9 @@ func Example_envVarOnly() {
os.Args = split("./example") os.Args = split("./example")
_ = os.Setenv("AUTH_KEY", "my_key") _ = os.Setenv("AUTH_KEY", "my_key")
defer os.Unsetenv("AUTH_KEY") defer func() {
_ = os.Unsetenv("AUTH_KEY")
}()
var args struct { var args struct {
AuthKey string `arg:"--,env:AUTH_KEY"` AuthKey string `arg:"--,env:AUTH_KEY"`

View File

@ -97,12 +97,12 @@ Environment variables:
type MyEnum int type MyEnum int
func (n *MyEnum) UnmarshalText(b []byte) error { func (n *MyEnum) UnmarshalText(_ []byte) error {
return nil return nil
} }
func (n *MyEnum) MarshalText() ([]byte, error) { 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) { func TestUsageWithDefaults(t *testing.T) {
@ -142,7 +142,7 @@ func TestUsageCannotMarshalToString(t *testing.T) {
v := MyEnum(42) v := MyEnum(42)
args.Name = &v args.Name = &v
_, err := NewParser(Config{Program: "example"}, &args) _, 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) { func TestUsageLongPositionalWithHelp_legacyForm(t *testing.T) {
@ -455,7 +455,8 @@ Global options:
assert.Equal(t, expectedHelp[1:], help.String()) assert.Equal(t, expectedHelp[1:], help.String())
var help2 bytes.Buffer 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()) assert.Equal(t, expectedHelp[1:], help2.String())
var usage bytes.Buffer var usage bytes.Buffer
@ -463,7 +464,8 @@ Global options:
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
var usage2 bytes.Buffer 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())) assert.Equal(t, expectedUsage, strings.TrimSpace(usage2.String()))
} }