-
Notifications
You must be signed in to change notification settings - Fork 4
/
environment_test.go
75 lines (68 loc) · 1.54 KB
/
environment_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
package venom
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEnvironment(t *testing.T) {
testIO := []struct {
tc string
key string
envVar string
value string
expect interface{}
ok bool
resolver Resolver
}{
{
tc: "should retrieve a basic environment variable",
key: "foo",
envVar: "FOO",
value: "bar",
resolver: defaultEnvResolver,
expect: "bar",
ok: true,
},
{
tc: "should retrieve a prefix environemnt variable",
key: "timeout",
envVar: "MY_SERVICE_TIMEOUT",
value: "10",
resolver: func() Resolver {
return &EnvironmentVariableResolver{
Prefix: "MY_SERVICE",
}
}(),
expect: "10",
ok: true,
},
{
tc: "should fail to retrieve non-prefixed environemnt variable",
key: "timeout",
envVar: "TIMEOUT",
value: "10",
resolver: func() Resolver {
return &EnvironmentVariableResolver{
Prefix: "MY_SERVICE",
}
}(),
expect: nil,
ok: false,
},
}
for _, test := range testIO {
t.Run(test.key, func(t *testing.T) {
v := New()
v.RegisterResolver(EnvironmentLevel, test.resolver)
// set the test value into the environment
os.Setenv(test.envVar, test.value)
// ensure we get the expected value back from the environment
actual, ok := v.Find(test.key)
assert.Equal(t, test.ok, ok)
assert.Equal(t, test.expect, actual)
// unset our test key from the environment to keep the next test run
// clean
os.Unsetenv(test.envVar)
})
}
}