/
options_test.go
272 lines (252 loc) · 7.91 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
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
package args_test
import (
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/thrawn01/args"
)
var _ = Describe("Options", func() {
var opts *args.Options
var log *TestLogger
BeforeEach(func() {
log = NewTestLogger()
parser := args.NewParser()
parser.SetLog(log)
opts = parser.NewOptionsFromMap(
map[string]interface{}{
"int": 1,
"bool": true,
"string": "one",
"path": "~/.myrc",
"endpoints": map[string]interface{}{
"endpoint1": "host1",
"endpoint2": "host2",
"endpoint3": "host3",
},
"deeply": map[string]interface{}{
"nested": map[string]interface{}{
"thing": "foo",
},
"foo": "bar",
},
},
)
})
Describe("log", func() {
It("Should log to StdLogger when cast fails", func() {
result := opts.Int("string")
Expect(log.GetEntry()).To(Equal(`unable to cast "one" of type string to int for key 'string'|`))
Expect(result).To(Equal(0))
})
})
Describe("Int()", func() {
It("Should convert values to integers", func() {
result := opts.Int("int")
Expect(log.GetEntry()).To(Equal(""))
Expect(result).To(Equal(1))
})
It("Should return default value if key doesn't exist", func() {
result := opts.Int("none")
Expect(log.GetEntry()).To(Equal(""))
Expect(result).To(Equal(0))
})
})
Describe("Bool()", func() {
It("Should convert values to boolean", func() {
result := opts.Bool("bool")
Expect(log.GetEntry()).To(Equal(""))
Expect(result).To(Equal(true))
})
It("Should return default value if key doesn't exist", func() {
result := opts.Bool("none")
Expect(result).To(Equal(false))
})
})
Describe("String()", func() {
It("Should return values as string", func() {
result := opts.String("string")
Expect(log.GetEntry()).To(Equal(""))
Expect(result).To(Equal("one"))
})
It("Should return default value if key doesn't exist", func() {
result := opts.String("none")
Expect(result).To(Equal(""))
})
})
Describe("FilePath()", func() {
It("Should return values as string", func() {
result := opts.FilePath("string")
Expect(log.GetEntry()).To(Equal(""))
Expect(result).To(Equal("one"))
})
It("Should return default value if key doesn't exist", func() {
result := opts.FilePath("none")
Expect(result).To(Equal(""))
})
It("Should return expanded path if value contains a tilde", func() {
result := opts.FilePath("path")
if len(result) == 7 {
Fail("Tilde was not expanded")
}
})
})
Describe("NoArgs()", func() {
It("Should return true if no arguments on the command line", func() {
parser := args.NewParser()
parser.AddOption("--power-level").IsInt().Default("1")
opt, err := parser.Parse(nil)
Expect(err).To(BeNil())
Expect(opt.Int("power-level")).To(Equal(1))
Expect(opt.NoArgs()).To(Equal(true))
})
It("Should return false if arguments on the command line", func() {
parser := args.NewParser()
parser.AddOption("--power-level").IsInt().Default("1")
opt, err := parser.Parse(&[]string{"--power-level", "2"})
Expect(err).To(BeNil())
Expect(opt.Int("power-level")).To(Equal(2))
Expect(opt.NoArgs()).To(Equal(false))
})
})
Describe("ToMap()", func() {
It("Should return a map of the group options", func() {
Expect(opts.Group("endpoints").ToMap()).To(Equal(map[string]interface{}{
"endpoint1": "host1",
"endpoint2": "host2",
"endpoint3": "host3",
}))
})
It("Should return an empty map if the group doesn't exist", func() {
Expect(opts.Group("no-group").ToMap()).To(Equal(map[string]interface{}{}))
})
})
Describe("IsSet()", func() {
It("Should return true if the value is not a cast default", func() {
parser := args.NewParser()
parser.AddOption("--is-set").IsInt().Default("1")
parser.AddOption("--not-set")
opt, err := parser.Parse(nil)
Expect(err).To(BeNil())
Expect(opt.IsSet("is-set")).To(Equal(true))
Expect(opt.IsSet("not-set")).To(Equal(false))
})
})
Describe("IsArg()", func() {
It("Should return true if the option as set via the command line", func() {
cmdLine := []string{"--two", "2"}
parser := args.NewParser()
parser.AddOption("--one").IsInt().Default("1")
parser.AddOption("--two").IsInt().Default("0")
opt, err := parser.Parse(&cmdLine)
Expect(err).To(BeNil())
Expect(opt.Int("one")).To(Equal(1))
Expect(opt.Int("two")).To(Equal(2))
Expect(opt.IsArg("one")).To(Equal(false))
Expect(opt.IsArg("two")).To(Equal(true))
})
})
Describe("IsEnv()", func() {
It("Should return true if the option was set via an environment variable", func() {
parser := args.NewParser()
parser.AddOption("--one").IsInt().Default("1").Env("ONE")
parser.AddOption("--two").IsInt().Default("0").Env("TWO")
os.Setenv("TWO", "2")
opt, err := parser.Parse(nil)
Expect(err).To(BeNil())
Expect(opt.Int("one")).To(Equal(1))
Expect(opt.Int("two")).To(Equal(2))
Expect(opt.IsEnv("one")).To(Equal(false))
Expect(opt.IsEnv("two")).To(Equal(true))
})
})
Describe("IsDefault()", func() {
It("Should return true if the option used the default value", func() {
cmdLine := []string{"--two", "2"}
parser := args.NewParser()
parser.AddOption("--one").IsInt().Default("1")
parser.AddOption("--two").IsInt().Default("0")
opt, err := parser.Parse(&cmdLine)
Expect(err).To(BeNil())
Expect(opt.Int("one")).To(Equal(1))
Expect(opt.Int("two")).To(Equal(2))
Expect(opt.IsDefault("one")).To(Equal(true))
Expect(opt.IsDefault("two")).To(Equal(false))
})
})
Describe("WasSeen()", func() {
It("Should return true if the option was seen on the commandline", func() {
cmdLine := []string{"--is-seen"}
parser := args.NewParser()
parser.AddOption("--is-set").IsInt().Default("1")
parser.AddOption("--is-seen").IsTrue()
parser.AddOption("--not-set")
opt, err := parser.Parse(&cmdLine)
Expect(err).To(BeNil())
Expect(opt.WasSeen("is-set")).To(Equal(false))
Expect(opt.WasSeen("not-set")).To(Equal(false))
Expect(opt.WasSeen("is-seen")).To(Equal(true))
})
It("Should return true if the option was seen in the environment", func() {
parser := args.NewParser()
parser.AddOption("--is-set").IsInt().Default("1")
parser.AddOption("--is-seen").IsTrue().Env("IS_SEEN")
parser.AddOption("--not-set")
os.Setenv("IS_SEEN", "true")
opt, err := parser.Parse(nil)
Expect(err).To(BeNil())
Expect(opt.WasSeen("is-set")).To(Equal(false))
Expect(opt.WasSeen("not-set")).To(Equal(false))
Expect(opt.WasSeen("is-seen")).To(Equal(true))
})
})
Describe("InspectOpt()", func() {
It("Should return the option object requested", func() {
parser := args.NewParser()
parser.AddOption("--is-set").IsInt().Default("1")
parser.AddOption("--not-set")
opt, err := parser.Parse(nil)
Expect(err).To(BeNil())
option := opt.InspectOpt("is-set")
Expect(option.GetValue().(int)).To(Equal(1))
Expect(option.GetRule().Flags).To(Equal(int64(544)))
})
})
Describe("Required()", func() {
It("Should return nil if all values are provided", func() {
parser := args.NewParser()
parser.AddOption("--is-set").IsInt().Default("1")
parser.AddOption("--is-provided")
parser.AddOption("--not-set")
opt, err := parser.Parse(&[]string{"--is-provided", "foo"})
Expect(err).To(BeNil())
// All options required have values
Expect(opt.Required([]string{"is-set", "is-provided"})).To(BeNil())
// Option 'not-set' is missing is not provided
err = opt.Required([]string{"is-set", "is-provided", "not-set"})
Expect(err).To(Not(BeNil()))
Expect(err.Error()).To(Equal("not-set"))
})
})
Describe("ToString()", func() {
It("Should return a string representation of the options", func() {
output := opts.ToString()
Expect(output).To(Equal(`{
'bool' = true
'deeply' = {
'foo' = bar
'nested' = {
'thing' = foo
}
}
'endpoints' = {
'endpoint1' = host1
'endpoint2' = host2
'endpoint3' = host3
}
'int' = 1
'path' = ~/.myrc
'string' = one
}`))
})
})
})