-
Notifications
You must be signed in to change notification settings - Fork 0
/
envy_test.go
307 lines (294 loc) · 6.47 KB
/
envy_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
package envy_test
import (
"flag"
"fmt"
"os"
"reflect"
"sort"
"strings"
"testing"
"github.com/licaonfee/envy/v2"
)
func TestGetEnv(t *testing.T) {
tests := []struct {
name string
env envy.Env
key string
want string
}{
{
name: "Get existent env",
env: envy.NewMapEnv(map[string]string{"existent_key": "value"}),
key: "existent_key",
want: "value",
},
{
name: "Get not existent env",
env: envy.NewMapEnv(map[string]string{"existent_key": "value"}),
key: "missing_key",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.env.Getenv(tt.key)
if got != tt.want {
t.Errorf("GetEnv() got = %s , want = %s", got, tt.want)
}
})
}
}
func TestLookupEnv(t *testing.T) {
setOsEnv := func() *envy.OsEnv {
_ = os.Setenv("existent_key", "value")
_ = os.Unsetenv("missing_key")
return envy.NewOsEnv()
}
objects := []envy.Env{
envy.NewMapEnv(map[string]string{"existent_key": "value"}),
setOsEnv(),
}
tests := []struct {
name string
env envy.Env
key string
want string
wantOk bool
}{
{
name: "Get existent env",
key: "existent_key",
want: "value",
wantOk: true,
},
{
name: "Get not existent env",
key: "missing_key",
want: "",
wantOk: false,
},
}
for _, m := range objects {
for _, tt := range tests {
name := fmt.Sprintf("%s_%T", tt.name, m)
t.Run(name, func(t *testing.T) {
got, ok := m.LookupEnv(tt.key)
if got != tt.want || ok != tt.wantOk {
t.Errorf("GetEnv() got = (%s,%v) , want = (%s,%v)", got, ok, tt.want, tt.wantOk)
}
})
}
}
}
func TestSetEnv(t *testing.T) {
tests := []struct {
name string
env envy.Env
key string
value string
}{
{
name: "Set key",
env: envy.NewMapEnv(map[string]string{}),
key: "setted_key",
value: "xxxxx",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = tt.env.Setenv(tt.key, tt.value)
got := tt.env.Getenv(tt.key)
if got != tt.value {
t.Errorf("GetEnv() got = %s , want = %s", got, tt.value)
}
})
}
}
func TestUnsetEnv(t *testing.T) {
tests := []struct {
name string
env envy.Env
key string
value string
}{
{
name: "unset key",
env: envy.NewMapEnv(map[string]string{"deleted_key": "asdf"}),
key: "deleted_key",
value: "asdf",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = tt.env.Unsetenv(tt.key)
got := tt.env.Getenv(tt.key)
if got != "" {
t.Errorf("Unsetenv() got = %s , want = %s", got, tt.value)
}
})
}
}
func TestCleanenv(t *testing.T) {
tests := []struct {
name string
env envy.Env
}{
{
name: "clean",
env: envy.NewMapEnv(map[string]string{"cleaned_key01": "asdf", "cleaned_key02": "asdf"}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.env.Clearenv()
got := tt.env.Getenv("cleaned_key01")
got += tt.env.Getenv("cleaned_key02")
if got != "" {
t.Errorf("Cleanenv() got = %s , want = \"\"", got)
}
})
}
}
func TestMapEnvEnviron(t *testing.T) {
tests := []struct {
name string
env envy.Env
want []string
}{
{
name: "Environ",
env: envy.NewMapEnv(map[string]string{"env_key_01": "01", "env_key_02": "02"}),
want: []string{"env_key_01=01", "env_key_02=02"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.env.Environ()
sort.Strings(got)
sort.Strings(tt.want)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Environ() got = %v , want = %v", got, tt.want)
}
})
}
}
func TestFillFlagsLookup(t *testing.T) {
tests := []struct {
name string
env envy.Env
lookup func(string) string
want map[string]interface{}
wantErr bool
}{
{name: "fill all",
env: envy.NewMapEnv(map[string]string{
"FLAG01": "value1",
"FLAG02": "55",
}),
lookup: strings.ToUpper,
want: map[string]interface{}{
"flag01": "value1",
"flag02": 55,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fs := flag.NewFlagSet(tt.name, flag.ContinueOnError)
got := make(map[string]interface{})
for k, v := range tt.want {
var value interface{}
switch v.(type) {
case string:
value = new(string)
fs.StringVar(value.(*string), k, "", "")
case int:
value = new(int)
fs.IntVar(value.(*int), k, 0, "")
}
got[k] = value
}
if err := fs.Parse([]string{tt.name}); err != nil {
t.Fatal(err)
}
err := envy.FillFlagsLookup(fs, tt.env, tt.lookup)
if (err != nil) != tt.wantErr {
t.Fatalf("FillFlagsLookup() err = %v , wantErr = %v", err, tt.wantErr)
}
for k, v := range tt.want {
g, ok := got[k]
if !ok {
t.Fatalf("FillFlagLookup() missing flag %s", k)
}
var val interface{}
switch t := g.(type) {
case *string:
val = *t
case *int:
val = *t
}
if !reflect.DeepEqual(val, v) {
t.Fatalf("FillFlagLookup() got = %v , want = %v", val, v)
}
}
})
}
}
func TestFillMap(t *testing.T) {
tests := map[string]struct {
env envy.Env
filter func(string) (envy.VarName, envy.VarType)
want map[string]any
}{
"only strings": {
env: envy.NewMapEnv(map[string]string{
"TEST01_FOO": "var 01",
"TEST01_BAR": "var 02",
"TEST01_BAZ": "var 03"}),
filter: envy.FilterPrefix("TEST01_"),
want: map[string]any{
"foo": "var 01",
"bar": "var 02",
"baz": "var 03",
},
},
"array": {
env: envy.NewMapEnv(map[string]string{
"TEST_CONFIG": "path",
"TEST_USERS_0": "user00",
"TEST_USERS_1": "user01",
"TEST_USERS_2": "user02",
"TEST_USERS_3": "user03",
}),
filter: envy.FilterPrefix("TEST_"),
want: map[string]any{
"config": "path",
"users": []string{"user00", "user01", "user02", "user03"},
},
},
"map": {
env: envy.NewMapEnv(map[string]string{
"TEST_CONFIG": "path",
"TEST_USERS_0": "user00",
"TEST_USERS_1": "user01",
"TEST_ADDR_DEFAULT": "localhost",
"TEST_ADDR_FALLBACK": "127.0.0.1",
}),
filter: envy.FilterPrefix("TEST_", "addr"),
want: map[string]any{
"config": "path",
"users": []string{"user00", "user01"},
"addr": map[string]string{"default": "localhost", "fallback": "127.0.0.1"},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := envy.FillMap(tt.env, tt.filter)
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("FillMap got %#v, want %#v", got, tt.want)
}
})
}
}