-
Notifications
You must be signed in to change notification settings - Fork 4
/
flag_test.go
110 lines (105 loc) · 2.63 KB
/
flag_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
package venom
import (
"flag"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlagsetResolver(t *testing.T) {
testIO := []struct {
tc string
flags *flag.FlagSet
args []string
keys []string
expect interface{}
ok bool
}{
{
tc: "should resolve nothing from os.Args",
flags: nil,
args: nil,
keys: []string{},
expect: nil,
ok: false,
},
{
tc: "should resolve nothing from an empty FlagSet",
flags: flag.NewFlagSet("test", flag.ContinueOnError),
args: []string{},
keys: []string{},
expect: nil,
ok: false,
},
{
tc: "should resolve boolean flag from FlagSet",
flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.Bool("verbose", false, "enable verbose")
return fs
}(),
args: []string{"-verbose"},
keys: []string{"verbose"},
expect: "true",
ok: true,
},
{
tc: "should resolve hyphenated config from FlagSet",
flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.String("log-level", "WARNING", "set log level")
return fs
}(),
args: []string{"-log-level=INFO"},
keys: []string{"log", "level"},
expect: "INFO",
ok: true,
},
{
tc: "should resolve expected value from FlagSet",
flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.Bool("verbose", false, "enable verbose")
fs.String("log-level", "WARNING", "set log level")
return fs
}(),
args: []string{"-verbose", "-log-level=INFO"},
keys: []string{"log", "level"},
expect: "INFO",
ok: true,
},
{
tc: "should resolve expected value from FlagSet, and ignore un specified flag",
flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.Bool("verbose", false, "enable verbose")
fs.String("log-level", "WARNING", "set log level")
return fs
}(),
args: []string{"-log-level=INFO"},
keys: []string{"log", "level"},
expect: "INFO",
ok: true,
},
{
tc: "should fail to parse unknown flag",
flags: flag.NewFlagSet("test", flag.ContinueOnError),
args: []string{"-verbose"},
keys: []string{"verbose"},
expect: nil,
ok: false,
},
}
for _, test := range testIO {
t.Run(test.tc, func(t *testing.T) {
r := &FlagsetResolver{
Flags: test.flags,
Arguments: test.args,
}
// iterate twice to ensure we only parse flags once
for i := 0; i < 2; i++ {
actual, ok := r.Resolve(test.keys, nil)
assert.Equal(t, test.ok, ok)
assert.Equal(t, test.expect, actual)
}
})
}
}