-
Notifications
You must be signed in to change notification settings - Fork 9
/
config_test.go
378 lines (348 loc) · 8.43 KB
/
config_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
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
package konf_test
import (
"testing"
"time"
"github.com/nil-go/konf"
"github.com/nil-go/konf/internal/assert"
"github.com/nil-go/konf/provider/env"
)
func TestConfig_nil(t *testing.T) {
var config *konf.Config
assert.True(t, !config.Exists([]string{"key"}))
assert.Equal(t, "key has no configuration.\n\n", config.Explain("key"))
var value string
assert.NoError(t, config.Unmarshal("key", &value))
assert.Equal(t, "", value)
assert.True(t, len(config.Explain("key")) > 0)
config = konf.New()
assert.True(t, !config.Exists([]string{"key"}))
assert.Equal(t, "key has no configuration.\n\n", config.Explain("key"))
assert.NoError(t, config.Unmarshal("key", &value))
assert.Equal(t, "", value)
assert.True(t, len(config.Explain("key")) > 0)
}
func TestConfig_Load(t *testing.T) {
t.Parallel()
testcases := []struct {
description string
loader konf.Loader
err string
}{
{
description: "error",
loader: &errorLoader{},
err: "load configuration: load error",
},
{
description: "nil loader",
},
}
for _, testcase := range testcases {
t.Run(testcase.description, func(t *testing.T) {
t.Parallel()
var config konf.Config
err := config.Load(testcase.loader)
if testcase.err == "" {
assert.NoError(t, err)
} else {
assert.Equal(t, testcase.err, err.Error())
}
})
}
}
func TestConfig_Unmarshal(t *testing.T) {
t.Parallel()
testcases := []struct {
description string
opts []konf.Option
loaders []konf.Loader
assert func(*konf.Config)
}{
{
description: "empty values",
assert: func(config *konf.Config) {
var value string
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, "", value)
},
},
{
description: "for primary type",
loaders: []konf.Loader{mapLoader{"config": "string"}},
assert: func(config *konf.Config) {
var value string
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, "string", value)
},
},
{
description: "config for map",
loaders: []konf.Loader{mapLoader{"Config": "struct"}},
assert: func(config *konf.Config) {
var value map[string]string
assert.NoError(t, config.Unmarshal("", &value))
assert.Equal(t, "struct", value["config"])
},
},
{
description: "config for map (case sensitive)",
loaders: []konf.Loader{mapLoader{"Config": "struct"}},
opts: []konf.Option{konf.WithMapKeyCaseSensitive()},
assert: func(config *konf.Config) {
var value map[string]string
assert.NoError(t, config.Unmarshal("", &value))
assert.Equal(t, "struct", value["Config"])
},
},
{
description: "config for struct",
loaders: []konf.Loader{mapLoader{"config": "struct"}},
assert: func(config *konf.Config) {
var value struct {
Config string
}
assert.NoError(t, config.Unmarshal("", &value))
assert.Equal(t, "struct", value.Config)
},
},
{
description: "config for struct (case sensitive)",
opts: []konf.Option{konf.WithCaseSensitive()},
loaders: []konf.Loader{mapLoader{"ConfigValue": "struct"}},
assert: func(config *konf.Config) {
var value struct {
ConfigValue string
Configvalue string
}
assert.NoError(t, config.Unmarshal("", &value))
assert.Equal(t, "struct", value.ConfigValue)
assert.Equal(t, "", value.Configvalue)
},
},
{
description: "default delimiter",
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "string",
},
},
},
assert: func(config *konf.Config) {
var value string
assert.NoError(t, config.Unmarshal("config.nest", &value))
assert.Equal(t, "string", value)
},
},
{
description: "customized delimiter",
opts: []konf.Option{
konf.WithDelimiter("_"),
},
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "string",
},
},
},
assert: func(config *konf.Config) {
var value string
assert.NoError(t, config.Unmarshal("config_nest", &value))
assert.Equal(t, "string", value)
},
},
{
description: "decode hook",
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "sky",
},
},
},
assert: func(config *konf.Config) {
var value struct {
N Enum `konf:"nest"`
}
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, Sky, value.N)
},
},
{
description: "customized decode hook",
opts: []konf.Option{
konf.WithDecodeHook[string, time.Duration](time.ParseDuration),
},
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "1s",
},
},
},
assert: func(config *konf.Config) {
var value struct {
N time.Duration `konf:"nest"`
}
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, time.Second, value.N)
},
},
{
description: "tag name",
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "string",
},
},
},
assert: func(config *konf.Config) {
var value struct {
N string `konf:"nest"`
}
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, "string", value.N)
},
},
{
description: "customized tag name",
opts: []konf.Option{
konf.WithTagName("test"),
},
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "a,b,c",
},
},
},
assert: func(config *konf.Config) {
var value struct {
N []string `test:"nest"`
}
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, []string{"a", "b", "c"}, value.N)
},
},
{
description: "non string key",
loaders: []konf.Loader{
mapLoader{
"config": map[int]any{
1: "string",
},
},
},
assert: func(config *konf.Config) {
var value string
assert.NoError(t, config.Unmarshal("config.nest", &value))
assert.Equal(t, "", value)
},
},
}
for _, testcase := range testcases {
t.Run(testcase.description, func(t *testing.T) {
t.Parallel()
var config konf.Config
if len(testcase.opts) > 0 {
config = *konf.New(testcase.opts...)
}
for _, loader := range testcase.loaders {
assert.NoError(t, config.Load(loader))
}
testcase.assert(&config)
})
}
}
func TestConfigCopyPanic(t *testing.T) {
defer func() {
assert.Equal(t, recover(), "illegal use of non-zero Config copied by value")
}()
var config konf.Config
assert.NoError(t, config.Load(mapLoader{}))
configCopy := config //nolint:govet
assert.NoError(t, configCopy.Load(mapLoader{}))
t.Fail()
}
type mapLoader map[string]any
func (m mapLoader) Load() (map[string]any, error) {
return m, nil
}
func (m mapLoader) String() string {
return "map"
}
func TestConfig_Explain(t *testing.T) {
t.Parallel()
var config konf.Config
err := config.Load(env.New())
assert.NoError(t, err)
err = config.Load(mapLoader{
"config": map[string]any{"nest": "env"},
})
assert.NoError(t, err)
err = config.Load(mapLoader{
"number": 123,
"password": "password",
"key": []byte("AKIA9SKKLKSKKSKKSKK8"),
"config": map[string]any{"nest": "map"},
})
assert.NoError(t, err)
testcases := []struct {
description string
path string
expected string
}{
{
description: "non-exist",
path: "non-exist",
expected: "non-exist has no configuration.\n\n",
},
{
description: "number",
path: "number",
expected: "number has value[123] that is loaded by loader[map].\n\n",
},
{
description: "password",
path: "password",
expected: "password has value[******] that is loaded by loader[map].\n\n",
},
{
description: "API key",
path: "key",
expected: "key has value[AWS API Key] that is loaded by loader[map].\n\n",
},
{
description: "config",
path: "config",
expected: `config.nest has value[map] that is loaded by loader[map].
Here are other value(loader)s:
- env(map)
`,
},
}
for _, testcase := range testcases {
t.Run(testcase.description, func(t *testing.T) {
t.Parallel()
assert.Equal(t, testcase.expected, config.Explain(testcase.path))
})
}
}
type Enum int
const (
Unknown Enum = iota
Sky
)
func (e *Enum) UnmarshalText(text []byte) error {
switch string(text) {
case "sky":
*e = Sky
default:
*e = Unknown
}
return nil
}