-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_test.go
78 lines (61 loc) · 1.55 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
package env
import (
"reflect"
"testing"
)
func assertNoError(t *testing.T, err error, msgAndArgs ...interface{}) {
t.Helper()
if err != nil {
t.Errorf("unexpected error: %v %v", err, msgAndArgs)
}
}
func assertError(t *testing.T, err error, msgAndArgs ...interface{}) {
t.Helper()
if err == nil {
t.Errorf("expected error, got nil %v", msgAndArgs...)
}
}
func assertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
t.Helper()
if !reflect.DeepEqual(expected, actual) {
t.Errorf("expected %v, got %v %v", expected, actual, msgAndArgs)
}
}
func setEnvForTest(t *testing.T, name string, value string) {
t.Helper()
err := Set(name, value)
assertNoError(t, err, "Set")
t.Cleanup(func() {
err = Unset(name) // clean after the test
assertNoError(t, err, "Unset")
})
}
func TestSetUnset(t *testing.T) {
key, value := "TEST_KEY", "TEST_VALUE"
err := Set(key, value)
assertNoError(t, err, "Set")
actual, err := Get(key)
assertNoError(t, err, "Get")
assertEqual(t, value, actual, "Get")
err = Unset(key)
assertNoError(t, err, "Unset")
if _, ok := Lookup(key); ok {
t.Errorf("Lookup: expected %s to be unset", key)
}
}
func TestGetUnset(t *testing.T) {
key := "TEST_UNSET"
_, err := Get(key)
assertError(t, err, "Get unset variable")
}
func TestRequire(t *testing.T) {
key := "TEST_REQUIRED"
err := Require(key)
assertError(t, err, "Require")
err = Set(key, "value")
assertNoError(t, err, "Set")
err = Require(key)
assertNoError(t, err, "Require")
err = Unset(key)
assertNoError(t, err, "Unset")
}