MCPcopy
hub / github.com/urfave/cli / TestFlagMutuallyExclusiveFlags

Function TestFlagMutuallyExclusiveFlags

flag_mutex_test.go:40–115  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

38}
39
40func TestFlagMutuallyExclusiveFlags(t *testing.T) {
41 tests := []struct {
42 name string
43 args []string
44 errStr string
45 required bool
46 envs map[string]string
47 }{
48 {
49 name: "simple",
50 },
51 {
52 name: "set one flag",
53 args: []string{"--i", "10"},
54 },
55 {
56 name: "set both flags",
57 args: []string{"--i", "11", "--ai", "12"},
58 errStr: "option i cannot be set along with option ai",
59 },
60 {
61 name: "required none set",
62 required: true,
63 errStr: "one of these flags needs to be provided",
64 },
65 {
66 name: "required one set",
67 args: []string{"--i", "10"},
68 required: true,
69 },
70 {
71 name: "required both set",
72 args: []string{"--i", "11", "--ai", "12"},
73 errStr: "option i cannot be set along with option ai",
74 required: true,
75 },
76 {
77 name: "set env var",
78 required: true,
79 envs: map[string]string{
80 "S_VAR": "some",
81 },
82 },
83 }
84
85 for _, test := range tests {
86 t.Run(test.name, func(t *testing.T) {
87 if test.envs != nil {
88 for k, v := range test.envs {
89 t.Setenv(k, v)
90 }
91 }
92 cmd := newCommand()
93 cmd.MutuallyExclusiveFlags[0].Required = test.required
94
95 err := cmd.Run(buildTestContext(t), append([]string{"foo"}, test.args...))
96 if test.errStr == "" {
97 assert.NoError(t, err)

Callers

nothing calls this directly

Calls 4

newCommandFunction · 0.85
buildTestContextFunction · 0.85
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected