-
Notifications
You must be signed in to change notification settings - Fork 16
/
com_test.go
579 lines (537 loc) · 13.8 KB
/
com_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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package commandeer
import (
"flag"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/jaffee/commandeer/test"
"github.com/spf13/pflag"
)
func TestLoadEnv(t *testing.T) {
fs := &flagSet{flag.NewFlagSet("", flag.ExitOnError)}
// create initial instance with defaults
mm := &test.SimpleMain{
One: "one",
Two: 2,
Three: 3,
Four: true,
Five: 5,
Six: 6,
Seven: 7.0,
Eight: time.Millisecond * 8,
Twelve: "blah",
}
// create flags with defaults by walking instance
err := Flags(fs, mm)
if err != nil {
t.Fatalf("making Flags: %v", err)
}
// set up environment
prefix := "COMMANDEER_"
err = os.Setenv("COMMANDEER_ONE", "z")
if err != nil {
t.Fatalf("setting up environment for set: %v", err)
}
defer os.Unsetenv("COMMANDEER_ONE")
// change values on instance by reading environment
err = loadEnv(fs, prefix)
if err != nil {
t.Fatalf("loading env: %v", err)
}
if mm.One != "z" {
t.Errorf("unexpected value for One: %s", mm.One)
}
// change values on instance by parsing command line
err = fs.Parse([]string{"-two", "99", "-twelve", "haha"})
if err != nil {
t.Fatalf("parsing command line: %v", err)
}
if mm.Two != 99 {
t.Fatalf("command line parsing didn't produce expected value 99, got %d", mm.Two)
}
// ensure that parsing command line doesn't vaules except one's that are specified.
if mm.One != "z" {
t.Errorf("unexpected value for One after command line parsing: %s", mm.One)
}
if mm.Twelve != "haha" {
t.Errorf("unexpected value for Twelve after cmd line parsing: %s", mm.Twelve)
}
// simulate parsing a config file and setting values directly from it
mm.Three = 33
mm.Five = 55
// reload environment
err = os.Setenv("COMMANDEER_FIVE", "56")
if err != nil {
t.Fatalf("setting up environment for set: %v", err)
}
defer os.Unsetenv("COMMANDEER_FIVE")
err = os.Setenv("COMMANDEER_TWO", "23")
if err != nil {
t.Fatalf("setting up environment for set: %v", err)
}
defer os.Unsetenv("COMMANDEER_TWO")
err = loadEnv(fs, prefix)
if err != nil {
t.Fatalf("loading env: %v", err)
}
if mm.Five != 56 {
t.Fatalf("env reload failed, five is %d", mm.Five)
}
if mm.Two != 23 {
t.Fatalf("env reload should have clobbered two, but mm.Two is %d", mm.Two)
}
// re parse command line since env clobbered two value
err = fs.Parse([]string{"-two", "99"})
if err != nil {
t.Fatalf("parsing command line: %v", err)
}
if mm.Two != 99 {
t.Fatalf("command line reparsing didn't produce expected value 99, got %d", mm.Two)
}
// ensure that parsing command line doesn't vaules except one's that are specified.
if mm.One != "z" {
t.Errorf("unexpected value for One after command line parsing: %s", mm.One)
}
}
func TestLoadArgsEnv(t *testing.T) {
mm := test.NewSimpleMain()
mustSetenv(t, "COMMANDEER_ONE", "envone")
mustSetenv(t, "COMMANDEER_TWO", "32")
fs := &flagSet{flag.NewFlagSet("", flag.ExitOnError)}
err := LoadArgsEnv(fs, mm, []string{"-two=24", "-seven", "7.3", "-nine", "a,b,c"}, "COMMANDEER_", nil)
if err != nil {
t.Fatalf("LoadArgsEnv: %v", err)
}
if mm.One != "envone" {
t.Errorf("unexpected value for One: %s", mm.One)
}
if mm.Two != 24 {
t.Errorf("unexpected value for Two: %d", mm.Two)
}
if mm.Seven != 7.3 {
t.Errorf("unexpected value for Seven: %f", mm.Seven)
}
if !reflect.DeepEqual(mm.Nine, []string{"a", "b", "c"}) {
t.Errorf("wrong value for Nine: %v", mm.Nine)
}
}
func mustSetenv(t *testing.T, key, val string) {
err := os.Setenv(key, val)
if err != nil {
t.Fatalf("setting env '%s' to '%s': %v", key, val, err)
}
}
func TestZeroStruct(t *testing.T) {
fs := pflag.NewFlagSet("myset", pflag.ContinueOnError)
mm := &test.MyMain{}
err := Flags(fs, mm)
if err != nil {
t.Fatalf("setting flags with zero MyMain: %v", err)
}
err = fs.Parse([]string{"-h"})
if err != nil && err != pflag.ErrHelp {
t.Fatalf("parsing help flag: %v", err)
}
}
func TestZeroStructStdlibFlag(t *testing.T) {
fs := flag.NewFlagSet("", flag.ContinueOnError)
mm := &test.SimpleMain{}
err := Flags(fs, mm)
if err != nil {
t.Fatalf("setting flags with zero MyMain: %v", err)
}
err = fs.Parse([]string{"-h"})
if err != nil && err != flag.ErrHelp {
t.Fatalf("parsing help flag: %v", err)
}
}
func TestNonStruct(t *testing.T) {
var a int = 4
err := Run(&a)
if !strings.Contains(err.Error(), "value must be pointer to struct, but is pointer to") {
t.Fatalf("pointer to int should have failed with different err: %v", err)
}
}
func TestDowncaseAndDash(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "a",
expected: "a",
},
{
input: "A",
expected: "a",
},
{
input: "aB",
expected: "a-b",
},
{
input: "Ba",
expected: "ba",
},
{
input: "HelloFriend",
expected: "hello-friend",
},
{
input: "helloFriend",
expected: "hello-friend",
},
{
input: "AAA",
expected: "aaa",
},
{
input: "myURL",
expected: "my-url",
},
{
input: "MyURL",
expected: "my-url",
},
{
input: "URLFinder",
expected: "url-finder",
},
{
input: "AaURLFinder",
expected: "aa-url-finder",
},
{
input: "aURLFinder",
expected: "a-url-finder",
},
{
input: "AURLFinder",
expected: "aurl-finder", // NOTE: no easy way to handle this properly
},
{
input: "MyURLs",
expected: "my-ur-ls", // NOTE: even worse
},
{
input: "KissAToad",
expected: "kiss-a-toad",
},
{
input: "IAmAToad",
expected: "i-am-a-toad",
},
}
for i, tst := range tests {
output := downcaseAndDash(tst.input)
if output != tst.expected {
t.Errorf("test: %d, '%v' is not '%v'", i, output, tst.expected)
}
}
}
type NonRunner struct {
A int
}
func TestRun(t *testing.T) {
tests := []struct {
main interface{}
err string
}{
{
main: &test.MyMain{},
err: "mymain error",
},
{
main: &NonRunner{},
err: "called 'Run' with something which doesn't implement the 'Run() error' method.",
},
{
main: test.MyMain{},
err: "calling Flags: value must be pointer to struct, but is struct",
},
}
for i, tst := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
err := RunArgs(pflag.NewFlagSet("tstSet", pflag.ContinueOnError), tst.main, os.Args[1:])
if err.Error() != tst.err {
t.Fatalf("expected '%s', got '%s'", tst.err, err.Error())
}
})
}
}
func TestRunMyMain(t *testing.T) {
mm := test.NewMyMain()
flags := pflag.NewFlagSet("tst", pflag.ContinueOnError)
err := Flags(flags, mm)
if err != nil {
t.Fatalf("getting flags for MyMain: %v", err)
}
if f := flags.Lookup("thing"); f != nil {
if f.DefValue != "Thing" {
t.Fatalf("'thing' not defined properly")
}
} else {
t.Fatalf("couldn't lookup 'thing'")
}
if f := flags.Lookup("wing"); f != nil {
t.Fatalf("wing shouldn't be defined")
}
if f := flags.Lookup("bing"); f != nil {
t.Fatalf("bing shouldn't be defined")
}
if f := flags.Lookup("-"); f != nil {
t.Fatalf("- shouldn't be defined")
}
if f := flags.Lookup("ipmask"); f != nil {
if f.DefValue != "01020300" {
t.Fatalf("'ipmask' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'ipmask'")
}
if f := flags.Lookup("aip-net"); f != nil {
if f.DefValue != "1.2.3.4/01020300" {
t.Fatalf("'aip-net' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'aip-net'")
}
if f := flags.Lookup("aip"); f != nil {
if f.DefValue != "1.2.3.4" {
t.Fatalf("'aip' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'aip'")
}
if f := flags.Lookup("aip-slice"); f != nil {
if f.DefValue != "[]" {
t.Fatalf("'aip-slice' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'aip-slice'")
}
if f := flags.Lookup("a-bool"); f != nil {
if f.DefValue != "false" {
t.Fatalf("'abool' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-bool'")
}
if f := flags.Lookup("a-int"); f != nil {
if f.DefValue != "1000000000" {
t.Fatalf("'aint' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-int'")
}
if f := flags.Lookup("aint8"); f != nil {
if f.DefValue != "-7" {
t.Fatalf("'aint8' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'aint8'")
}
if f := flags.Lookup("anint16"); f != nil {
if f.DefValue != "-30000" {
t.Fatalf("'anint16' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'anint16'")
}
if f := flags.Lookup("aint32"); f != nil {
if f.DefValue != "-2000000000" {
t.Fatalf("'aint32' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'aint32'")
}
if f := flags.Lookup("a-int64"); f != nil {
if f.DefValue != "-9999999999" {
t.Fatalf("'aint64' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-int64'")
}
if f := flags.Lookup("a-float"); f != nil {
if f.DefValue != "12.2" {
t.Fatalf("'a-float' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'afloat'")
}
if f := flags.Lookup("afloat32"); f != nil {
if f.DefValue != "-12.3" {
t.Fatalf("'afloat32' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'afloat32'")
}
if f := flags.Lookup("a-uint"); f != nil {
if f.DefValue != "9" {
t.Fatalf("'a-uint' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'auint'")
}
if f := flags.Lookup("auint8"); f != nil {
if f.DefValue != "10" {
t.Fatalf("'auint8' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'auint8'")
}
if f := flags.Lookup("auint16"); f != nil {
if f.DefValue != "11" {
t.Fatalf("'auint16' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'auint16'")
}
if f := flags.Lookup("auint32"); f != nil {
if f.DefValue != "12" {
t.Fatalf("'auint32' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'auint32'")
}
if f := flags.Lookup("a-uint64"); f != nil {
if f.DefValue != "13" {
t.Fatalf("'a-uint64' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-uint64'")
}
if f := flags.Lookup("a-duration"); f != nil {
if f.DefValue != "1s" {
t.Fatalf("'a-duration' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-duration'")
}
if f := flags.Lookup("a-string-slice"); f != nil {
if f.DefValue != "[hey,there]" {
t.Fatalf("'a-string-slice' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-string-slice'")
}
if f := flags.Lookup("a-bool-slice"); f != nil {
if f.DefValue != "[true,false]" {
t.Fatalf("'a-bool-slice' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-bool-slice'")
}
if f := flags.Lookup("a-int-slice"); f != nil {
if f.DefValue != "[9,-8,7]" {
t.Fatalf("'a-int-slice' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-int-slice'")
}
if f := flags.Lookup("a-uint-slice"); f != nil {
if f.DefValue != "[7,8,9]" {
t.Fatalf("'a-uint-slice' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'a-uint-slice'")
}
if f := flags.Lookup("subthing.a-bool"); f != nil {
if f.DefValue != "true" {
t.Fatalf("'subthing.a-bool' not defined properly, got '%v'", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'subthing.a-bool'")
}
}
func TestRunSimpleMain(t *testing.T) {
mm := test.NewSimpleMain()
flags := flag.NewFlagSet("tstsimplemain", flag.ContinueOnError)
err := Flags(flags, mm)
if err != nil {
t.Fatalf("getting flags for MyMain: %v", err)
}
if f := flags.Lookup("one"); f != nil {
if f.DefValue != "one" {
t.Fatalf("wrong default value for 'one': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'one'")
}
if f := flags.Lookup("two"); f != nil {
if f.DefValue != "2" {
t.Fatalf("wrong default value for 'two': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'two'")
}
if f := flags.Lookup("three"); f != nil {
if f.DefValue != "3" {
t.Fatalf("wrong default value for 'three': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'three'")
}
if f := flags.Lookup("four"); f != nil {
if f.DefValue != "true" {
t.Fatalf("wrong default value for 'four': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'four'")
}
if f := flags.Lookup("five"); f != nil {
if f.DefValue != "5" {
t.Fatalf("wrong default value for 'five': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'five'")
}
if f := flags.Lookup("six"); f != nil {
if f.DefValue != "6" {
t.Fatalf("wrong default value for 'six': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'six'")
}
if f := flags.Lookup("seven"); f != nil {
if f.DefValue != "7" {
t.Fatalf("wrong default value for 'seven': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'seven'")
}
if f := flags.Lookup("eight"); f != nil {
if f.DefValue != "8ms" {
t.Fatalf("wrong default value for 'eight': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't lookup 'eight'")
}
if f := flags.Lookup("nine"); f != nil {
if f.DefValue != "[9,nine]" {
t.Fatalf("wrong default value for 'nine': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't look up 'nine'")
}
if f := flags.Lookup("ten"); f != nil {
if f.DefValue != "1970-01-01T00:00:00Z" {
t.Fatalf("wrong default value for 'ten': %v", f.DefValue)
}
} else {
t.Fatalf("couldn't look up 'ten'")
}
if f := flags.Lookup("eleven"); f != nil {
if f.DefValue != "11s" {
t.Fatalf("wrong default value for 'eleven': %v", f.DefValue)
}
}
if f := flags.Lookup("twelve"); f != nil {
if f.DefValue != "twelve" {
t.Fatalf("wrong default value for 'tweleve': %v", f.DefValue)
}
}
}