forked from cloudfoundry/go-envstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvstruct_test.go
482 lines (397 loc) · 12.5 KB
/
envstruct_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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package envstruct_test
import (
"errors"
"fmt"
"net/url"
"os"
"time"
envstruct "code.cloudfoundry.org/go-envstruct"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("envstruct", func() {
Describe("Load()", func() {
var (
ts LargeTestStruct
loadError error
envVars map[string]string
)
BeforeEach(func() {
ts = LargeTestStruct{}
ts.UnmarshallerPointer = &spyUnmarshaller{}
ts.UnmarshallerValue = spyUnmarshaller{}
envVars = make(map[string]string)
for k, v := range baseEnvVars {
envVars[k] = v
}
})
JustBeforeEach(func() {
for k, v := range envVars {
os.Setenv(k, v)
}
})
Context("when load is successful", func() {
JustBeforeEach(func() {
loadError = envstruct.Load(&ts)
})
AfterEach(func() {
for k := range envVars {
os.Setenv(k, "")
}
})
It("does not return an error", func() {
Expect(loadError).ToNot(HaveOccurred())
})
Context("with unmarshallers", func() {
It("passes the value to the pointer field", func() {
Expect(ts.UnmarshallerPointer.UnmarshalEnvInput).To(Equal("pointer"))
})
It("passes the value to the value field's address", func() {
Expect(ts.UnmarshallerValue.UnmarshalEnvInput).To(Equal("value"))
})
})
Context("with strings", func() {
It("populates the string thing", func() {
Expect(ts.StringThing).To(Equal("stringy thingy"))
})
})
Describe("case sensitiveity", func() {
It("populates the case sensitive thing", func() {
Expect(ts.CaseSensitiveThing).To(Equal("case sensitive"))
})
})
Context("with bools", func() {
Context("with 'true'", func() {
It("is true", func() {
Expect(ts.BoolThing).To(BeTrue())
})
})
Context("with 'false'", func() {
BeforeEach(func() {
envVars["BOOL_THING"] = "false"
})
It("is true", func() {
Expect(ts.BoolThing).To(BeFalse())
})
})
Context("with '1'", func() {
BeforeEach(func() {
envVars["BOOL_THING"] = "1"
})
It("is true", func() {
Expect(ts.BoolThing).To(BeTrue())
})
})
Context("with '0'", func() {
BeforeEach(func() {
envVars["BOOL_THING"] = "0"
})
It("is false", func() {
Expect(ts.BoolThing).To(BeFalse())
})
})
})
Context("with ints", func() {
It("populates the int thing", func() {
Expect(ts.IntThing).To(Equal(100))
})
It("populates the int 8 thing", func() {
Expect(ts.Int8Thing).To(Equal(int8(20)))
})
It("populates the int 16 thing", func() {
Expect(ts.Int16Thing).To(Equal(int16(2000)))
})
It("populates the int 32 thing", func() {
Expect(ts.Int32Thing).To(Equal(int32(200000)))
})
It("populates the int 64 thing", func() {
Expect(ts.Int64Thing).To(Equal(int64(200000000)))
})
})
Context("with uints", func() {
It("populates the uint thing", func() {
Expect(ts.UintThing).To(Equal(uint(100)))
})
It("populates the uint 8 thing", func() {
Expect(ts.Uint8Thing).To(Equal(uint8(20)))
})
It("populates the uint 16 thing", func() {
Expect(ts.Uint16Thing).To(Equal(uint16(2000)))
})
It("populates the uint 32 thing", func() {
Expect(ts.Uint32Thing).To(Equal(uint32(200000)))
})
It("populates the uint 64 thing", func() {
Expect(ts.Uint64Thing).To(Equal(uint64(200000000)))
})
})
Context("with floats", func() {
It("populates the float32 thing", func() {
Expect(ts.Float32Thing).To(Equal(float32(1.2345)))
})
It("populates the float64 thing", func() {
Expect(ts.Float64Thing).To(Equal(float64(9.8765)))
})
})
Context("with complex types", func() {
It("populates the complex64 thing", func() {
Expect(ts.Complex64Thing).To(Equal(complex64(1 + 2345i)))
})
It("populates the complex128 thing", func() {
Expect(ts.Complex128Thing).To(Equal(complex128(9 + 8765i)))
})
})
Context("with pointers to primatives", func() {
It("populates the pointer to string thing", func() {
Expect(*ts.PtrToString).To(Equal("pointy stringy thingy"))
})
It("populates the pointer to bool thing", func() {
Expect(*ts.PtrToBool).To(BeTrue())
})
It("populates the pointer to int thing", func() {
Expect(*ts.PtrToInt).To(Equal(int(20)))
})
It("populates the pointer to uint thing", func() {
Expect(*ts.PtrToUint).To(Equal(uint(20)))
})
})
Context("with comma separated strings", func() {
Context("slice of strings", func() {
It("populates a slice of strings", func() {
Expect(ts.StringSliceThing).To(Equal([]string{"one", "two", "three"}))
})
Context("with leading and trailing spaces", func() {
BeforeEach(func() {
envVars["STRING_SLICE_THING"] = "one , two , three"
})
It("populates a slice of strings", func() {
Expect(ts.StringSliceThing).To(Equal([]string{"one", "two", "three"}))
})
})
})
Context("slice of ints", func() {
It("populates a slice of ints", func() {
Expect(ts.IntSliceThing).To(Equal([]int{1, 2, 3}))
})
})
})
Context("with map[string]string", func() {
It("takes a comma separated list of key:value", func() {
Expect(ts.MapStringStringThing).To(Equal(map[string]string{
"key_one": "value_one",
"key_two": "value_two:with_colon",
}))
})
Context("when no value is given", func() {
BeforeEach(func() {
envVars["MAP_STRING_STRING_THING"] = "key"
})
It("returns an error if value is missing", func() {
Expect(loadError).To(MatchError("map[string]string key 'key' is missing a value"))
})
})
})
Context("with map[int]string", func() {
It("takes a comma separated list of key:value", func() {
Expect(ts.MapIntStringThing).To(Equal(map[int]string{
1: "value_one",
2: "value_two:with_colon",
}))
})
Context("when no value is given", func() {
BeforeEach(func() {
envVars["MAP_INT_STRING_THING"] = "2"
})
It("returns an error if value is missing", func() {
Expect(loadError).To(MatchError("map[int]string key '2' is missing a value"))
})
})
})
Context("with a sub struct contains env tags", func() {
It("populates the values of the substruct", func() {
Expect(ts.SubStruct.SubThingA).To(Equal("sub-string-a"))
Expect(ts.SubStruct.SubThingB).To(Equal(200))
})
It("populates the values of the pointer to substruct", func() {
Expect(ts.SubPointerStruct).ToNot(BeNil())
Expect(ts.SubPointerStruct.SubThingA).To(Equal("sub-string-a"))
Expect(ts.SubPointerStruct.SubThingB).To(Equal(200))
})
Describe("with default values", func() {
BeforeEach(func() {
ts.SubStruct = SubTestStruct{
SubThingA: "default-sub-a",
}
ts.SubPointerStruct = &SubTestStruct{
SubThingA: "default-sub-pointer-a",
}
envVars["SUB_THING_A"] = ""
})
It("maintains the default values", func() {
Expect(ts.SubStruct.SubThingA).To(Equal("default-sub-a"))
Expect(ts.SubPointerStruct.SubThingA).To(Equal("default-sub-pointer-a"))
})
})
})
Context("with duration struct", func() {
It("parses the duration string", func() {
Expect(ts.DurationThing).To(Equal(2 * time.Second))
})
})
Context("with url struct", func() {
It("parses the url string", func() {
Expect(ts.URLThing.Scheme).To(Equal("http"))
Expect(ts.URLThing.Host).To(Equal("github.com"))
Expect(ts.URLThing.Path).To(Equal("/some/path"))
})
})
})
Context("with defaults", func() {
It("honors default values if env var is empty", func() {
ts.DefaultThing = "Default Value"
Expect(envstruct.Load(&ts)).To(Succeed())
Expect(ts.DefaultThing).To(Equal("Default Value"))
})
})
Context("when load is unsuccessful", func() {
Context("when a required environment variable is not given", func() {
BeforeEach(func() {
envVars["REQUIRED_THING_A"] = ""
envVars["REQUIRED_THING_B"] = ""
})
It("includes all required environment variables in the error", func() {
loadError = envstruct.Load(&ts)
Expect(loadError).To(MatchError(fmt.Errorf("missing required environment variables: REQUIRED_THING_A, REQUIRED_THING_B")))
})
})
Context("when a required environment variable for substruct is not given", func() {
BeforeEach(func() {
envVars["SUB_THING_B"] = ""
})
It("returns a validation error", func() {
loadError = envstruct.Load(&ts)
Expect(loadError).To(MatchError(fmt.Errorf("missing required environment variables: SUB_THING_B")))
})
})
Context("when top level and substruct are missing required arguments", func() {
BeforeEach(func() {
envVars["REQUIRED_THING_A"] = ""
envVars["SUB_THING_B"] = ""
})
It("returns an error with both environment variables", func() {
loadError = envstruct.Load(&ts)
Expect(loadError).To(MatchError(fmt.Errorf("missing required environment variables: REQUIRED_THING_A, SUB_THING_B")))
})
})
Context("with an invalid int", func() {
BeforeEach(func() {
envVars["INT_THING"] = "Hello!"
})
It("returns an error", func() {
Expect(envstruct.Load(&ts)).ToNot(Succeed())
})
})
Context("with an invalid uint", func() {
BeforeEach(func() {
envVars["UINT_THING"] = "Hello!"
})
It("returns an error", func() {
Expect(envstruct.Load(&ts)).ToNot(Succeed())
})
})
Context("with a failing unmarshaller pointer", func() {
BeforeEach(func() {
ts.UnmarshallerPointer.UnmarshalEnvOutput = errors.New("failed to unmarshal")
})
It("returns an error", func() {
Expect(envstruct.Load(&ts)).ToNot(Succeed())
})
})
Context("with a failing unmarshaller value", func() {
BeforeEach(func() {
ts.UnmarshallerValue.UnmarshalEnvOutput = errors.New("failed to unmarshal")
})
It("returns an error", func() {
Expect(envstruct.Load(&ts)).ToNot(Succeed())
})
})
Context("with a missing unmarshaller on struct with an env tag", func() {
var withoutMarhsaller SmallTestStructWithSubStructWithoutMarshaller
BeforeEach(func() {
withoutMarhsaller = SmallTestStructWithSubStructWithoutMarshaller{}
})
It("returns an error", func() {
Expect(envstruct.Load(&withoutMarhsaller)).ToNot(Succeed())
})
})
Context("with an unsupported type", func() {
var withUnsupportedType WithUnsupportedTypeStruct
BeforeEach(func() {
envVars["UNSUPPORTED"] = "unsupported"
withUnsupportedType = WithUnsupportedTypeStruct{}
})
It("returns an error", func() {
Expect(envstruct.Load(&withUnsupportedType)).To(MatchError("unsupported type uintptr"))
})
})
})
})
Describe("ToEnv", func() {
It("returns a slice of strings formatted as KEY=value", func() {
url, err := url.Parse("https://example.com")
Expect(err).ToNot(HaveOccurred())
ts := ToEnvTestStruct{
HiddenThing: "hidden-thing",
StringThing: "string-thing",
BoolThing: true,
IntThing: 200,
URLThing: url,
StringSliceThing: []string{"thing-1", "thing-2", "thing-3"},
CaseSensitiveThing: "case-sensitive-thing",
SubStruct: SubTestStruct{
SubThingA: "sub-string-a",
SubThingB: 300,
},
SubPointerStruct: &SubTestStruct{
SubThingA: "sub-pointer-thing-a",
SubThingB: 500,
},
}
for k, v := range baseEnvVars {
os.Setenv(k, v)
}
ret := envstruct.ToEnv(&ts)
Expect(ret).To(ConsistOf(
"HIDDEN_THING=hidden-thing",
"STRING_THING=string-thing",
"BOOL_THING=true",
"INT_THING=200",
"URL_THING=https://example.com",
"STRING_SLICE_THING=thing-1,thing-2,thing-3",
"CaSe_SeNsItIvE_ThInG=case-sensitive-thing",
"SUB_THING_A=sub-string-a",
"SUB_THING_B=300",
"SUB_THING_A=sub-pointer-thing-a",
"SUB_THING_B=500",
))
})
Context("with a map", func() {
It("returns a slice with a formatted map for environment variable", func() {
ts := ToEnvMapTestStruct{
MapStringStringThing: map[string]string{
"key_one": "value_one",
"key_two": "value_two",
},
}
for k, v := range baseEnvVars {
os.Setenv(k, v)
}
ret := envstruct.ToEnv(&ts)
Expect(ret[0]).To(ContainSubstring("MAP_STRING_STRING_THING="))
Expect(ret[0]).To(ContainSubstring("key_one:value_one"))
Expect(ret[0]).To(ContainSubstring(","))
Expect(ret[0]).To(ContainSubstring("key_two:value_two"))
})
})
})
})