-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathenv_test.go
196 lines (183 loc) · 5.05 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
package devstatscode
import (
"fmt"
"os"
"testing"
lib "github.com/cncf/devstatscode"
testlib "github.com/cncf/devstatscode/test"
)
func TestEnv(t *testing.T) {
// Test cases
var testCases = []struct {
name string
environment map[string]string
prefix string
suffix string
newEnvs []string
expectedSave map[string]string
expectedEnv map[string]string
}{
{
"No op",
map[string]string{},
"",
"",
[]string{},
map[string]string{},
map[string]string{},
},
{
"No replaces",
map[string]string{"a": "A", "c": "C", "b": "B"},
"",
"",
[]string{},
map[string]string{},
map[string]string{"a": "A", "c": "C", "b": "B"},
},
{
"No suffix",
map[string]string{"pref_a": "A", "pref_c": "C", "pref_b": "B"},
"pref_",
"",
[]string{},
map[string]string{},
map[string]string{"pref_a": "A", "pref_c": "C", "pref_b": "B"},
},
{
"No prefix and no suffix hit",
map[string]string{"pref_a": "A", "pref_c": "C", "pref_b": "B"},
"",
"_suff",
[]string{},
map[string]string{},
map[string]string{"pref_a": "A", "pref_c": "C", "pref_b": "B"},
},
{
"No prefix with suffix hit",
map[string]string{"pref_a": "A", "pref_c": "C", "pref_b": "B", "pref_a_suff": "D"},
"",
"_suff",
[]string{},
map[string]string{"pref_a": "A"},
map[string]string{"pref_a": "D", "pref_c": "C", "pref_b": "B", "pref_a_suff": "D"},
},
{
"Prefix and suffix",
map[string]string{"pref_a": "A", "pref_c": "C", "pref_b": "B", "pref_a_suff": "D", "a": "A", "a_suff": "D"},
"pref_",
"_suff",
[]string{},
map[string]string{"pref_a": "A"},
map[string]string{"pref_a": "D", "pref_c": "C", "pref_b": "B", "pref_a_suff": "D", "a": "A", "a_suff": "D"},
},
{
"Replace all starting with 'a' with suffix 2",
map[string]string{"a1": "1", "a2": "2", "b1": "3", "b2": "4", "a12": "5", "a22": "6", "b12": "7", "b22": "8"},
"a",
"2",
[]string{},
map[string]string{"a": "{{unset}}", "a1": "1", "a2": "2"},
map[string]string{"a1": "5", "a2": "6", "b1": "3", "b2": "4", "a12": "5", "a22": "6", "b12": "7", "b22": "8"},
},
{
"Need to save empty variables too",
map[string]string{"a": "", "b": "B", "c": "", "a2": "1", "b2": "2", "c2": "3"},
"",
"2",
[]string{},
map[string]string{"a": "", "b": "B", "c": ""},
map[string]string{"a": "1", "b": "2", "c": "3", "a2": "1", "b2": "2", "c2": "3"},
},
{
"Replace nonexisting var",
map[string]string{"pref_a_suff": "new_value"},
"pref_",
"_suff",
[]string{"pref_a"},
map[string]string{"pref_a": "{{unset}}"},
map[string]string{"pref_a": "new_value", "pref_a_suff": "new_value"},
},
{
"Crazy",
map[string]string{"aa": "2", "a": "1", "aaaa": "4", "aaa": "3"},
"a",
"a",
[]string{},
map[string]string{"a": "1", "aa": "2", "aaa": "3"},
map[string]string{"a": "2", "aa": "3", "aaa": "4", "aaaa": "4"},
},
}
// Execute test cases
for index, test := range testCases {
// Remember initial environment
currEnv := make(map[string]string)
for key := range test.environment {
currEnv[key] = os.Getenv(key)
}
// Set new environment
for key, value := range test.environment {
err := os.Setenv(key, value)
if err != nil {
t.Errorf(err.Error())
}
}
// Call EnvReplace
saved := lib.EnvReplace(test.prefix, test.suffix)
// Get replaced environment
replacedEnv := make(map[string]string)
for key := range test.environment {
replacedEnv[key] = os.Getenv(key)
}
for _, key := range test.newEnvs {
replacedEnv[key] = os.Getenv(key)
}
// Call EnvRestore
lib.EnvRestore(saved)
// Get restored environment
restoredEnv := make(map[string]string)
for key := range test.environment {
restoredEnv[key] = os.Getenv(key)
}
// Remove the test environment
for key := range test.environment {
//err := os.Setenv(key, currEnv[key])
err := os.Unsetenv(key)
if err != nil {
t.Errorf(err.Error())
}
}
// Maps are not directly compareable (due to unknown key order) - need to transorm them
testlib.MakeComparableMapStr(&test.environment)
testlib.MakeComparableMapStr(&test.expectedSave)
testlib.MakeComparableMapStr(&test.expectedEnv)
testlib.MakeComparableMapStr(&saved)
testlib.MakeComparableMapStr(&replacedEnv)
testlib.MakeComparableMapStr(&restoredEnv)
// Check if we got expected values
got := fmt.Sprintf("%+v", replacedEnv)
expected := fmt.Sprintf("%+v", test.expectedEnv)
if got != expected {
t.Errorf(
"Test case number %d \"%s\"\nExpected replaced env:\n%+v\nGot:\n%+v\n",
index+1, test.name, expected, got,
)
}
got = fmt.Sprintf("%+v", saved)
expected = fmt.Sprintf("%+v", test.expectedSave)
if got != expected {
t.Errorf(
"Test case number %d \"%s\"\nExpected saved env:\n%+v\nGot:\n%+v\n",
index+1, test.name, expected, got,
)
}
got = fmt.Sprintf("%+v", restoredEnv)
expected = fmt.Sprintf("%+v", test.environment)
if got != expected {
t.Errorf(
"Test case number %d \"%s\"\nExpected restored env:\n%+v\nGot:\n%+v\n",
index+1, test.name, expected, got,
)
}
}
}