-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
291 lines (282 loc) · 7.8 KB
/
env_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
package struct2env
import (
"reflect"
"strings"
"testing"
"time"
)
func TestSplitByCase(t *testing.T) {
tests := []struct {
in string
out []string
}{
{"", nil},
{"http2Server", []string{"http2", "Server"}},
{"HTTPSServer42", []string{"HTTPS", "Server42"}},
{"1", []string{"1"}},
{"1a", []string{"1a"}},
{"1a2Bb", []string{"1a2", "Bb"}}, // note 1a2B doesn't split
{"a", []string{"a"}},
{"A", []string{"A"}},
{"Ab", []string{"Ab"}},
{"AB", []string{"AB"}},
{"AB", []string{"AB"}},
{"ABC", []string{"ABC"}},
{"ABCd", []string{"AB", "Cd"}},
{"aa", []string{"aa"}},
{"aaA", []string{"aa", "A"}},
{"AAb", []string{"A", "Ab"}},
{"aaBbbCcc", []string{"aa", "Bbb", "Ccc"}},
{"AaBbbCcc", []string{"Aa", "Bbb", "Ccc"}},
{"AABbbCcc", []string{"AA", "Bbb", "Ccc"}},
}
for _, test := range tests {
got := SplitByCase(test.in)
if !reflect.DeepEqual(got, test.out) {
t.Errorf("mismatch for %q: got %v expected %v", test.in, got, test.out)
}
}
}
// TestCamelCaseToSnakeCase tests the CamelCaseToUpperSnakeCase and CamelCaseToLowerSnakeCase functions.
func TestCamelCaseToSnakeCase(t *testing.T) {
tests := []struct {
in string
out string
}{
{"", ""},
{"a", "A"},
{"A", "A"},
{"Ab", "AB"},
{"AB", "AB"},
{"ABCd", "AB_CD"},
{"aa", "AA"},
{"aaA", "AA_A"},
{"AAb", "A_AB"},
{"aaBbbCcc", "AA_BBB_CCC"},
{"http2Server", "HTTP2_SERVER"},
{"HTTPSServer42", "HTTPS_SERVER42"},
}
for _, test := range tests {
if got := CamelCaseToUpperSnakeCase(test.in); got != test.out {
t.Errorf("for %q expected upper %q and got %q", test.in, test.out, got)
}
lower := strings.ToLower(test.out)
if got := CamelCaseToLowerSnakeCase(test.in); got != lower {
t.Errorf("for %q expected lower %q and got %q", test.in, lower, got)
}
}
}
func TestCamelCaseToLowerKebabCase(t *testing.T) {
tests := []struct {
in string
out string
}{
{"", ""},
{"a", "a"},
{"A", "a"},
{"Ab", "ab"},
{"AB", "ab"},
{"ABCd", "ab-cd"},
{"aa", "aa"},
{"aaA", "aa-a"},
{"AAb", "a-ab"},
{"aaBbbCcc", "aa-bbb-ccc"},
{"http2Server", "http2-server"},
{"HTTPSServer42", "https-server42"},
}
for _, test := range tests {
if got := CamelCaseToLowerKebabCase(test.in); got != test.out {
t.Errorf("for %q expected %q and got %q", test.in, test.out, got)
}
}
}
type Embedded struct {
InnerA string
InnerB string
}
type HiddenEmbedded struct {
HA string
HB string
}
type FooConfig struct {
Foo string
Bar string
Blah int `env:"A_SPECIAL_BLAH"`
ABool bool
NotThere int `env:"-"`
HTTPServer string
IntPointer *int
FloatPointer *float64
WontShowYet map[string]string
Embedded
HiddenEmbedded `env:"-"`
RecurseHere Embedded
SomeBinary []byte
Dur time.Duration
TS time.Time
}
func TestStructToEnvVars(t *testing.T) {
intV := 199
foo := FooConfig{
Foo: "a newline:\nfoo with $X, `backticks`, \" quotes and \\ and ' in middle and end '",
Bar: "42str",
Blah: 42,
ABool: true,
NotThere: 13,
HTTPServer: "http://localhost:8080",
IntPointer: &intV,
FloatPointer: nil,
RecurseHere: Embedded{
InnerA: "rec a",
InnerB: "rec b",
},
SomeBinary: []byte{0, 1, 2},
Dur: 1*time.Hour + 100*time.Millisecond,
TS: time.Date(1998, time.November, 5, 14, 30, 0, 0, time.UTC),
}
foo.InnerA = "inner a"
foo.InnerB = "inner b"
empty, errors := StructToEnvVars(42) // error/empty
if len(empty) != 0 {
t.Errorf("expected empty, got %v", empty)
}
if len(errors) != 1 {
t.Errorf("expected errors, got %v", errors)
}
envVars, errors := StructToEnvVars(&foo)
if len(errors) != 0 {
t.Errorf("expected no error, got %v", errors)
}
if len(envVars) != 14 {
t.Errorf("expected 14 env vars, got %d: %+v", len(envVars), envVars)
}
str := ToShellWithPrefix("TST_", envVars, true)
expected := `TST_FOO='a newline:
foo with $X, ` + "`backticks`" + `, " quotes and \ and '\'' in middle and end '\'''
TST_BAR='42str'
TST_A_SPECIAL_BLAH='42'
TST_A_BOOL=true
TST_HTTP_SERVER='http://localhost:8080'
TST_INT_POINTER='199'
TST_FLOAT_POINTER=
TST_INNER_A='inner a'
TST_INNER_B='inner b'
TST_RECURSE_HERE_INNER_A='rec a'
TST_RECURSE_HERE_INNER_B='rec b'
TST_SOME_BINARY='AAEC'
TST_DUR=3600.1
TST_TS='1998-11-05T14:30:00Z'
`
if str != expected {
t.Errorf("\n---expected:---\n%s\n---got:---\n%s", expected, str)
}
// Again with export:
//nolint:lll
expected += `export TST_FOO TST_BAR TST_A_SPECIAL_BLAH TST_A_BOOL TST_HTTP_SERVER TST_INT_POINTER TST_FLOAT_POINTER TST_INNER_A TST_INNER_B TST_RECURSE_HERE_INNER_A TST_RECURSE_HERE_INNER_B TST_SOME_BINARY TST_DUR TST_TS
`
str = ToShellWithPrefix("TST_", envVars, false)
if str != expected {
t.Errorf("\n---expected:---\n%s\n---got:---\n%s", expected, str)
}
// Same no prefix
str = ToShell(envVars)
expected = strings.ReplaceAll(expected, "TST_", "")
if str != expected {
t.Errorf("\n---expected:---\n%s\n---got:---\n%s", expected, str)
}
// YAML check
str = ToYamlWithPrefix(2, "Y_", envVars)
expected = ` - name: Y_FOO
value: "a newline:\nfoo with $X, ` + "`backticks`" + `, \" quotes and \\ and ' in middle and end '"
- name: Y_BAR
value: "42str"
- name: Y_A_SPECIAL_BLAH
value: "42"
- name: Y_A_BOOL
value: true
- name: Y_HTTP_SERVER
value: "http://localhost:8080"
- name: Y_INT_POINTER
value: "199"
- name: Y_FLOAT_POINTER
value: null
- name: Y_INNER_A
value: "inner a"
- name: Y_INNER_B
value: "inner b"
- name: Y_RECURSE_HERE_INNER_A
value: "rec a"
- name: Y_RECURSE_HERE_INNER_B
value: "rec b"
- name: Y_SOME_BINARY
value: 'AAEC'
- name: Y_DUR
value: 3600.1
- name: Y_TS
value: "1998-11-05T14:30:00Z"
`
if str != expected {
t.Errorf("\n---expected:---\n%s\n---got:---\n%s", expected, str)
}
// NUL in string
type Cfg struct {
Foo string
}
cfg := Cfg{Foo: "ABC\x00DEF"}
envVars, errors = StructToEnvVars(&cfg)
if len(errors) != 1 {
t.Errorf("Should have had error with embedded NUL")
}
if envVars[0].Key != "FOO" {
t.Errorf("Expecting key to be present %v", envVars)
}
if envVars[0].ShellQuotedVal != "" {
t.Errorf("Expecting value to be empty %v", envVars)
}
}
func TestSetFromEnv(t *testing.T) {
foo := FooConfig{}
envs := map[string]string{
"TST2_FOO": "another\nfoo",
"TST2_BAR": "bar",
"TST2_RECURSE_HERE_INNER_B": "in1",
"TST2_A_SPECIAL_BLAH": "31",
"TST2_A_BOOL": "1",
"TST2_FLOAT_POINTER": "5.75",
"TST2_INT_POINTER": "73",
"TST2_SOME_BINARY": "QUJDAERFRg==",
"TST2_DUR": "123.456789",
"TST2_TS": "1998-11-05T14:30:00Z",
}
lookup := func(key string) (string, bool) {
value, found := envs[key]
return value, found
}
errors := SetFrom(lookup, "TST2_", &foo)
if len(errors) != 0 {
t.Errorf("Unexpectedly got errors :%v", errors)
}
if foo.Foo != "another\nfoo" || foo.Bar != "bar" || foo.RecurseHere.InnerB != "in1" || foo.Blah != 31 || foo.ABool != true {
t.Errorf("Mismatch in object values, got: %+v", foo)
}
if foo.IntPointer == nil || *foo.IntPointer != 73 {
t.Errorf("IntPointer not set correctly: %v %v", foo.IntPointer, *foo.IntPointer)
}
if foo.FloatPointer == nil || *foo.FloatPointer != 5.75 {
t.Errorf("FloatPointer not set correctly: %v %v", foo.FloatPointer, *foo.FloatPointer)
}
if string(foo.SomeBinary) != "ABC\x00DEF" {
t.Errorf("Base64 decoding not working for []byte field: %q", string(foo.SomeBinary))
}
if foo.Dur != 123456789*time.Microsecond {
t.Errorf("Duration not set correctly: %v", foo.Dur)
}
if foo.TS != time.Date(1998, time.November, 5, 14, 30, 0, 0, time.UTC) {
t.Errorf("Time not set correctly: %v", foo.TS)
}
envs["TST2_TS"] = "not a rfc3339 time"
errors = SetFrom(lookup, "TST2_", &foo)
if len(errors) != 1 {
t.Errorf("Expected 1 error, got %v", errors)
}
}