-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflagutil_test.go
504 lines (466 loc) · 11.5 KB
/
flagutil_test.go
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package flagutil
import (
"bytes"
"context"
"flag"
"fmt"
"math/rand"
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/gobwas/flagutil/parse"
)
func TestSetActual(t *testing.T) {
fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
fs.String("flag", "default", "usage")
f := fs.Lookup("flag")
f.Value = OverrideSet(f.Value, func(string) error {
t.Fatalf("unexpected value change")
return nil
})
mustNotBeActual(t, fs, "flag")
SetActual(fs, "flag")
mustBeActual(t, fs, "flag")
}
type fullParser struct {
Parser
Printer
}
func TestPrintUsage(t *testing.T) {
var buf bytes.Buffer
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.SetOutput(&buf)
var (
foo string
bar bool
baz int
)
fs.StringVar(&foo, "foo", "", "`custom` description here")
fs.BoolVar(&bar, "bar", bar, "description here")
fs.IntVar(&baz, "baz", baz, "description here")
PrintDefaults(context.Background(), fs,
WithParser(
&fullParser{
Parser: nil,
Printer: PrinterFunc(func(_ context.Context, fs parse.FlagSet) (func(*flag.Flag, func(string)), error) {
return func(f *flag.Flag, it func(string)) {
it("MUST-IGNORE-" + strings.ToUpper(f.Name))
}, nil
}),
},
WithStashPrefix("f"),
WithStashName("bar"),
),
WithParser(
&fullParser{
Parser: nil,
Printer: PrinterFunc(func(_ context.Context, fs parse.FlagSet) (func(*flag.Flag, func(string)), error) {
return func(f *flag.Flag, it func(string)) {
it("-" + string(f.Name[0]))
it("-" + f.Name)
}, nil
}),
},
WithStashName("foo"),
),
WithParser(&fullParser{
Parser: nil,
Printer: PrinterFunc(func(_ context.Context, fs parse.FlagSet) (func(*flag.Flag, func(string)), error) {
return func(f *flag.Flag, it func(string)) {
it("$" + strings.ToUpper(f.Name))
}, nil
}),
}),
WithStashRegexp(regexp.MustCompile(".*baz.*")),
)
exp := "" +
" $BAR, -b, -bar\n" +
" \tbool\n" +
" \tdescription here (default false)\n" +
"\n" +
" $FOO\n" + // -foo is ignored.
" \tcustom\n" +
" \tcustom description here (default \"\")\n" +
"\n"
if act := buf.String(); act != exp {
t.Error(cmp.Diff(exp, act))
}
}
func TestUnquoteUsage(t *testing.T) {
type expMode map[UnquoteUsageMode][2]string
for _, test := range []struct {
name string
flag *flag.Flag
modes expMode
}{
{
flag: &flag.Flag{
Usage: "foo `bar` baz",
},
modes: expMode{
UnquoteNothing: [2]string{
"", "foo `bar` baz",
},
UnquoteQuoted: [2]string{
"bar", "foo bar baz",
},
UnquoteClean: [2]string{
"", "foo baz",
},
},
},
{
flag: stringFlag("", "", "some kind of `hello` message"),
modes: expMode{
UnquoteDefault: [2]string{
"hello", "some kind of hello message",
},
UnquoteInferType: [2]string{
"string", "some kind of `hello` message",
},
UnquoteInferType | UnquoteClean: [2]string{
"string", "some kind of message",
},
},
},
{
flag: stringFlag("", "", "no quoted info"),
modes: expMode{
UnquoteQuoted: [2]string{
"", "no quoted info",
},
UnquoteInferType: [2]string{
"string", "no quoted info",
},
},
},
} {
t.Run(test.name, func(t *testing.T) {
for mode, exp := range test.modes {
t.Run(mode.String(), func(t *testing.T) {
actName, actUsage := unquoteUsage(mode, test.flag)
if expName := exp[0]; actName != expName {
t.Errorf("unexpected name:\n%s", cmp.Diff(expName, actName))
}
if expUsage := exp[1]; actUsage != expUsage {
t.Errorf("unexpected usage:\n%s", cmp.Diff(expUsage, actUsage))
}
})
}
})
}
}
func ExampleMerge() {
fs := flag.NewFlagSet("superset", flag.PanicOnError)
var (
s0 string
s1 string
)
// Setup flag in a superset.
fs.StringVar(&s0,
"foo", "42",
"some flag usage here",
)
// Now we need to setup same flag (probably from some different place).
// Setting it up again in a superset will cause error.
MergeInto(fs, func(sub *flag.FlagSet) {
// Notice that default value of this flag is different.
// However, it will be discarded in favour of default value from superset.
sub.StringVar(&s1,
"foo", "84",
"another flag usage here",
)
})
fmt.Println(s0)
fmt.Println(s1)
fs.Set("foo", "34")
fmt.Println(s0)
fmt.Println(s1)
flag := fs.Lookup("foo")
fmt.Println(flag.Usage)
// Output:
// 42
// 84
// 34
// 34
// some flag usage here / another flag usage here
}
func ExampleMerge_different_types() {
fs := flag.NewFlagSet("superset", flag.PanicOnError)
var (
s string
i int
)
fs.StringVar(&s,
"foo", "42",
"some flag usage here",
)
MergeInto(fs, func(sub *flag.FlagSet) {
sub.IntVar(&i,
"foo", 84,
"another flag usage here",
)
})
fs.Set("foo", "34")
fmt.Println(s)
fmt.Println(i)
// Output:
// 34
// 34
}
func TestMerge(t *testing.T) {
fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
var (
s0 string
s1 string
s2 string
)
fs.StringVar(&s0,
"foo", "bar",
"superset usage",
)
MergeInto(fs, func(fs *flag.FlagSet) {
fs.StringVar(&s1, "foo", "baz", "subset1 usage")
})
MergeInto(fs, func(fs *flag.FlagSet) {
fs.StringVar(&s2, "foo", "baq", "subset2 usage")
})
if s0 == s1 || s1 == s2 {
t.Fatalf("strings are equal: %q vs %q vs %q", s0, s1, s2)
}
if err := fs.Set("foo", "42"); err != nil {
t.Fatal(err)
}
if s0 != "42" {
t.Fatalf("unexpected value after Set(): %q", s0)
}
if s0 != s1 || s1 != s2 {
t.Fatalf("strings are not equal: %q vs %q vs %q", s0, s1, s2)
}
f := fs.Lookup("foo")
if s := f.Value.String(); s != s0 {
t.Fatalf("String() is %q; want %q", s, s0)
}
if act, exp := f.Usage, "superset usage / subset1 usage / subset2 usage"; act != exp {
t.Fatalf("unexpected usage: %q; want %q", act, exp)
}
}
func TestCombineSets(t *testing.T) {
var (
nameInBoth = "both"
nameInFirst = "first"
nameInSecond = "second"
nameUnknown = "whoa"
)
var (
fs0 = flag.NewFlagSet("FlagSet#0", flag.ContinueOnError)
fs1 = flag.NewFlagSet("FlagSet#1", flag.ContinueOnError)
)
fs0.String(nameInFirst, "first-default", "")
fs0.String(nameInBoth, "both-default-0", "")
fs1.String(nameInBoth, "both-default-1", "")
fs1.String(nameInSecond, "second-default", "")
fs := CombineSets(fs0, fs1)
mustNotBeDefined(t, fs, nameUnknown)
mustBeEqualTo(t, fs, nameInFirst, "first-default")
mustBeEqualTo(t, fs, nameInSecond, "second-default")
mustBeEqualTo(t, fs, nameInBoth, "")
mustNotSet(t, fs, nameUnknown, "want error")
mustSet(t, fs, nameInFirst, "first")
mustBeEqualTo(t, fs, nameInFirst, "first")
mustBeEqualTo(t, fs0, nameInFirst, "first")
mustSet(t, fs, nameInSecond, "second")
mustBeEqualTo(t, fs, nameInSecond, "second")
mustBeEqualTo(t, fs1, nameInSecond, "second")
mustSet(t, fs, nameInBoth, "both")
mustBeEqualTo(t, fs, nameInBoth, "both")
mustBeEqualTo(t, fs0, nameInBoth, "both")
mustBeEqualTo(t, fs1, nameInBoth, "both")
}
func mustNotSet(t *testing.T, fs *flag.FlagSet, name, value string) {
if err := fs.Set(name, value); err == nil {
t.Fatalf(
"want error on setting flag %q value to %q: %v",
name, value, err,
)
}
}
func mustSet(t *testing.T, fs *flag.FlagSet, name, value string) {
if err := fs.Set(name, value); err != nil {
t.Fatalf("can't set flag %q value to %q: %v", name, value, err)
}
}
func mustBeEqualTo(t *testing.T, fs *flag.FlagSet, name, value string) {
mustBeDefined(t, fs, name)
if act, exp := fs.Lookup(name).Value.String(), value; act != exp {
t.Fatalf("flag %q value is %q; want %q", name, act, exp)
}
}
func mustNotBeDefined(t *testing.T, fs *flag.FlagSet, name string) {
if fs.Lookup(name) != nil {
t.Fatalf("want flag %q to not be present in set", name)
}
}
func mustBeDefined(t *testing.T, fs *flag.FlagSet, name string) {
if fs.Lookup(name) == nil {
t.Fatalf("want flag %q to be present in set", name)
}
}
func mustBeActual(t *testing.T, fs *flag.FlagSet, name string) {
if !isActual(fs, name) {
t.Fatalf("want flag %q to be actual in set", name)
}
}
func mustNotBeActual(t *testing.T, fs *flag.FlagSet, name string) {
if isActual(fs, name) {
t.Fatalf("want flag %q to not be actual in set", name)
}
}
func isActual(fs *flag.FlagSet, name string) (actual bool) {
fs.Visit(func(f *flag.Flag) {
if f.Name == name {
actual = true
}
})
return
}
func TestCombineFlags(t *testing.T) {
for _, test := range []struct {
name string
flags [2]*flag.Flag
exp *flag.Flag
panic bool
}{
{
name: "different names",
flags: [2]*flag.Flag{
stringFlag("foo", "def", "desc#0"),
stringFlag("bar", "def", "desc#1"),
},
panic: true,
},
{
name: "different default values",
flags: [2]*flag.Flag{
stringFlag("foo", "def#0", "desc#0"),
stringFlag("foo", "def#1", "desc#1"),
},
exp: stringFlag("foo", "", "desc#0 / desc#1"),
},
{
name: "basic",
flags: [2]*flag.Flag{
stringFlag("foo", "def", "desc#0"),
stringFlag("foo", "def", "desc#1"),
},
exp: stringFlag("foo", "def", "desc#0 / desc#1"),
},
{
name: "basic",
flags: [2]*flag.Flag{
stringFlag("foo", "def", "desc#0"),
stringFlag("foo", "", "desc#1"),
},
exp: stringFlag("foo", "", "desc#0 / desc#1"),
},
} {
t.Run(test.name, func(t *testing.T) {
type flagOrPanic struct {
flag *flag.Flag
panic interface{}
}
done := make(chan flagOrPanic)
go func() {
defer func() {
if p := recover(); p != nil {
done <- flagOrPanic{
panic: p,
}
}
}()
done <- flagOrPanic{
flag: CombineFlags(test.flags[0], test.flags[1]),
}
}()
x := <-done
if !test.panic && x.panic != nil {
t.Fatalf("panic() recovered: %s", x.panic)
}
if test.panic {
if x.panic == nil {
t.Fatalf("want panic; got nothing")
}
return
}
opts := []cmp.Option{
cmp.Transformer("Value", func(v flag.Value) string {
return v.String()
}),
}
if act, exp := x.flag, test.exp; !cmp.Equal(act, exp, opts...) {
t.Errorf("unexpected flag:\n%s", cmp.Diff(exp, act, opts...))
}
exp := fmt.Sprintf("%x", rand.Int63())
if err := x.flag.Value.Set(exp); err != nil {
t.Fatalf("unexpected Set() error: %v", err)
}
for _, f := range test.flags {
assertEquals(t, f, exp)
}
})
}
}
func TestLinkFlag(t *testing.T) {
for _, test := range []struct {
name string
flags [2]*flag.Flag
links [2]string
}{
{
name: "basic",
flags: [2]*flag.Flag{
stringFlag("foo", "def#0", "desc#0"),
stringFlag("bar", "def#1", "desc#1"),
},
links: [2]string{"foo", "bar"},
},
} {
t.Run(test.name, func(t *testing.T) {
fs := flag.NewFlagSet("", flag.PanicOnError)
for _, f := range test.flags {
if f != nil {
fs.Var(f.Value, f.Name, f.Usage)
}
}
LinkFlag(fs, test.links[0], test.links[1])
// First, test that setting for src flag affects dst flag.
exp := fmt.Sprintf("%x", rand.Int63())
fs.Set(test.links[0], exp)
for _, n := range test.links {
if f := fs.Lookup(n); f != nil {
assertEquals(t, f, exp)
}
}
// Second, test that setting dst flag doesn't affect src flag.
nonExp := fmt.Sprintf("%x", rand.Int63())
fs.Set(test.flags[1].Name, nonExp)
assertEquals(t, test.flags[0], exp) // Still the same.
assertEquals(t, test.flags[1], nonExp) // Updated.
})
}
}
func assertEquals(t *testing.T, f *flag.Flag, exp string) {
if act := f.Value.String(); act != exp {
t.Errorf(
"unexpected flag %q value: %s; want %s",
f.Name, act, exp,
)
}
}
func stringFlag(name, def, desc string) *flag.Flag {
fs := flag.NewFlagSet("", flag.PanicOnError)
fs.String(name, def, desc)
f := fs.Lookup(name)
return f
}