-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_test.go
61 lines (55 loc) · 1.2 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
package main
import (
"os"
"testing"
)
func TestGetenv(t *testing.T) {
type checkFunc func(string) error
tests := [...]struct {
name string
key string
env string
def string
expect string
}{
{
name: "fetches from env",
key: "MY_TEST_KEY",
env: "theenvvalue",
def: "",
expect: "theenvvalue",
},
{
name: "fetches from env even if default is set",
key: "MY_TEST_KEY",
env: "theenvvalue",
def: "thedefaultvalue",
expect: "theenvvalue",
},
{
name: "uses defaults if env is empty",
key: "MY_TEST_KEY",
env: "",
def: "thedefaultvalue",
expect: "thedefaultvalue",
},
{
name: "fetches weird values from env",
key: "the TEST key",
env: "this is the value. \n\n //\\\\ \nEOF\n Whynot",
expect: "this is the value. \n\n //\\\\ \nEOF\n Whynot",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
os.Setenv(tc.key, tc.env)
have := getenv(tc.key, tc.def)
if have != tc.expect {
t.Errorf("expected value %q, found %q", tc.expect, have)
}
if err := os.Unsetenv(tc.key); err != nil {
t.Fatalf("Unable to unset the key %q: %v", tc.key, err)
}
})
}
}