Welcome to mirror list, hosted at ThFree Co, Russian Federation.

env_test.go « env « helper « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9984cf38f0801c71315ae94dca74eadff1a7f691 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package env_test

import (
	"fmt"
	"strconv"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v16/internal/helper/env"
	"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
)

func TestGetBool(t *testing.T) {
	const envvar = "TEST_BOOL"

	for _, tc := range []struct {
		desc          string
		setup         func(t *testing.T)
		fallback      bool
		expectedValue bool
		expectedErr   error
	}{
		{
			desc:          "explicitly true",
			setup:         setupEnv(envvar, "true"),
			expectedValue: true,
		},
		{
			desc:          "explicitly false",
			setup:         setupEnv(envvar, "false"),
			expectedValue: false,
		},
		{
			desc:          "explicitly 1",
			setup:         setupEnv(envvar, "1"),
			expectedValue: true,
		},
		{
			desc:          "explicitly 0",
			setup:         setupEnv(envvar, "0"),
			expectedValue: false,
		},
		{
			desc: "missing value",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			expectedValue: false,
		},
		{
			desc: "missing value with fallback",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			fallback:      true,
			expectedValue: true,
		},
		{
			desc:          "empty value",
			setup:         setupEnv(envvar, ""),
			expectedValue: false,
		},
		{
			desc:          "empty value with fallback",
			setup:         setupEnv(envvar, ""),
			fallback:      true,
			expectedValue: true,
		},
		{
			desc:          "invalid value",
			setup:         setupEnv(envvar, "bad"),
			expectedValue: false,
			expectedErr: fmt.Errorf("get bool TEST_BOOL: %w", &strconv.NumError{
				Func: "ParseBool",
				Num:  "bad",
				Err:  strconv.ErrSyntax,
			}),
		},
		{
			desc:          "invalid value with fallback",
			setup:         setupEnv(envvar, "bad"),
			fallback:      true,
			expectedValue: true,
			expectedErr: fmt.Errorf("get bool TEST_BOOL: %w", &strconv.NumError{
				Func: "ParseBool",
				Num:  "bad",
				Err:  strconv.ErrSyntax,
			}),
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			tc.setup(t)

			value, err := env.GetBool(envvar, tc.fallback)
			require.Equal(t, tc.expectedErr, err)
			require.Equal(t, tc.expectedValue, value)
		})
	}
}

func TestGetInt(t *testing.T) {
	const envvar = "TEST_INT"

	for _, tc := range []struct {
		desc          string
		setup         func(t *testing.T)
		fallback      int
		expectedValue int
		expectedErr   error
	}{
		{
			desc:          "valid number",
			setup:         setupEnv(envvar, "3"),
			expectedValue: 3,
		},
		{
			desc: "unset value",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			expectedValue: 0,
		},
		{
			desc: "unset value with fallback",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			fallback:      3,
			expectedValue: 3,
		},
		{
			desc:          "empty value",
			setup:         setupEnv(envvar, ""),
			expectedValue: 0,
		},
		{
			desc:          "empty value with fallback",
			setup:         setupEnv(envvar, ""),
			fallback:      3,
			expectedValue: 3,
		},
		{
			desc:          "invalid value",
			setup:         setupEnv(envvar, "bad"),
			expectedValue: 0,
			expectedErr: fmt.Errorf("get int TEST_INT: %w", &strconv.NumError{
				Func: "Atoi",
				Num:  "bad",
				Err:  strconv.ErrSyntax,
			}),
		},
		{
			desc:          "invalid value with fallback",
			setup:         setupEnv(envvar, "bad"),
			fallback:      3,
			expectedValue: 3,
			expectedErr: fmt.Errorf("get int TEST_INT: %w", &strconv.NumError{
				Func: "Atoi",
				Num:  "bad",
				Err:  strconv.ErrSyntax,
			}),
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			tc.setup(t)

			value, err := env.GetInt(envvar, tc.fallback)
			require.Equal(t, tc.expectedErr, err)
			require.Equal(t, tc.expectedValue, value)
		})
	}
}

func TestGetDuration(t *testing.T) {
	const envvar = "TEST_DURATION"

	for _, tc := range []struct {
		desc          string
		setup         func(t *testing.T)
		fallback      time.Duration
		expectedValue time.Duration
		expectedErr   error
	}{
		{
			desc:          "valid duration",
			setup:         setupEnv(envvar, "3m"),
			fallback:      0,
			expectedValue: 3 * time.Minute,
		},
		{
			desc: "unset value",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			expectedValue: 0,
		},
		{
			desc: "unset value with fallback",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			fallback:      3,
			expectedValue: 3,
		},
		{
			desc:          "empty value",
			setup:         setupEnv(envvar, ""),
			expectedValue: 0,
		},
		{
			desc:          "empty value with fallback",
			setup:         setupEnv(envvar, ""),
			fallback:      3,
			expectedValue: 3,
		},
		{
			desc:          "invalid value",
			setup:         setupEnv(envvar, "bad"),
			expectedValue: 0,
			expectedErr:   fmt.Errorf("get duration TEST_DURATION: %w", fmt.Errorf("time: invalid duration \"bad\"")),
		},
		{
			desc:          "invalid value with fallback",
			setup:         setupEnv(envvar, "bad"),
			fallback:      3,
			expectedValue: 3,
			expectedErr:   fmt.Errorf("get duration TEST_DURATION: %w", fmt.Errorf("time: invalid duration \"bad\"")),
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			tc.setup(t)

			value, err := env.GetDuration(envvar, tc.fallback)
			require.Equal(t, tc.expectedErr, err)
			require.Equal(t, tc.expectedValue, value)
		})
	}
}

func TestGetString(t *testing.T) {
	const envvar = "TEST_STRING"

	for _, tc := range []struct {
		desc          string
		setup         func(t *testing.T)
		fallback      string
		expectedValue string
	}{
		{
			desc:          "simple string",
			setup:         setupEnv(envvar, "Hello"),
			expectedValue: "Hello",
		},
		{
			desc:          "string with trailing whitespace",
			setup:         setupEnv(envvar, "hello "),
			expectedValue: "hello",
		},
		{
			desc: "unset value",
			setup: func(t *testing.T) {
				testhelper.Unsetenv(t, envvar)
			},
			fallback:      "fallback value",
			expectedValue: "fallback value",
		},
		{
			desc:          "empty value",
			setup:         setupEnv(envvar, ""),
			fallback:      "fallback value",
			expectedValue: "fallback value",
		},
		{
			desc:          "whitespace only",
			setup:         setupEnv(envvar, " "),
			fallback:      "fallback value",
			expectedValue: "",
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			tc.setup(t)

			value := env.GetString(envvar, tc.fallback)
			require.Equal(t, tc.expectedValue, value)
		})
	}
}

func TestExtractKey(t *testing.T) {
	for _, tc := range []struct {
		desc          string
		environment   []string
		key           string
		expectedValue string
	}{
		{
			desc:          "nil",
			environment:   nil,
			key:           "something",
			expectedValue: "",
		},
		{
			desc: "found",
			environment: []string{
				"FOO=bar",
				"BAR=qux",
			},
			key:           "BAR",
			expectedValue: "qux",
		},
		{
			desc: "found with multiple matches",
			environment: []string{
				"FOO=1",
				"FOO=2",
			},
			key:           "FOO",
			expectedValue: "2",
		},
		{
			desc: "not found",
			environment: []string{
				"FOO=bar",
				"BAR=qux",
			},
			key:           "doesnotexist",
			expectedValue: "",
		},
		{
			desc: "prefix-match",
			environment: []string{
				"FOObar=value",
			},
			key:           "FOO",
			expectedValue: "",
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			value := env.ExtractValue(tc.environment, tc.key)
			require.Equal(t, tc.expectedValue, value)
		})
	}
}

func setupEnv(key, value string) func(t *testing.T) {
	return func(t *testing.T) {
		t.Setenv(key, value)
	}
}