-
Notifications
You must be signed in to change notification settings - Fork 16
/
options_test.go
242 lines (207 loc) · 7.25 KB
/
options_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
package options_test
import (
"flag"
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/mreiferson/go-options"
)
// TestFlagSetDefaults verifies that default flag values are applied in the
// absence of user-specified setting.
func TestFlagSetDefaults(t *testing.T) {
flagSet := flag.NewFlagSet("TestFlagSetDefaults", flag.PanicOnError)
opts := &Options{1024768, 1 * time.Hour, ""}
flagSet.Int64("max-size", 1024768, "maximum size")
flagSet.Duration("timeout", 1*time.Hour, "timeout setting")
flagSet.String("description", "", "description info")
if err := flagSet.Parse([]string{"-timeout=5s"}); err != nil {
t.Fatal(err)
}
cfg := map[string]interface{}{}
options.Resolve(opts, flagSet, cfg)
if expected, actual := flagSet.Lookup("max-size").Value.(flag.Getter).Get().(int64), opts.MaxSize; actual != expected {
t.Errorf("Expected opts.MaxSize to default to %v but actual=%v", expected, actual)
}
}
// TestConfigWithOverlappingOsArgs verifies that flag values set via map config
// are respected.
func TestConfigWithOverlappingOsArgs(t *testing.T) {
osArgsBak := make([]string, len(os.Args))
for i, arg := range os.Args {
osArgsBak[i] = arg
}
defer func() { os.Args = osArgsBak }() // Restore os.Args afterwards.
os.Args = []string{"./logserver/logserver", "-config", "/tmp/cfg.toml"}
type ServerOptions struct {
Server bool `flag:"server"`
Debug bool `flag:"debug"`
Version bool `flag:"version"`
}
flagSet := flag.NewFlagSet("TestConfigWithOverlappingOsArgs", flag.PanicOnError)
flagSet.Bool("server", false, "run in server mode")
flagSet.Bool("debug", false, "toggle debug output")
flagSet.Bool("version", false, "show version information and then exit")
if err := flagSet.Parse(os.Args); err != nil {
t.Fatal(err)
}
opts := &ServerOptions{}
cfg := map[string]interface{}{
"server": true,
}
options.Resolve(opts, flagSet, cfg)
if expected, actual := true, opts.Server; actual != expected {
t.Errorf("Expected opts.Server=%v but actual=%v", expected, actual)
}
if expected, actual := false, opts.Debug; actual != expected {
t.Errorf("Expected opts.Debug=%v but actual=%v", expected, actual)
}
if expected, actual := false, opts.Version; actual != expected {
t.Errorf("Expected opts.Version=%v but actual=%v", expected, actual)
}
}
func TestFloat64(t *testing.T) {
type ConfigurableThing struct {
Percentage float64 `flag:"percentage"`
}
const defaultValue = 0.5
testCases := []struct {
Args []string
Expected float64
}{
{
Args: []string{""},
Expected: defaultValue,
},
{
Args: []string{},
Expected: defaultValue,
},
{
Args: []string{"-percentage", fmt.Sprint(defaultValue)},
Expected: defaultValue,
},
{
Args: []string{"-percentage", "0.753"},
Expected: 0.753,
},
{
Args: []string{"-percentage", "-0.753"},
Expected: -0.753,
},
{
Args: []string{"-percentage=-0.117983"},
Expected: -0.117983,
},
}
for i, testCase := range testCases {
flagSet := flag.NewFlagSet("TestFloat64", flag.PanicOnError)
flagSet.Float64("percentage", defaultValue, "integer or decimal representing the percentage")
if err := flagSet.Parse(testCase.Args); err != nil {
t.Fatal(err)
}
configThing := &ConfigurableThing{defaultValue}
cfg := map[string]interface{}{}
options.Resolve(configThing, flagSet, cfg)
if expected, actual := testCase.Expected, configThing.Percentage; actual != expected {
t.Errorf("[i=%v testCase=%+v] Expected configThing.Percentage=%v but actual=%v", i, testCase, expected, actual)
}
}
}
func TestEmbeddedTypeOptions(t *testing.T) {
newFlagSet := func() *flag.FlagSet {
flagSet := flag.NewFlagSet("TestEmbeddedTypeOptions", flag.ExitOnError)
flagSet.Bool("peaceful", false, "peaceful flag details")
flagSet.Bool("inside", false, "inside flag details")
flagSet.Bool("undo", false, "undo flag details")
flagSet.Bool("install", false, "install flag details")
flagSet.Int("arbitrary", 0, "arbitrary flag details")
flagSet.String("fields", "", "fields flag details")
flagSet.String("user", "", "user flag details")
return flagSet
}
type EmbeddedAsValue struct {
Arbitrary int `flag:"arbitrary"`
Fields []string `flag:"fields"`
}
type EmbeddedAsPointer struct {
EmbeddedAsValue
Peaceful bool `flag:"peaceful"`
Inside bool `flag:"inside"`
Undo bool `flag:"undo"`
}
type SampleConfig struct {
*EmbeddedAsPointer
Install bool `flag:"install"`
User string `flag:"user"`
otherField string
}
testCases := []struct {
Args []string
CallbackFunc func(config *SampleConfig)
}{
{
Args: []string{"-inside"},
CallbackFunc: func(config *SampleConfig) {
if expected, actual := false, config.Peaceful; actual != expected {
t.Errorf("Expected config.Peaceful=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := true, config.Inside; actual != expected {
t.Errorf("Expected config.Inside=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := false, config.Undo; actual != expected {
t.Errorf("Expected config.Undo=%v but actual=%v (config=%+v)", expected, actual, *config)
}
},
},
{
Args: []string{"-peaceful", "-undo", "-user=mreiferson"},
CallbackFunc: func(config *SampleConfig) {
if expected, actual := true, config.Peaceful; actual != expected {
t.Errorf("Expected config.Peaceful=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := false, config.Inside; actual != expected {
t.Errorf("Expected config.Inside=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := true, config.Undo; actual != expected {
t.Errorf("Expected config.Undo=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := "mreiferson", config.User; actual != expected {
t.Errorf("Expected config.User=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := 0, config.EmbeddedAsPointer.Arbitrary; actual != expected {
t.Errorf("Expected config.EmbeddedAsPointer.Arbitrary=%v but actual=%v (config=%+v)", expected, actual, *config)
}
},
},
{
Args: []string{"-peaceful", "-arbitrary", "99", "-fields=a,a1,B,B1,c"},
CallbackFunc: func(config *SampleConfig) {
if expected, actual := true, config.Peaceful; actual != expected {
t.Errorf("Expected config.Peaceful=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := 99, config.Arbitrary; actual != expected {
t.Errorf("Expected config.Arbitrary=%v but actual=%v (config=%+v)", expected, actual, *config)
}
if expected, actual := []string{"a", "a1", "B", "B1", "c"}, config.EmbeddedAsPointer.EmbeddedAsValue.Fields; !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected config.EmbeddedAsPointer.EmbeddedAsValue.Fields=%v but actual=%v (config=%+v)", expected, actual, *config)
}
},
},
}
for _, testCase := range testCases {
var (
config = &SampleConfig{
EmbeddedAsPointer: &EmbeddedAsPointer{
EmbeddedAsValue: EmbeddedAsValue{},
},
otherField: "hello there!",
}
flagSet = newFlagSet()
)
flagSet.Parse(testCase.Args)
options.Resolve(config, flagSet, nil)
testCase.CallbackFunc(config)
}
}