-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
330 lines (293 loc) · 9.34 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
package main_test
import (
"os"
"testing"
main "github.com/Lidbetter/go-samples"
)
func TestFillEnvTagsSimple(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117"`
FooBool bool `env:"TEST_ENV_FOO_117"`
FooInt int `env:"TEST_ENV_FOO_117"`
FooInt8 int8 `env:"TEST_ENV_FOO_117"`
FooInt16 int16 `env:"TEST_ENV_FOO_117"`
FooInt32 int32 `env:"TEST_ENV_FOO_117"`
FooInt64 int64 `env:"TEST_ENV_FOO_117"`
FooUint uint `env:"TEST_ENV_FOO_117"`
FooUint8 uint8 `env:"TEST_ENV_FOO_117"`
FooUint16 uint16 `env:"TEST_ENV_FOO_117"`
FooUint32 uint32 `env:"TEST_ENV_FOO_117"`
FooUint64 uint64 `env:"TEST_ENV_FOO_117"`
}{}
err := main.FillEnvTags(&conf)
if err != nil {
t.Fatalf("error filling conf struct: %v", err)
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
if conf.FooBool != true {
t.Fatalf("FooBool expected 'true', actual '%t'", conf.FooBool)
}
if conf.FooInt != int(117) {
t.Fatalf("FooInt expected '117', actual '%d'", conf.FooInt)
}
if conf.FooInt8 != int8(117) {
t.Fatalf("FooInt8 expected '117', actual '%d'", conf.FooInt8)
}
if conf.FooInt16 != int16(117) {
t.Fatalf("FooInt16 expected '117', actual '%d'", conf.FooInt16)
}
if conf.FooInt32 != int32(117) {
t.Fatalf("FooInt32 expected '117', actual '%d'", conf.FooInt32)
}
if conf.FooInt64 != int64(117) {
t.Fatalf("FooInt64 expected '117', actual '%d'", conf.FooInt64)
}
if conf.FooUint != uint(117) {
t.Fatalf("FooUint expected '117', actual '%d'", conf.FooUint)
}
if conf.FooUint8 != uint8(117) {
t.Fatalf("FooUint8 expected '117', actual '%d'", conf.FooUint8)
}
if conf.FooUint16 != uint16(117) {
t.Fatalf("FooUint16 expected '117', actual '%d'", conf.FooUint16)
}
if conf.FooUint32 != uint32(117) {
t.Fatalf("FooUint32 expected '117', actual '%d'", conf.FooUint32)
}
if conf.FooUint64 != uint64(117) {
t.Fatalf("FooUint64 expected '117', actual '%d'", conf.FooUint64)
}
}
func TestFillEnvTagsRequired(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117" required:"true"`
FooStringRequired string `env:"TEST_ENV_FOO_117______MISSING" required:"true"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when an environment variable is marked as required, but is not set")
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
}
func TestFillEnvTagsRequiredFalse(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117"`
FooStringNotRequired1 string `env:"TEST_ENV_FOO_117______MISSING"`
FooStringNotRequired2 string `env:"TEST_ENV_FOO_117______MISSING" required:""`
FooStringNotRequired3 string `env:"TEST_ENV_FOO_117______MISSING" required:"0"`
FooStringNotRequired4 string `env:"TEST_ENV_FOO_117______MISSING" required:"false"`
FooStringNotRequired5 string `env:"TEST_ENV_FOO_117______MISSING" required:"FALSE"`
}{}
err := main.FillEnvTags(&conf)
if err != nil {
t.Fatalf("error filling conf struct: %v", err)
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
}
func TestFillEnvTagsOverridesExistingValue(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117"`
}{
FooString: "existing value",
}
err := main.FillEnvTags(&conf)
if err != nil {
t.Fatalf("error filling conf struct: %v", err)
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
}
func TestFillEnvTagsBool(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
_ = os.Setenv("TEST_ENV_FOO_0", "0")
_ = os.Setenv("TEST_ENV_FOO_EMPTY", "")
_ = os.Setenv("TEST_ENV_FOO_FALSE", "false")
_ = os.Setenv("TEST_ENV_FOO_FALSE2", "FALSE")
conf := struct {
FooBoolTrue bool `env:"TEST_ENV_FOO_117"`
FooBoolFalse bool `env:"TEST_ENV_FOO_117______MISSING"`
FooBoolFalse1 bool `env:"TEST_ENV_FOO_0"`
FooBoolFalse2 bool `env:"TEST_ENV_FOO_EMPTY"`
FooBoolFalse3 bool `env:"TEST_ENV_FOO_FALSE"`
FooBoolFalse4 bool `env:"TEST_ENV_FOO_FALSE2"`
}{}
err := main.FillEnvTags(&conf)
if err != nil {
t.Fatalf("error filling conf struct: %v", err)
}
if conf.FooBoolTrue != true {
t.Fatalf("FooBoolTrue expected 'true', actual '%t'", conf.FooBoolTrue)
}
if conf.FooBoolFalse != false {
t.Fatalf("FooBoolFalse expected 'false', actual '%t'", conf.FooBoolFalse)
}
if conf.FooBoolFalse1 != false {
t.Fatalf("FooBoolFalse1 expected 'false', actual '%t'", conf.FooBoolFalse1)
}
if conf.FooBoolFalse2 != false {
t.Fatalf("FooBoolFalse2 expected 'false', actual '%t'", conf.FooBoolFalse2)
}
if conf.FooBoolFalse3 != false {
t.Fatalf("FooBoolFalse3 expected 'false', actual '%t'", conf.FooBoolFalse3)
}
if conf.FooBoolFalse4 != false {
t.Fatalf("FooBoolFalse4 expected 'false', actual '%t'", conf.FooBoolFalse4)
}
}
func TestFillEnvTagsOverflow8(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_1177", "1177")
conf := struct {
FooInt8 int8 `env:"TEST_ENV_FOO_1177"`
}{}
conf2 := struct {
FooUint8 uint8 `env:"TEST_ENV_FOO_1177"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when setting int8 to 1177 (overflow)")
}
err2 := main.FillEnvTags(&conf2)
if err2 == nil {
t.Fatalf("conf struct should error when setting uint8 to 1177 (overflow)")
}
}
func TestFillEnvTagsOverflow16(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117766", "117766")
conf := struct {
FooInt16 int16 `env:"TEST_ENV_FOO_117766"`
}{}
conf2 := struct {
FooUint16 uint16 `env:"TEST_ENV_FOO_117766"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when setting int16 to 117766 (overflow)")
}
err2 := main.FillEnvTags(&conf2)
if err2 == nil {
t.Fatalf("conf struct should error when setting uint16 to 117766 (overflow)")
}
}
func TestFillEnvTagsOverflow32(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117766554433", "117766554433")
conf := struct {
FooInt32 int32 `env:"TEST_ENV_FOO_117766554433"`
}{}
conf2 := struct {
FooUint32 uint32 `env:"TEST_ENV_FOO_117766554433"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when setting int32 to 117766554433 (overflow)")
}
err2 := main.FillEnvTags(&conf2)
if err2 == nil {
t.Fatalf("conf struct should error when setting uint32 to 117766554433 (overflow)")
}
}
func TestFillEnvTagsOverflow64(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_1177665544332211001122", "1177665544332211001122")
conf := struct {
FooInt64 int64 `env:"TEST_ENV_FOO_1177665544332211001122"`
}{}
conf2 := struct {
FooUint64 uint64 `env:"TEST_ENV_FOO_1177665544332211001122"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when setting int64 to 1177665544332211001122 (overflow)")
}
err2 := main.FillEnvTags(&conf2)
if err2 == nil {
t.Fatalf("conf struct should error when setting uint32 to 1177665544332211001122 (overflow)")
}
}
func TestFillEnvTagsIgnoresOtherFields(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117"`
Bar string
baz string
}{}
err := main.FillEnvTags(&conf)
if err != nil {
t.Fatalf("error filling conf struct: %v", err)
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
if conf.Bar != "" {
t.Fatalf("FooString expected '', actual '%s'", conf.Bar)
}
}
func TestFillEnvTagsErrorsOnUnexportedFields(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117"`
foo string `env:"TEST_ENV_FOO_117"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when unexpored field has env tag")
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
}
func TestFillEnvTagsErrorsOnUnsupportedTypes(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct {
FooString string `env:"TEST_ENV_FOO_117"`
FooStruct struct{} `env:"TEST_ENV_FOO_117"`
}{}
err := main.FillEnvTags(&conf)
if err == nil {
t.Fatalf("conf struct should error when unsupported field has env tag")
}
if conf.FooString != "117" {
t.Fatalf("FooString expected '117', actual '%s'", conf.FooString)
}
}
func TestFillEnvTagsHandlesBadInput(t *testing.T) {
_ = os.Setenv("TEST_ENV_FOO_117", "117")
conf := struct{}{}
err := main.FillEnvTags(conf)
if err == nil {
t.Fatalf("FillEnvTags should error when passed in non struct ptr")
}
// nil ptr
var conf1 *[]byte = nil
err1 := main.FillEnvTags(conf1)
if err1 == nil {
t.Fatalf("FillEnvTags should error when passed in non struct ptr")
}
conf2 := int(1)
err2 := main.FillEnvTags(conf2)
if err2 == nil {
t.Fatalf("FillEnvTags should error when passed in non struct ptr")
}
err3 := main.FillEnvTags(&conf2)
if err3 == nil {
t.Fatalf("FillEnvTags should error when passed in non struct ptr")
}
var conf4 error = nil
err4 := main.FillEnvTags(conf4)
if err4 == nil {
t.Fatalf("FillEnvTags should error when passed in non struct ptr")
}
err5 := main.FillEnvTags(&conf4)
if err5 == nil {
t.Fatalf("FillEnvTags should error when passed in non struct ptr")
}
}