Skip to content

Commit 2335016

Browse files
committed
add the tests
1 parent cb0fc21 commit 2335016

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

command_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5168,6 +5168,77 @@ func TestJSONExportCommand(t *testing.T) {
51685168
assert.JSONEq(t, expected, string(out))
51695169
}
51705170

5171+
func TestCommand_ExclusiveFlags(t *testing.T) {
5172+
var (
5173+
foo1 = &StringFlag{
5174+
Name: "foo1",
5175+
}
5176+
foo2 = &StringFlag{
5177+
Name: "foo2",
5178+
}
5179+
)
5180+
cmd := &Command{
5181+
Name: "bar",
5182+
Flags: []Flag{
5183+
foo1,
5184+
foo2,
5185+
},
5186+
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
5187+
{
5188+
Flags: [][]Flag{
5189+
{
5190+
foo1,
5191+
},
5192+
{
5193+
foo2,
5194+
},
5195+
},
5196+
},
5197+
},
5198+
}
5199+
5200+
err := cmd.Run(buildTestContext(t), []string{"bar", "--foo1", "var1", "--foo2", "var2"})
5201+
5202+
require.Equal(t, "option foo1 cannot be set along with option foo2", err.Error())
5203+
}
5204+
5205+
func TestCommand_ExclusiveFlagsWithOnUsageError(t *testing.T) {
5206+
var (
5207+
foo1 = &StringFlag{
5208+
Name: "foo1",
5209+
}
5210+
foo2 = &StringFlag{
5211+
Name: "foo2",
5212+
}
5213+
)
5214+
cmd := &Command{
5215+
Name: "bar",
5216+
Flags: []Flag{
5217+
foo1,
5218+
foo2,
5219+
},
5220+
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
5221+
{
5222+
Flags: [][]Flag{
5223+
{
5224+
foo1,
5225+
},
5226+
{
5227+
foo2,
5228+
},
5229+
},
5230+
},
5231+
},
5232+
OnUsageError: func(_ context.Context, _ *Command, _ error, _ bool) error {
5233+
return errors.New("my custom error")
5234+
},
5235+
}
5236+
5237+
err := cmd.Run(buildTestContext(t), []string{"bar", "--foo1", "v1", "--foo2", "v2"})
5238+
5239+
require.Equal(t, "my custom error", err.Error())
5240+
}
5241+
51715242
func TestCommand_ExclusiveFlagsWithAfter(t *testing.T) {
51725243
var called bool
51735244
cmd := &Command{

0 commit comments

Comments
 (0)