-
Notifications
You must be signed in to change notification settings - Fork 565
/
env_test.go
425 lines (402 loc) · 9.74 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
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
// Copyright 2014 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the DOCKER-LICENSE file.
package docker
import (
"bytes"
"errors"
"reflect"
"slices"
"testing"
)
func TestGet(t *testing.T) {
t.Parallel()
tests := []struct {
input []string
query string
expected string
}{
{[]string{"PATH=/usr/bin:/bin", "PYTHONPATH=/usr/local"}, "PATH", "/usr/bin:/bin"},
{[]string{"PATH=/usr/bin:/bin", "PYTHONPATH=/usr/local"}, "PYTHONPATH", "/usr/local"},
{[]string{"PATH=/usr/bin:/bin", "PYTHONPATH=/usr/local"}, "PYTHONPATHI", ""},
{[]string{"WAT="}, "WAT", ""},
}
for _, tt := range tests {
test := tt
t.Run("", func(t *testing.T) {
t.Parallel()
env := Env(test.input)
got := env.Get(test.query)
if got != test.expected {
t.Errorf("Env.Get(%q): wrong result. Want %q. Got %q", test.query, test.expected, got)
}
})
}
}
func TestExists(t *testing.T) {
t.Parallel()
tests := []struct {
input []string
query string
expected bool
}{
{[]string{"WAT=", "PYTHONPATH=/usr/local"}, "WAT", true},
{[]string{"PATH=/usr/bin:/bin", "PYTHONPATH=/usr/local"}, "PYTHONPATH", true},
{[]string{"PATH=/usr/bin:/bin", "PYTHONPATH=/usr/local"}, "PYTHONPATHI", false},
}
for _, tt := range tests {
test := tt
t.Run("", func(t *testing.T) {
t.Parallel()
env := Env(test.input)
got := env.Exists(test.query)
if got != test.expected {
t.Errorf("Env.Exists(%q): wrong result. Want %v. Got %v", test.query, test.expected, got)
}
})
}
}
func TestGetBool(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected bool
}{
{"EMPTY_VAR", false},
{"ZERO_VAR", false},
{"NO_VAR", false},
{"FALSE_VAR", false},
{"NONE_VAR", false},
{"TRUE_VAR", true},
{"WAT", true},
{"PATH", true},
{"ONE_VAR", true},
{"NO_VAR_TAB", false},
}
env := Env([]string{
"EMPTY_VAR=", "ZERO_VAR=0", "NO_VAR=no", "FALSE_VAR=false",
"NONE_VAR=none", "TRUE_VAR=true", "WAT=wat", "PATH=/usr/bin:/bin",
"ONE_VAR=1", "NO_VAR_TAB=0 \t\t\t",
})
for _, tt := range tests {
test := tt
t.Run(test.input, func(t *testing.T) {
got := env.GetBool(test.input)
if got != test.expected {
t.Errorf("Env.GetBool(%q): wrong result. Want %v. Got %v.", test.input, test.expected, got)
}
})
}
}
func TestSetBool(t *testing.T) {
t.Parallel()
tests := []struct {
input bool
expected string
}{
{true, "1"},
{false, "0"},
}
for _, tt := range tests {
var env Env
env.SetBool("SOME", tt.input)
if got := env.Get("SOME"); got != tt.expected {
t.Errorf("Env.SetBool(%v): wrong result. Want %q. Got %q", tt.input, tt.expected, got)
}
}
}
func TestGetInt(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected int
}{
{"NEGATIVE_INTEGER", -10},
{"NON_INTEGER", -1},
{"ONE", 1},
{"TWO", 2},
}
env := Env([]string{"NEGATIVE_INTEGER=-10", "NON_INTEGER=wat", "ONE=1", "TWO=2"})
for _, tt := range tests {
test := tt
t.Run(test.input, func(t *testing.T) {
t.Parallel()
got := env.GetInt(test.input)
if got != test.expected {
t.Errorf("Env.GetInt(%q): wrong result. Want %d. Got %d", test.input, test.expected, got)
}
})
}
}
func TestSetInt(t *testing.T) {
t.Parallel()
tests := []struct {
input int
expected string
}{
{10, "10"},
{13, "13"},
{7, "7"},
{33, "33"},
{0, "0"},
{-34, "-34"},
}
for _, tt := range tests {
test := tt
t.Run(test.expected, func(t *testing.T) {
t.Parallel()
var env Env
env.SetInt("SOME", test.input)
if got := env.Get("SOME"); got != test.expected {
t.Errorf("Env.SetBool(%d): wrong result. Want %q. Got %q", test.input, test.expected, got)
}
})
}
}
func TestGetInt64(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected int64
}{
{"NEGATIVE_INTEGER", -10},
{"NON_INTEGER", -1},
{"ONE", 1},
{"TWO", 2},
}
env := Env([]string{"NEGATIVE_INTEGER=-10", "NON_INTEGER=wat", "ONE=1", "TWO=2"})
for _, tt := range tests {
test := tt
t.Run(test.input, func(t *testing.T) {
t.Parallel()
got := env.GetInt64(test.input)
if got != test.expected {
t.Errorf("Env.GetInt64(%q): wrong result. Want %d. Got %d", test.input, test.expected, got)
}
})
}
}
func TestSetInt64(t *testing.T) {
t.Parallel()
tests := []struct {
input int64
expected string
}{
{10, "10"},
{13, "13"},
{7, "7"},
{33, "33"},
{0, "0"},
{-34, "-34"},
}
for _, tt := range tests {
test := tt
t.Run(test.expected, func(t *testing.T) {
t.Parallel()
var env Env
env.SetInt64("SOME", test.input)
if got := env.Get("SOME"); got != test.expected {
t.Errorf("Env.SetBool(%d): wrong result. Want %q. Got %q", test.input, test.expected, got)
}
})
}
}
func TestGetJSON(t *testing.T) {
t.Parallel()
var p struct {
Name string `json:"name"`
Age int `json:"age"`
}
var env Env
env.Set("person", `{"name":"Gopher","age":5}`)
err := env.GetJSON("person", &p)
if err != nil {
t.Error(err)
}
if p.Name != "Gopher" {
t.Errorf("Env.GetJSON(%q): wrong name. Want %q. Got %q", "person", "Gopher", p.Name)
}
if p.Age != 5 {
t.Errorf("Env.GetJSON(%q): wrong age. Want %d. Got %d", "person", 5, p.Age)
}
}
func TestGetJSONAbsent(t *testing.T) {
t.Parallel()
var l []string
var env Env
err := env.GetJSON("person", &l)
if err != nil {
t.Error(err)
}
if l != nil {
t.Errorf("Env.GetJSON(): get unexpected list %v", l)
}
}
func TestGetJSONFailure(t *testing.T) {
t.Parallel()
var p []string
var env Env
env.Set("list-person", `{"name":"Gopher","age":5}`)
err := env.GetJSON("list-person", &p)
if err == nil {
t.Errorf("Env.GetJSON(%q): got unexpected <nil> error.", "list-person")
}
}
func TestSetJSON(t *testing.T) {
t.Parallel()
p1 := struct {
Name string `json:"name"`
Age int `json:"age"`
}{Name: "Gopher", Age: 5}
var env Env
err := env.SetJSON("person", p1)
if err != nil {
t.Error(err)
}
var p2 struct {
Name string `json:"name"`
Age int `json:"age"`
}
err = env.GetJSON("person", &p2)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(p1, p2) {
t.Errorf("Env.SetJSON(%q): wrong result. Want %v. Got %v", "person", p1, p2)
}
}
func TestSetJSONFailure(t *testing.T) {
t.Parallel()
var env Env
err := env.SetJSON("person", unmarshable{})
if err == nil {
t.Error("Env.SetJSON(): got unexpected <nil> error")
}
if env.Exists("person") {
t.Errorf("Env.SetJSON(): should not define the key %q, but did", "person")
}
}
func TestGetList(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected []string
}{
{"WAT=wat", []string{"wat"}},
{`WAT=["wat","wet","wit","wot","wut"]`, []string{"wat", "wet", "wit", "wot", "wut"}},
{"WAT=", nil},
}
for _, tt := range tests {
env := Env([]string{tt.input})
got := env.GetList("WAT")
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("Env.GetList(%q): wrong result. Want %v. Got %v", "WAT", tt.expected, got)
}
}
}
func TestSetList(t *testing.T) {
t.Parallel()
list := []string{"a", "b", "c"}
var env Env
if err := env.SetList("SOME", list); err != nil {
t.Error(err)
}
if got := env.GetList("SOME"); !reflect.DeepEqual(got, list) {
t.Errorf("Env.SetList(%v): wrong result. Got %v", list, got)
}
}
func TestSet(t *testing.T) {
t.Parallel()
var env Env
env.Set("PATH", "/home/bin:/bin")
env.Set("SOMETHING", "/usr/bin")
env.Set("PATH", "/bin")
if expected, got := "/usr/bin", env.Get("SOMETHING"); got != expected {
t.Errorf("Env.Set(%q): wrong result. Want %q. Got %q", expected, expected, got)
}
if expected, got := "/bin", env.Get("PATH"); got != expected {
t.Errorf("Env.Set(%q): wrong result. Want %q. Got %q", expected, expected, got)
}
}
func TestDecode(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expectedOut []string
expectedErr bool
}{
{
`{"PATH":"/usr/bin:/bin","containers":54,"wat":["123","345"]}`,
[]string{"PATH=/usr/bin:/bin", "containers=54", `wat=["123","345"]`},
false,
},
{"}}", nil, true},
{`{}`, nil, false},
}
for _, tt := range tests {
test := tt
t.Run(test.input, func(t *testing.T) {
t.Parallel()
var env Env
err := env.Decode(bytes.NewBufferString(test.input))
if !test.expectedErr && err != nil {
t.Error(err)
} else if test.expectedErr && err == nil {
t.Error("Env.Decode(): unexpected <nil> error")
}
got := []string(env)
slices.Sort(got)
slices.Sort(test.expectedOut)
if !reflect.DeepEqual(got, test.expectedOut) {
t.Errorf("Env.Decode(): wrong result. Want %v. Got %v.", test.expectedOut, got)
}
})
}
}
func TestSetAuto(t *testing.T) {
t.Parallel()
buf := bytes.NewBufferString("oi")
tests := []struct {
input any
expected string
}{
{10, "10"},
{10.3, "10"},
{"oi", "oi"},
{buf, "{}"},
{unmarshable{}, "{}"},
}
for _, tt := range tests {
test := tt
t.Run(test.expected, func(t *testing.T) {
t.Parallel()
var env Env
env.SetAuto("SOME", test.input)
if got := env.Get("SOME"); got != test.expected {
t.Errorf("Env.SetAuto(%v): wrong result. Want %q. Got %q", test.input, test.expected, got)
}
})
}
}
func TestMap(t *testing.T) {
t.Parallel()
tests := []struct {
input []string
expected map[string]string
}{
{[]string{"PATH=/usr/bin:/bin", "PYTHONPATH=/usr/local"}, map[string]string{"PATH": "/usr/bin:/bin", "PYTHONPATH": "/usr/local"}},
{[]string{"ENABLE_LOGGING", "PYTHONPATH=/usr/local"}, map[string]string{"ENABLE_LOGGING": "", "PYTHONPATH": "/usr/local"}},
{nil, nil},
}
for _, tt := range tests {
env := Env(tt.input)
got := env.Map()
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("Env.Map(): wrong result. Want %v. Got %v", tt.expected, got)
}
}
}
type unmarshable struct{}
func (unmarshable) MarshalJSON() ([]byte, error) {
return nil, errors.New("cannot marshal")
}