forked from richardartoul/gobuildcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_test.go
More file actions
284 lines (274 loc) · 7.62 KB
/
env_test.go
File metadata and controls
284 lines (274 loc) · 7.62 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"testing"
)
func TestGetEnvWithPrefix(t *testing.T) {
tests := []struct {
name string
key string
defaultValue string
envVars map[string]string
expected string
}{
{
name: "returns default when neither env var is set",
key: "TEST_KEY",
defaultValue: "default",
envVars: map[string]string{},
expected: "default",
},
{
name: "returns unprefixed value when only unprefixed is set",
key: "TEST_KEY",
defaultValue: "default",
envVars: map[string]string{"TEST_KEY": "unprefixed_value"},
expected: "unprefixed_value",
},
{
name: "returns prefixed value when only prefixed is set",
key: "TEST_KEY",
defaultValue: "default",
envVars: map[string]string{"GOBUILDCACHE_TEST_KEY": "prefixed_value"},
expected: "prefixed_value",
},
{
name: "prefixed value takes precedence over unprefixed",
key: "TEST_KEY",
defaultValue: "default",
envVars: map[string]string{
"TEST_KEY": "unprefixed_value",
"GOBUILDCACHE_TEST_KEY": "prefixed_value",
},
expected: "prefixed_value",
},
{
name: "works with S3_BUCKET style keys",
key: "S3_BUCKET",
defaultValue: "",
envVars: map[string]string{"GOBUILDCACHE_S3_BUCKET": "my-bucket"},
expected: "my-bucket",
},
{
name: "works with BACKEND_TYPE style keys",
key: "BACKEND_TYPE",
defaultValue: "disk",
envVars: map[string]string{"GOBUILDCACHE_BACKEND_TYPE": "s3"},
expected: "s3",
},
{
name: "empty prefixed value falls through to unprefixed",
key: "TEST_KEY",
defaultValue: "default",
envVars: map[string]string{
"TEST_KEY": "unprefixed_value",
"GOBUILDCACHE_TEST_KEY": "",
},
expected: "unprefixed_value",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear environment variables (t.Setenv auto-restores on test completion)
t.Setenv(tt.key, "")
t.Setenv("GOBUILDCACHE_"+tt.key, "")
// Set test-specific environment variables
for k, v := range tt.envVars {
t.Setenv(k, v)
}
result := getEnvWithPrefix(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("getEnvWithPrefix(%q, %q) = %q, want %q", tt.key, tt.defaultValue, result, tt.expected)
}
})
}
}
func TestGetEnvBoolWithPrefix(t *testing.T) {
tests := []struct {
name string
key string
defaultValue bool
envVars map[string]string
expected bool
}{
{
name: "returns default when neither env var is set",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{},
expected: false,
},
{
name: "returns true default when neither env var is set",
key: "TEST_BOOL",
defaultValue: true,
envVars: map[string]string{},
expected: true,
},
{
name: "returns unprefixed value when only unprefixed is set",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{"TEST_BOOL": "true"},
expected: true,
},
{
name: "returns prefixed value when only prefixed is set",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{"GOBUILDCACHE_TEST_BOOL": "true"},
expected: true,
},
{
name: "prefixed value takes precedence over unprefixed",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{
"TEST_BOOL": "false",
"GOBUILDCACHE_TEST_BOOL": "true",
},
expected: true,
},
{
name: "prefixed false overrides unprefixed true",
key: "TEST_BOOL",
defaultValue: true,
envVars: map[string]string{
"TEST_BOOL": "true",
"GOBUILDCACHE_TEST_BOOL": "false",
},
expected: false,
},
{
name: "accepts 1 as true",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{"GOBUILDCACHE_TEST_BOOL": "1"},
expected: true,
},
{
name: "accepts yes as true",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{"GOBUILDCACHE_TEST_BOOL": "yes"},
expected: true,
},
{
name: "accepts YES as true (case insensitive)",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{"GOBUILDCACHE_TEST_BOOL": "YES"},
expected: true,
},
{
name: "empty prefixed value falls through to unprefixed",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{
"TEST_BOOL": "true",
"GOBUILDCACHE_TEST_BOOL": "",
},
expected: true,
},
{
name: "invalid prefixed value falls through to unprefixed",
key: "TEST_BOOL",
defaultValue: false,
envVars: map[string]string{
"TEST_BOOL": "true",
"GOBUILDCACHE_TEST_BOOL": "not-a-bool",
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear environment variables (t.Setenv auto-restores on test completion)
t.Setenv(tt.key, "")
t.Setenv("GOBUILDCACHE_"+tt.key, "")
// Set test-specific environment variables
for k, v := range tt.envVars {
t.Setenv(k, v)
}
result := getEnvBoolWithPrefix(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("getEnvBoolWithPrefix(%q, %v) = %v, want %v", tt.key, tt.defaultValue, result, tt.expected)
}
})
}
}
func TestGetEnvFloatWithPrefix(t *testing.T) {
tests := []struct {
name string
key string
defaultValue float64
envVars map[string]string
expected float64
}{
{
name: "returns default when neither env var is set",
key: "TEST_FLOAT",
defaultValue: 0.5,
envVars: map[string]string{},
expected: 0.5,
},
{
name: "returns unprefixed value when only unprefixed is set",
key: "TEST_FLOAT",
defaultValue: 0.0,
envVars: map[string]string{"TEST_FLOAT": "0.75"},
expected: 0.75,
},
{
name: "returns prefixed value when only prefixed is set",
key: "TEST_FLOAT",
defaultValue: 0.0,
envVars: map[string]string{"GOBUILDCACHE_TEST_FLOAT": "0.25"},
expected: 0.25,
},
{
name: "prefixed value takes precedence over unprefixed",
key: "TEST_FLOAT",
defaultValue: 0.0,
envVars: map[string]string{
"TEST_FLOAT": "0.5",
"GOBUILDCACHE_TEST_FLOAT": "0.9",
},
expected: 0.9,
},
{
name: "returns default for invalid prefixed value, falls back to unprefixed",
key: "TEST_FLOAT",
defaultValue: 0.0,
envVars: map[string]string{
"TEST_FLOAT": "0.5",
"GOBUILDCACHE_TEST_FLOAT": "not-a-number",
},
expected: 0.5,
},
{
name: "empty prefixed value falls through to unprefixed",
key: "TEST_FLOAT",
defaultValue: 0.0,
envVars: map[string]string{
"TEST_FLOAT": "0.75",
"GOBUILDCACHE_TEST_FLOAT": "",
},
expected: 0.75,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear environment variables (t.Setenv auto-restores on test completion)
t.Setenv(tt.key, "")
t.Setenv("GOBUILDCACHE_"+tt.key, "")
// Set test-specific environment variables
for k, v := range tt.envVars {
t.Setenv(k, v)
}
result := getEnvFloatWithPrefix(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("getEnvFloatWithPrefix(%q, %v) = %v, want %v", tt.key, tt.defaultValue, result, tt.expected)
}
})
}
}