-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvconf_test.go
380 lines (324 loc) · 11 KB
/
envconf_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
379
380
package envconf
import (
"math"
"os"
"reflect"
"testing"
"time"
)
type Config struct {
Mongo MongoConfig `env:"mongo"`
AppIDList []string `env:"app_id_list"`
Replicas uint `env:"replicas"`
EmbeddedConfig `env:",inline"`
Duration time.Duration `env:"duration"`
I64 int64 `env:"i64"`
F64 float64 `env:"f64"`
unexported string
UnLoadedFloat64 float64 `env:"unloadedfloat64"`
UnLoadedInt int `env:"unloadedint"`
UnLoadedUint uint `env:"unloadeduint"`
UnLoadedBool bool `env:"unloadedbool"`
UnLoadedStr string `env:"unloadedstring"`
UnLoadedStrSlice []string `env:"unloadedstrslice"`
UnLoadedDuration time.Duration `env:"unloadedduration"`
}
type MongoConfig struct {
Nodes string `env:"nodes"`
Database string `env:"db"`
ReplicaSet string `env:"replicaset"`
Port int `env:"port"`
Debug bool `env:"debug"`
}
type EmbeddedConfig struct {
StringInEmbeddedStructure string `env:"string_in_embedded_structure"`
IntInEmbeddedStructure int `env:"int_in_embedded_structure"`
}
func assertEqual(t testing.TB, fieldName string, expected, received interface{}) {
if !reflect.DeepEqual(expected, received) {
t.Helper()
t.Errorf("%v is not loaded correctly.\nExpecting: %v %v\nReceived: %v %v",
fieldName,
reflect.TypeOf(expected), expected,
reflect.TypeOf(received), received,
)
}
}
func assertPanic(t testing.TB) {
if r := recover(); r == nil {
t.Helper()
t.Errorf("this test should panic")
}
}
func TestLoad(t *testing.T) {
os.Setenv("TEST_MONGO_NODES", "www.example.com")
os.Setenv("TEST_MONGO_PORT", "332")
os.Setenv("TEST_MONGO_DEBUG", "false")
os.Setenv("TEST_APP_ID_LIST", " aa, bb ,cc ,dd")
os.Setenv("TEST_REPLICAS", "3")
os.Setenv("TEST_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_INT_IN_EMBEDDED_STRUCTURE", "19")
os.Setenv("TEST_DURATION", "10m")
os.Setenv("TEST_I64", "600")
os.Setenv("TEST_F64", "3.14")
initConfig := Config{
unexported: "unexported string",
UnLoadedFloat64: math.MaxFloat64,
UnLoadedInt: -5,
UnLoadedUint: 9,
UnLoadedBool: true,
UnLoadedStr: "unloaded string",
UnLoadedStrSlice: []string{"some", "random", "str"},
}
config := initConfig
Load("TEST", &config)
assertEqual(t, "Mongo.Port", 332, config.Mongo.Port)
assertEqual(t, "Mongo.Nodes", "www.example.com", config.Mongo.Nodes)
assertEqual(t, "Mongo.Database", "", config.Mongo.Database)
assertEqual(t, "Mongo.Debug", false, config.Mongo.Debug)
assertEqual(t, "Replicas", uint(3), config.Replicas)
assertEqual(t, "AppIDList", []string{"aa", "bb", "cc", "dd"}, config.AppIDList)
assertEqual(t, "StringInEmbeddedStrcuture", "a-z", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStrcuture", 19, config.IntInEmbeddedStructure)
assertEqual(t, "Duration", time.Minute*10, config.Duration)
assertEqual(t, "int64", int64(600), config.I64)
assertEqual(t, "float64", 3.14, config.F64)
assertEqual(t, "unexported", initConfig.unexported, config.unexported)
assertEqual(t, "UnLoadedFloat64", initConfig.UnLoadedFloat64, config.UnLoadedFloat64)
assertEqual(t, "UnLoadedInt", initConfig.UnLoadedInt, config.UnLoadedInt)
assertEqual(t, "UnLoadedUint", initConfig.UnLoadedUint, config.UnLoadedUint)
assertEqual(t, "UnLoadedBool", initConfig.UnLoadedBool, config.UnLoadedBool)
assertEqual(t, "UnLoadedStr", initConfig.UnLoadedStr, config.UnLoadedStr)
assertEqual(t, "UnLoadedStrSlice", initConfig.UnLoadedStrSlice, config.UnLoadedStrSlice)
}
func TestTaggedUnsupportedTypeShouldPanic(t *testing.T) {
defer assertPanic(t)
type Invalid struct {
Unsupported float32 `env:"unsupported"`
}
os.Setenv("FAIL_UNSUPPORTED", "55.66")
var invalid Invalid
Load("FAIL", &invalid)
}
func TestUnsupportedSliceShouldPanic(t *testing.T) {
defer assertPanic(t)
type Invalid struct {
Unsupported []map[string]string
}
os.Setenv("FAIL_UNSUPPORTED", "[]")
var invalid Invalid
Load("FAIL", &invalid)
}
func TestInvalidIntShouldPanic(t *testing.T) {
defer assertPanic(t)
type InvalidInt struct {
InvalidInt int `env:"invalidint"`
}
os.Setenv("FAIL_INVALIDINT", "not a int")
var inv InvalidInt
Load("FAIL", &inv)
}
func TestInvalidDurationShouldPanic(t *testing.T) {
defer assertPanic(t)
type InvalidDuration struct {
InvalidDuration time.Duration `env:"invalidduration"`
}
os.Setenv("FAIL_INVALIDDURATION", "not a duration")
var inv InvalidDuration
Load("FAIL", &inv)
}
func TestInvalidUintShouldPanic(t *testing.T) {
defer assertPanic(t)
type InvalidUint struct {
InvalidUint uint `env:"invaliduint"`
}
os.Setenv("FAIL_INVALIDUINT", "-2")
var inv InvalidUint
Load("FAIL", &inv)
}
func TestInvalidBoolShouldPanic(t *testing.T) {
defer assertPanic(t)
type InvalidBool struct {
InvalidBool bool `env:"invalidbool"`
}
os.Setenv("FAIL_INVALIDBOOL", "ture")
var inv InvalidBool
Load("FAIL", &inv)
}
func TestInvalidFloatShouldPanic(t *testing.T) {
defer assertPanic(t)
type InvalidFloat struct {
InvalidFloat float64 `env:"invalidfloat"`
}
os.Setenv("FAIL_INVALIDFLOAT", "not a float")
var inv InvalidFloat
Load("FAIL", &inv)
}
func TestInlineWithoutComma(t *testing.T) {
os.Setenv("TEST_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_INT_IN_EMBEDDED_STRUCTURE", "19")
// no comma
config := struct {
EmbeddedConfig `env:"inline"`
}{}
Load("TEST", &config)
assertEqual(t, "StringInEmbeddedStructure", "", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 0, config.IntInEmbeddedStructure)
}
func TestInlineWithTagName(t *testing.T) {
os.Setenv("TEST_STR", "outer")
os.Setenv("TEST_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_INT_IN_EMBEDDED_STRUCTURE", "19")
os.Setenv("TEST_STRING_IN_STRUCTURE", "in struct")
os.Setenv("TEST_INT_IN_STRUCTURE", "1239")
// inline option ignores tag name
type structure struct {
StringInStructure string `env:"string_in_structure"`
IntInStructure int `env:"int_in_structure"`
}
config := struct {
Str string `env:"str"`
EmbeddedConfig `env:"str,inline"`
Struct structure `env:"str,inline"`
}{}
Load("TEST", &config)
assertEqual(t, "Str", "outer", config.Str)
assertEqual(t, "StringInEmbeddedStructure", "a-z", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 19, config.IntInEmbeddedStructure)
assertEqual(t, "StringInStructure", "in struct", config.Struct.StringInStructure)
assertEqual(t, "IntInStructure", 1239, config.Struct.IntInStructure)
}
func TestInlineWithoutStruct(t *testing.T) {
defer assertPanic(t)
config := struct {
Str string `env:",inline"`
}{}
Load("TEST", &config)
}
func TestEmbeddedStructureWithoutInline(t *testing.T) {
os.Setenv("TEST_EMBEDDED_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_EMBEDDED_INT_IN_EMBEDDED_STRUCTURE", "19")
configWithTag := struct {
EmbeddedConfig `env:"embedded"`
}{}
Load("TEST", &configWithTag)
assertEqual(t, "StringInEmbeddedStructure", "a-z", configWithTag.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 19, configWithTag.IntInEmbeddedStructure)
}
func TestIgnoringSpecificTag(t *testing.T) {
// invalid
os.Setenv("TEST_-", "panics")
// ignored
os.Setenv("TEST_IGNOREDSTRING", "ignored")
os.Setenv("TEST_IGNOREDINT", "123")
os.Setenv("TEST_IGNOREDSTRUCT_IGNOREDSTRINGINSTRUCT", "ignored")
os.Setenv("TEST_IGNOREDSTRUCT_IGNOREDINTINSTRUCT", "12345")
os.Setenv("TEST_EMBEDDEDCONFIG_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_EMBEDDEDCONFIG_INT_IN_EMBEDDED_STRUCTURE", "19")
config := struct {
IgnoredString string `env:"-"`
IgnoredInt int `env:"-"`
IgnoredStruct struct {
IgnoredStringInStruct string
IgnoredIntInStruct int
} `env:"-"`
EmbeddedConfig `env:"-"`
}{}
Load("TEST", &config)
assertEqual(t, "IgnoredString", "", config.IgnoredString)
assertEqual(t, "IgnoredInt", 0, config.IgnoredInt)
assertEqual(t, "IgnoredStringInStruct", "", config.IgnoredStruct.IgnoredStringInStruct)
assertEqual(t, "IgnoredIntInStruct", 0, config.IgnoredStruct.IgnoredIntInStruct)
assertEqual(t, "StringInEmbeddedStructure", "", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 0, config.IntInEmbeddedStructure)
}
func TestNoTag(t *testing.T) {
os.Setenv("TEST_AUTONAMEDSTRING", "auto named")
os.Setenv("TEST_AUTONAMEDINT", "321")
os.Setenv("TEST_AUTONAMEDSTRUCT_AUTONAMEDSTRINGINSTRUCT", "auto named in struct")
os.Setenv("TEST_AUTONAMEDSTRUCT_AUTONAMEDINTINSTRUCT", "54321")
os.Setenv("TEST_EMBEDDEDCONFIG_STRING_IN_EMBEDDED_STRUCTURE", "a-z")
os.Setenv("TEST_EMBEDDEDCONFIG_INT_IN_EMBEDDED_STRUCTURE", "19")
config := struct {
AutoNamedString string
AutoNamedInt int
AutoNamedStruct struct {
AutoNamedStringInStruct string
AutoNamedIntInStruct int
}
EmbeddedConfig
}{}
Load("TEST", &config)
assertEqual(t, "AutoNamedString", "auto named", config.AutoNamedString)
assertEqual(t, "AutoNamedInt", 321, config.AutoNamedInt)
assertEqual(t, "AutoNamedStringInStruct", "auto named in struct", config.AutoNamedStruct.AutoNamedStringInStruct)
assertEqual(t, "AutoNamedIntInStruct", 54321, config.AutoNamedStruct.AutoNamedIntInStruct)
assertEqual(t, "StringInEmbeddedStructure", "a-z", config.StringInEmbeddedStructure)
assertEqual(t, "IntInEmbeddedStructure", 19, config.IntInEmbeddedStructure)
}
func TestDuplicatedKeys(t *testing.T) {
defer assertPanic(t)
os.Setenv("TEST_DUPLICATED_STRING", "duplication")
config := struct {
Str string `env:"duplicated_string"`
Struct struct {
Str string `env:"string"`
} `env:"duplicated"`
}{}
Load("TEST", &config)
}
func TestDuplicatedKeysBetweenInlineStructs(t *testing.T) {
defer assertPanic(t)
os.Setenv("TEST_DUPLICATED_STRING", "duplication")
type em1 struct {
Str1 string `env:"duplicated_string"`
}
type em2 struct {
Str2 string `env:"duplicated_string"`
}
config := struct {
em1 `env:",inline"`
em2 `env:",inline"`
}{}
Load("TEST", &config)
}
func TestDuplicatedKeysBetweenStructs(t *testing.T) {
defer assertPanic(t)
os.Setenv("TEST_DUPLICATED_STRING", "duplication")
type em1 struct {
Str1 string `env:"duplicated_string"`
}
type em2 struct {
Str2 string `env:"string"`
}
config := struct {
EM1 em1 `env:"em"`
EM2 em2 `env:"em_duplicated"`
}{}
Load("TEST", &config)
}
func TestLogger(t *testing.T) {
os.Setenv("TEST_INTEGER", "-3")
os.Setenv("TEST_UNSIGNED_INTEGER", "3")
result := map[string]*EnvStatus{}
config := struct {
String string `env:"string"`
Integer int `env:"integer"`
Struct struct {
Unsigned uint `env:"unsigned_integer"`
Bool bool `env:"bool"`
StrSlice []string `env:"string_slice"`
} `env:",inline"`
}{}
Load("TEST", &config, CustomHandleEnvVarsOption(func(status map[string]*EnvStatus) {
result = status
}))
expected := map[string]*EnvStatus{
"TEST_STRING": {false},
"TEST_INTEGER": {true},
"TEST_UNSIGNED_INTEGER": {true},
"TEST_BOOL": {false},
"TEST_STRING_SLICE": {false},
}
assertEqual(t, "", expected, result)
}