1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package cli
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func newCommand() *Command {
return &Command{
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
{
Flags: [][]Flag{
{
&Int64Flag{
Name: "i",
},
&StringFlag{
Name: "s",
Sources: EnvVars("S_VAR"),
},
&BoolWithInverseFlag{
Name: "b",
},
},
{
&Int64Flag{
Name: "t",
Aliases: []string{"ai"},
Sources: EnvVars("T_VAR"),
},
},
},
},
},
}
}
func TestFlagMutuallyExclusiveFlags(t *testing.T) {
tests := []struct {
name string
args []string
errStr string
required bool
envs map[string]string
}{
{
name: "simple",
},
{
name: "set one flag",
args: []string{"--i", "10"},
},
{
name: "set both flags",
args: []string{"--i", "11", "--ai", "12"},
errStr: "option i cannot be set along with option ai",
},
{
name: "required none set",
required: true,
errStr: "one of these flags needs to be provided",
},
{
name: "required one set",
args: []string{"--i", "10"},
required: true,
},
{
name: "required both set",
args: []string{"--i", "11", "--ai", "12"},
errStr: "option i cannot be set along with option ai",
required: true,
},
{
name: "set env var",
required: true,
envs: map[string]string{
"S_VAR": "some",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.envs != nil {
for k, v := range test.envs {
t.Setenv(k, v)
}
}
cmd := newCommand()
cmd.MutuallyExclusiveFlags[0].Required = test.required
err := cmd.Run(buildTestContext(t), append([]string{"foo"}, test.args...))
if test.errStr == "" {
assert.NoError(t, err)
return
}
if err == nil {
t.Error("Expected mutual exclusion error")
return
}
switch err.(type) {
case (*mutuallyExclusiveGroup), (*mutuallyExclusiveGroupRequiredFlag):
if !strings.Contains(err.Error(), test.errStr) {
t.Logf("Invalid error string %v", err)
}
default:
t.Errorf("got invalid error type %T", err)
}
})
}
}
|