forked from alecthomas/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_test.go
353 lines (298 loc) · 7.97 KB
/
resolver_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
package kong_test
import (
"errors"
"os"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/alecthomas/kong"
)
type envMap map[string]string
func tempEnv(env envMap) func() {
for k, v := range env {
os.Setenv(k, v)
}
return func() {
for k := range env {
os.Unsetenv(k)
}
}
}
func newEnvParser(t *testing.T, cli interface{}, env envMap, options ...kong.Option) (*kong.Kong, func()) {
t.Helper()
restoreEnv := tempEnv(env)
parser := mustNew(t, cli, options...)
return parser, restoreEnv
}
func TestEnvarsFlagBasic(t *testing.T) {
var cli struct {
String string `env:"KONG_STRING"`
Slice []int `env:"KONG_SLICE"`
Interp string `env:"${kongInterp}"`
}
kongInterpEnv := "KONG_INTERP"
parser, unsetEnvs := newEnvParser(t, &cli,
envMap{
"KONG_STRING": "bye",
"KONG_SLICE": "5,2,9",
"KONG_INTERP": "foo",
},
kong.Vars{
"kongInterp": kongInterpEnv,
},
)
defer unsetEnvs()
_, err := parser.Parse([]string{})
require.NoError(t, err)
require.Equal(t, "bye", cli.String)
require.Equal(t, []int{5, 2, 9}, cli.Slice)
require.Equal(t, "foo", cli.Interp)
}
func TestEnvarsFlagOverride(t *testing.T) {
var cli struct {
Flag string `env:"KONG_FLAG"`
}
parser, restoreEnv := newEnvParser(t, &cli, envMap{"KONG_FLAG": "bye"})
defer restoreEnv()
_, err := parser.Parse([]string{"--flag=hello"})
require.NoError(t, err)
require.Equal(t, "hello", cli.Flag)
}
func TestEnvarsTag(t *testing.T) {
var cli struct {
Slice []int `env:"KONG_NUMBERS"`
}
parser, restoreEnv := newEnvParser(t, &cli, envMap{"KONG_NUMBERS": "5,2,9"})
defer restoreEnv()
_, err := parser.Parse([]string{})
require.NoError(t, err)
require.Equal(t, []int{5, 2, 9}, cli.Slice)
}
func TestEnvarsEnvPrefix(t *testing.T) {
type Anonymous struct {
Slice []int `env:"NUMBERS"`
}
var cli struct {
Anonymous `envprefix:"KONG_"`
}
parser, restoreEnv := newEnvParser(t, &cli, envMap{"KONG_NUMBERS": "1,2,3"})
defer restoreEnv()
_, err := parser.Parse([]string{})
require.NoError(t, err)
require.Equal(t, []int{1, 2, 3}, cli.Slice)
}
func TestEnvarsNestedEnvPrefix(t *testing.T) {
type NestedAnonymous struct {
String string `env:"STRING"`
}
type Anonymous struct {
NestedAnonymous `envprefix:"ANON_"`
}
var cli struct {
Anonymous `envprefix:"KONG_"`
}
parser, restoreEnv := newEnvParser(t, &cli, envMap{"KONG_ANON_STRING": "abc"})
defer restoreEnv()
_, err := parser.Parse([]string{})
require.NoError(t, err)
require.Equal(t, "abc", cli.String)
}
func TestEnvarsWithDefault(t *testing.T) {
var cli struct {
Flag string `env:"KONG_FLAG" default:"default"`
}
parser, restoreEnv := newEnvParser(t, &cli, envMap{})
defer restoreEnv()
_, err := parser.Parse(nil)
require.NoError(t, err)
require.Equal(t, "default", cli.Flag)
parser, restoreEnv = newEnvParser(t, &cli, envMap{"KONG_FLAG": "moo"})
defer restoreEnv()
_, err = parser.Parse(nil)
require.NoError(t, err)
require.Equal(t, "moo", cli.Flag)
}
func TestEnv(t *testing.T) {
type Embed struct {
Flag string
}
type Cli struct {
One Embed `prefix:"one-" embed:""`
Two Embed `prefix:"two." embed:""`
Three Embed `prefix:"three_" embed:""`
Four Embed `prefix:"four_" embed:""`
Five bool
Six bool `env:"-"`
}
var cli Cli
expected := Cli{
One: Embed{Flag: "one"},
Two: Embed{Flag: "two"},
Three: Embed{Flag: "three"},
Four: Embed{Flag: "four"},
Five: true,
}
// With the prefix
parser, unsetEnvs := newEnvParser(t, &cli, envMap{
"KONG_ONE_FLAG": "one",
"KONG_TWO_FLAG": "two",
"KONG_THREE_FLAG": "three",
"KONG_FOUR_FLAG": "four",
"KONG_FIVE": "true",
"KONG_SIX": "true",
}, kong.DefaultEnvars("KONG"))
defer unsetEnvs()
_, err := parser.Parse(nil)
require.NoError(t, err)
require.Equal(t, expected, cli)
// Without the prefix
parser, unsetEnvs = newEnvParser(t, &cli, envMap{
"ONE_FLAG": "one",
"TWO_FLAG": "two",
"THREE_FLAG": "three",
"FOUR_FLAG": "four",
"FIVE": "true",
"SIX": "true",
}, kong.DefaultEnvars(""))
defer unsetEnvs()
_, err = parser.Parse(nil)
require.NoError(t, err)
require.Equal(t, expected, cli)
}
func TestJSONBasic(t *testing.T) {
type Embed struct {
String string
}
var cli struct {
String string
Slice []int
SliceWithCommas []string
Bool bool
One Embed `prefix:"one." embed:""`
Two Embed `prefix:"two." embed:""`
}
json := `{
"string": "🍕",
"slice": [5, 8],
"bool": true,
"slice_with_commas": ["a,b", "c"],
"one":{
"string": "one value"
},
"two.string": "two value"
}`
r, err := kong.JSON(strings.NewReader(json))
require.NoError(t, err)
parser := mustNew(t, &cli, kong.Resolvers(r))
_, err = parser.Parse([]string{})
require.NoError(t, err)
require.Equal(t, "🍕", cli.String)
require.Equal(t, []int{5, 8}, cli.Slice)
require.Equal(t, []string{"a,b", "c"}, cli.SliceWithCommas)
require.Equal(t, "one value", cli.One.String)
require.Equal(t, "two value", cli.Two.String)
require.True(t, cli.Bool)
}
type testUppercaseMapper struct{}
func (testUppercaseMapper) Decode(ctx *kong.DecodeContext, target reflect.Value) error {
var value string
err := ctx.Scan.PopValueInto("lowercase", &value)
if err != nil {
return err
}
target.SetString(strings.ToUpper(value))
return nil
}
func TestResolversWithMappers(t *testing.T) {
var cli struct {
Flag string `env:"KONG_MOO" type:"upper"`
}
restoreEnv := tempEnv(envMap{"KONG_MOO": "meow"})
defer restoreEnv()
parser := mustNew(t, &cli,
kong.NamedMapper("upper", testUppercaseMapper{}),
)
_, err := parser.Parse([]string{})
require.NoError(t, err)
require.Equal(t, "MEOW", cli.Flag)
}
func TestResolverWithBool(t *testing.T) {
var cli struct {
Bool bool
}
var resolver kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
if flag.Name == "bool" {
return true, nil
}
return nil, nil
}
p := mustNew(t, &cli, kong.Resolvers(resolver))
_, err := p.Parse(nil)
require.NoError(t, err)
require.True(t, cli.Bool)
}
func TestLastResolverWins(t *testing.T) {
var cli struct {
Int []int
}
var first kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
if flag.Name == "int" {
return 1, nil
}
return nil, nil
}
var second kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
if flag.Name == "int" {
return 2, nil
}
return nil, nil
}
p := mustNew(t, &cli, kong.Resolvers(first, second))
_, err := p.Parse(nil)
require.NoError(t, err)
require.Equal(t, []int{2}, cli.Int)
}
func TestResolverSatisfiesRequired(t *testing.T) {
var cli struct {
Int int `required`
}
var resolver kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
if flag.Name == "int" {
return 1, nil
}
return nil, nil
}
_, err := mustNew(t, &cli, kong.Resolvers(resolver)).Parse(nil)
require.NoError(t, err)
require.Equal(t, 1, cli.Int)
}
func TestResolverTriggersHooks(t *testing.T) {
ctx := &hookContext{}
var cli struct {
Flag hookValue
}
var first kong.ResolverFunc = func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
if flag.Name == "flag" {
return "one", nil
}
return nil, nil
}
_, err := mustNew(t, &cli, kong.Bind(ctx), kong.Resolvers(first)).Parse(nil)
require.NoError(t, err)
require.Equal(t, "one", string(cli.Flag))
require.Equal(t, []string{"before:", "after:one"}, ctx.values)
}
type validatingResolver struct {
err error
}
func (v *validatingResolver) Validate(app *kong.Application) error { return v.err }
func (v *validatingResolver) Resolve(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
return nil, nil
}
func TestValidatingResolverErrors(t *testing.T) {
resolver := &validatingResolver{err: errors.New("invalid")}
var cli struct{}
_, err := mustNew(t, &cli, kong.Resolvers(resolver)).Parse(nil)
require.EqualError(t, err, "invalid")
}