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: f2c5e75498bb1adb768670d2b70880340bc5d710 (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
package env_test

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

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

func TestGetBool(t *testing.T) {
	for _, tc := range []struct {
		value         string
		fallback      bool
		expected      bool
		expectedErrIs error
	}{
		{
			value:    "true",
			expected: true,
		},
		{
			value:    "false",
			expected: false,
		},
		{
			value:    "1",
			expected: true,
		},
		{
			value:    "0",
			expected: false,
		},
		{
			value:    "",
			expected: false,
		},
		{
			value:    "",
			fallback: true,
			expected: true,
		},
		{
			value:         "bad",
			expected:      false,
			expectedErrIs: strconv.ErrSyntax,
		},
		{
			value:         "bad",
			fallback:      true,
			expected:      true,
			expectedErrIs: strconv.ErrSyntax,
		},
	} {
		t.Run(fmt.Sprintf("value=%s,fallback=%t", tc.value, tc.fallback), func(t *testing.T) {
			testhelper.ModifyEnvironment(t, "TEST_BOOL", tc.value)

			result, err := env.GetBool("TEST_BOOL", tc.fallback)

			if tc.expectedErrIs != nil {
				assert.Error(t, err)
				assert.True(t, errors.Is(err, tc.expectedErrIs), err)
			} else {
				assert.NoError(t, err)
			}

			assert.Equal(t, tc.expected, result)
		})
	}
}

func TestGetInt(t *testing.T) {
	for _, tc := range []struct {
		value         string
		fallback      int
		expected      int
		expectedErrIs error
	}{
		{
			value:    "3",
			expected: 3,
		},
		{
			value:    "",
			expected: 0,
		},
		{
			value:    "",
			fallback: 3,
			expected: 3,
		},
		{
			value:         "bad",
			expected:      0,
			expectedErrIs: strconv.ErrSyntax,
		},
		{
			value:         "bad",
			fallback:      3,
			expected:      3,
			expectedErrIs: strconv.ErrSyntax,
		},
	} {
		t.Run(fmt.Sprintf("value=%s,fallback=%d", tc.value, tc.fallback), func(t *testing.T) {
			testhelper.ModifyEnvironment(t, "TEST_INT", tc.value)

			result, err := env.GetInt("TEST_INT", tc.fallback)

			if tc.expectedErrIs != nil {
				assert.Error(t, err)
				assert.True(t, errors.Is(err, tc.expectedErrIs), err)
			} else {
				assert.NoError(t, err)
			}

			assert.Equal(t, tc.expected, result)
		})
	}
}

func TestGetDuration(t *testing.T) {
	for _, tc := range []struct {
		value       string
		fallback    time.Duration
		expected    time.Duration
		expectedErr string
	}{
		{
			value:    "3m",
			fallback: 0,
			expected: 3 * time.Minute,
		},
		{
			value:    "",
			expected: 0,
		},
		{
			value:    "",
			fallback: 3,
			expected: 3,
		},
		{
			value:       "bad",
			expected:    0,
			expectedErr: `get duration TEST_DURATION: time: invalid duration "bad"`,
		},
		{
			value:       "bad",
			fallback:    3,
			expected:    3,
			expectedErr: `get duration TEST_DURATION: time: invalid duration "bad"`,
		},
	} {
		t.Run(fmt.Sprintf("value=%s,fallback=%d", tc.value, tc.fallback), func(t *testing.T) {
			testhelper.ModifyEnvironment(t, "TEST_DURATION", tc.value)

			result, err := env.GetDuration("TEST_DURATION", tc.fallback)

			if tc.expectedErr != "" {
				assert.Error(t, err)
				assert.EqualError(t, err, tc.expectedErr)
			} else {
				assert.NoError(t, err)
			}

			assert.Equal(t, tc.expected, result)
		})
	}
}

func TestGetString(t *testing.T) {
	for _, tc := range []struct {
		value    string
		fallback string
		expected string
	}{
		{
			value:    "Hello",
			expected: "Hello",
		},
		{
			value:    "hello ",
			expected: "hello",
		},
		{
			fallback: "fallback value",
			expected: "fallback value",
		},
		{
			value:    " ",
			fallback: "fallback value",
			expected: "",
		},
	} {
		t.Run(fmt.Sprintf("value=%s,fallback=%s", tc.value, tc.fallback), func(t *testing.T) {
			testhelper.ModifyEnvironment(t, "TEST_STRING", tc.value)

			result := env.GetString("TEST_STRING", tc.fallback)

			assert.Equal(t, tc.expected, result)
		})
	}
}