-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
184 lines (155 loc) · 3.38 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
package env
import (
"errors"
"os"
"testing"
"github.com/blugnu/test"
)
func TestClear(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Setenv("VAR", "value")
// ACT
Clear()
// ASSERT
_, isSet := os.LookupEnv("VAR")
test.IsFalse(t, isSet)
}
func TestGet(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Clearenv()
os.Setenv("VAR", "value")
// ACT
result := Get("VAR")
// ASSERT
test.That(t, result, "variable present").Equals("value")
// ACT
result = Get("NOTSET")
// ASSERT
test.That(t, result, "variable not present").Equals("")
}
func TestGetVars(t *testing.T) {
// ARRANGE
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
{scenario: "all variables",
exec: func(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Clearenv()
os.Setenv("VAR1", "value1")
os.Setenv("VAR2", "value2")
// ACT
result := GetVars()
// ASSERT
test.Map(t, result).Equals(Vars{"VAR1": "value1", "VAR2": "value2"})
},
},
{scenario: "specified variables (including ones not set)",
exec: func(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Clearenv()
os.Setenv("VAR1", "value1")
os.Setenv("VAR2", "value2")
// ACT
result := GetVars("VAR1", "VAR3")
// ASSERT
test.Map(t, result).Equals(Vars{"VAR1": "value1"})
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
tc.exec(t)
})
}
}
func TestLookup(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Clearenv()
os.Setenv("VAR", "value")
// ACT
result, ok := Lookup("VAR")
// ASSERT
test.That(t, result, "variable present").Equals("value")
test.IsTrue(t, ok, "variable present")
// ACT
result, ok = Lookup("NOTSET")
// ASSERT
test.That(t, result, "variable not present").Equals("")
test.IsFalse(t, ok, "variable not present")
}
func TestSet(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Clearenv()
// ACT
err := Set("VAR1", "value1")
// ASSERT
test.That(t, err).IsNil()
test.That(t, os.Getenv("VAR1")).Equals("value1")
}
func TestUnset(t *testing.T) {
// ARRANGE
testEnv := map[string]string{
"VAR1": "value1",
"VAR2": "value2",
}
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
{scenario: "no names specified",
exec: func(t *testing.T) {
// ACT
err := Unset()
// ASSERT
test.That(t, err).IsNil()
test.That(t, os.Getenv("VAR1")).Equals("value1")
test.That(t, os.Getenv("VAR2")).Equals("value2")
},
},
{scenario: "name specified",
exec: func(t *testing.T) {
// ACT
err := Unset("VAR1")
// ASSERT
test.That(t, err).IsNil()
_, isSet := os.LookupEnv("VAR1")
test.IsFalse(t, isSet)
test.That(t, os.Getenv("VAR2")).Equals("value2")
},
},
{
scenario: "error when unsetting",
exec: func(t *testing.T) {
// ARRANGE
unseterr := errors.New("unset error")
defer test.Using(&osUnsetenv, func(string) error { return unseterr })()
// ACT
err := Unset("VAR1", "VAR2")
// ASSERT
test.Error(t, err).Is(unseterr)
test.That(t, os.Getenv("VAR1")).Equals("value1")
test.That(t, os.Getenv("VAR2")).Equals("value2")
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
// ARRANGE
defer State().Reset()
os.Clearenv()
for k, v := range testEnv {
os.Setenv(k, v)
}
// ACT & ASSERT
tc.exec(t)
})
}
}