-
Notifications
You must be signed in to change notification settings - Fork 146
/
env_test.go
260 lines (251 loc) · 5.27 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
package env
import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
func TestProvider(t *testing.T) {
testCases := []struct {
name string
prefix string
delim string
key string
value string
expKey string
expValue string
cb func(key string) string
want *Env
}{
{
name: "Nil cb",
prefix: "TESTVAR_",
delim: ".",
want: &Env{
prefix: "TESTVAR_",
delim: ".",
},
},
{
name: "Simple cb",
prefix: "TESTVAR_",
delim: ".",
key: "TestKey",
value: "TestVal",
expKey: "testkey",
expValue: "TestVal",
cb: func(key string) string {
return strings.ToLower(key)
},
want: &Env{
prefix: "TESTVAR_",
delim: ".",
},
},
{
name: "Empty string nil cb",
prefix: "",
delim: ".",
want: &Env{
prefix: "",
delim: ".",
},
},
{
name: "Cb is given",
prefix: "",
delim: ".",
key: "test_key",
value: "test_val",
expKey: "TEST.KEY",
expValue: "test_val",
cb: func(key string) string {
return strings.Replace(strings.ToUpper(key), "_", ".", -1)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotProvider := Provider(tc.prefix, tc.delim, tc.cb)
if tc.cb == nil {
assert.Equal(t, tc.want, gotProvider)
}
if tc.cb != nil {
k, v := gotProvider.cb(tc.key, tc.value)
assert.Equal(t, tc.expKey, k)
assert.Equal(t, tc.expValue, v)
}
})
}
}
func TestProviderWithValue(t *testing.T) {
testCases := []struct {
name string
prefix string
delim string
cb func(key string, value string) (string, interface{})
nilCallback bool
want *Env
}{
{
name: "Nil cb",
prefix: "TEST_",
delim: ".",
nilCallback: true,
want: &Env{
prefix: "TEST_",
delim: ".",
},
},
{
name: "Empty string nil cb",
prefix: "",
delim: ".",
nilCallback: true,
want: &Env{
prefix: "",
delim: ".",
},
},
{
name: "Return the same key-value pair in cb",
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
return key, value
},
want: &Env{
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
return key, value
},
},
},
{
name: "Custom cb function",
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
key = strings.Replace(strings.TrimPrefix(strings.ToLower(key), "test_"), "_", ".", -1)
return key, value
},
want: &Env{
prefix: "TEST_",
delim: ".",
cb: func(key string, value string) (string, interface{}) {
key = strings.Replace(strings.TrimPrefix(strings.ToLower(key), "test_"), "_", ".", -1)
return key, value
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := ProviderWithValue(tc.prefix, tc.delim, tc.cb)
if tc.nilCallback {
assert.Equal(t, tc.want, got)
} else {
keyGot, valGot := got.cb("test_key_env_1", "test_val")
keyWant, valWant := tc.want.cb("test_key_env_1", "test_val")
assert.Equal(t, tc.prefix, got.prefix)
assert.Equal(t, tc.delim, got.delim)
assert.Equal(t, keyWant, keyGot)
assert.Equal(t, valWant, valGot)
}
})
}
}
func TestRead(t *testing.T) {
testCases := []struct {
name string
key string
value string
expKey string
expValue string
env *Env
}{
{
name: "No cb",
key: "TEST_KEY",
value: "TEST_VAL",
expKey: "TEST_KEY",
expValue: "TEST_VAL",
env: &Env{
delim: ".",
},
},
{
name: "cb given",
key: "TEST_KEY",
value: "TEST_VAL",
expKey: "test.key",
expValue: "TEST_VAL",
env: &Env{
delim: "_",
cb: func(key string, value string) (string, interface{}) {
return strings.Replace(strings.ToLower(key), "_", ".", -1), value
},
},
},
{
name: "No cb - prefix given",
key: "TEST_KEY",
value: "TEST_VAL",
expKey: "test.key",
expValue: "TEST_VAL",
env: &Env{
prefix: "TEST",
delim: "/",
cb: func(key string, value string) (string, interface{}) {
return strings.Replace(strings.ToLower(key), "_", ".", -1), value
},
},
},
{
name: "Path value",
key: "TEST_DIR",
value: "/test/dir/file",
expKey: "TEST_DIR",
expValue: "/test/dir/file",
env: &Env{
delim: ".",
},
},
{
name: "Replace value with underscore",
key: "TEST_DIR",
value: "/test/dir/file",
expKey: "TEST_DIR",
expValue: "_test_dir_file",
env: &Env{
delim: ".",
cb: func(key string, value string) (string, interface{}) {
return key, strings.Replace(strings.ToLower(value), "/", "_", -1)
},
},
},
{
name: "Empty value",
key: "KEY",
value: "",
expKey: "KEY",
expValue: "",
env: &Env{
delim: ".",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := os.Setenv(tc.key, tc.value)
assert.Nil(t, err)
defer os.Unsetenv(tc.key)
envs, err := tc.env.Read()
assert.Nil(t, err)
v, ok := envs[tc.expKey]
assert.True(t, ok)
assert.Equal(t, tc.expValue, v)
})
}
}