Skip to content

Commit

Permalink
add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Nov 13, 2024
1 parent e053fda commit 457cbbe
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,12 +840,14 @@ func TestCommand_OptionsWithSharedValue(t *testing.T) {
{
Name: "url",
Flag: "url",
Env: "URL",
Default: def,
Value: serpent.StringOf(&got),
},
{
Name: "alt-url",
Flag: "alt-url",
Env: "ALT_URL",
Default: altDef,
Value: serpent.StringOf(&got),
},
Expand Down Expand Up @@ -873,6 +875,30 @@ func TestCommand_OptionsWithSharedValue(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "hup", got)

// Both flags are given, last wins.
err = makeCmd("def.com", "").Invoke("--url", "sup", "--alt-url", "hup").Run()
require.NoError(t, err)
require.Equal(t, "hup", got)

// Both flags are given, last wins #2.
err = makeCmd("", "def.com").Invoke("--alt-url", "hup", "--url", "sup").Run()
require.NoError(t, err)
require.Equal(t, "sup", got)

// Both flags are given, option type priority wins.
inv := makeCmd("def.com", "").Invoke("--alt-url", "hup")
inv.Environ.Set("URL", "sup")
err = inv.Run()
require.NoError(t, err)
require.Equal(t, "hup", got)

// Both flags are given, option type priority wins #2.
inv = makeCmd("", "def.com").Invoke("--url", "sup")
inv.Environ.Set("ALT_URL", "hup")
err = inv.Run()
require.NoError(t, err)
require.Equal(t, "sup", got)

// Catch invalid configuration.
err = makeCmd("def.com", "alt-def.com").Invoke().Run()
require.Error(t, err, "default values are different")
Expand Down

0 comments on commit 457cbbe

Please sign in to comment.