Repository navigation
Expand file tree
/
Copy pathflag_mutex_test.go
More file actions
231 lines (206 loc) · 5.44 KB
/
Copy pathflag_mutex_test.go
File metadata and controls
231 lines (206 loc) · 5.44 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package cli
import (
"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"),
},
&BoolFlag{
Name: "q",
},
},
},
},
},
}
}
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 t",
},
{
name: "set both flags second member",
args: []string{"--i", "11", "--q"},
errStr: "option i cannot be set along with option q",
},
{
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 t",
required: true,
},
{
name: "required both set second member",
args: []string{"--i", "11", "--q"},
errStr: "option i cannot be set along with option q",
required: true,
},
{
name: "required both set second member both groups",
args: []string{"--s", "value", "--q"},
errStr: "option s cannot be set along with option q",
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):
assert.Contains(t, err.Error(), test.errStr)
default:
t.Errorf("got invalid error type %T", err)
}
})
}
}
func TestMutuallyExclusiveFlags_PropagateStringer(t *testing.T) {
customStringer := func(f Flag) string {
return "custom:" + f.Names()[0]
}
grp := MutuallyExclusiveFlags{
Stringer: customStringer,
Flags: [][]Flag{
{
&StringFlag{Name: "foo"},
&BoolWithInverseFlag{Name: "bar"},
},
{
&Int64Flag{Name: "baz"},
},
},
}
grp.propagateStringer()
assert.Equal(t, "custom:foo", grp.Flags[0][0].String())
assert.Contains(t, grp.Flags[0][1].String(), "custom:bar")
assert.Equal(t, "custom:baz", grp.Flags[1][0].String())
}
func TestMutuallyExclusiveFlags_PropagateStringerNil(t *testing.T) {
grp := MutuallyExclusiveFlags{
Flags: [][]Flag{
{
&StringFlag{Name: "foo"},
},
},
}
// should not panic and should leave flags using the default FlagStringer
grp.propagateStringer()
assert.NotEqual(t, "", grp.Flags[0][0].String())
}
func TestMutuallyExclusiveFlags_PropagateStringerNilResetsToDefault(t *testing.T) {
f := &StringFlag{Name: "foo"}
defaultOut := f.String()
customStringer := func(f Flag) string {
return "custom:" + f.Names()[0]
}
grp := MutuallyExclusiveFlags{
Stringer: customStringer,
Flags: [][]Flag{{f}},
}
grp.propagateStringer()
assert.Equal(t, "custom:foo", f.String())
// Resetting the stringer to nil via SetStringer must restore the
// default FlagStringer-based behavior.
grp.Stringer = nil
f.SetStringer(nil)
assert.Equal(t, defaultOut, f.String())
}
// nonStringerSettingFlag is a minimal Flag implementation that deliberately
// does not implement StringerSetter, so that propagateStringer must skip it
// via its type assertion rather than panicking or otherwise misbehaving.
type nonStringerSettingFlag struct {
name string
}
func (f *nonStringerSettingFlag) String() string { return "plain:" + f.name }
func (f *nonStringerSettingFlag) Get() any { return nil }
func (f *nonStringerSettingFlag) PreParse() error { return nil }
func (f *nonStringerSettingFlag) PostParse() error { return nil }
func (f *nonStringerSettingFlag) Set(string, string) error { return nil }
func (f *nonStringerSettingFlag) Names() []string { return []string{f.name} }
func (f *nonStringerSettingFlag) IsSet() bool { return false }
func TestMutuallyExclusiveFlags_PropagateStringerSkipsNonImplementor(t *testing.T) {
customStringer := func(f Flag) string {
return "custom:" + f.Names()[0]
}
plain := &nonStringerSettingFlag{name: "plain"}
grp := MutuallyExclusiveFlags{
Stringer: customStringer,
Flags: [][]Flag{
{plain},
},
}
// should not panic
grp.propagateStringer()
// plain does not implement StringerSetter, so it must keep its own
// String() implementation rather than picking up the custom stringer.
assert.Equal(t, "plain:plain", grp.Flags[0][0].String())
}