-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_test.go
274 lines (249 loc) · 7.09 KB
/
load_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
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
package env
import (
"bytes"
"errors"
"io"
"io/fs"
"os"
"testing"
"github.com/blugnu/test"
)
func fakeFile(content string) fileReader {
return io.NopCloser(bytes.NewReader([]byte(content)))
}
func TestLoad(t *testing.T) {
// ARRANGE
testcases := []struct {
scenario string
args []any
exec func(t *testing.T)
}{
{scenario: "no arguments/.env exists",
exec: func(t *testing.T) {
// ARRANGE
readsDotEnv := false
defer State().Reset()
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
readsDotEnv = readsDotEnv || path == ".env"
return fakeFile("VAR1=loaded-value-1\nVAR2=loaded-value-2"), nil
})()
os.Clearenv()
os.Setenv("VAR1", "env-value")
// ACT
err := Load()
// ASSERT
test.That(t, err).IsNil()
test.IsTrue(t, readsDotEnv)
test.That(t, os.Getenv("VAR1")).Equals("loaded-value-1")
test.That(t, os.Getenv("VAR2")).Equals("loaded-value-2")
},
},
{scenario: "no arguments/.env does not exist",
exec: func(t *testing.T) {
// ARRANGE
defer State().Reset()
defer test.Using(&newFileReader, func(string) (fileReader, error) {
return nil, os.ErrNotExist
})()
os.Clearenv()
os.Setenv("VAR1", "env-value")
// ACT
err := Load()
// ASSERT
test.Error(t, err).Is(os.ErrNotExist)
test.That(t, os.Getenv("VAR1")).Equals("env-value")
},
},
{scenario: ".env argument/.env does not exist",
exec: func(t *testing.T) {
// ARRANGE
defer State().Reset()
defer test.Using(&newFileReader, func(string) (fileReader, error) {
return nil, os.ErrNotExist
})()
os.Clearenv()
os.Setenv("VAR1", "env-value")
// ACT
err := Load(".env")
// ASSERT
test.Error(t, err).Is(os.ErrNotExist)
test.That(t, os.Getenv("VAR1")).Equals("env-value")
},
},
{scenario: "file path argument/valid file/.env does not exist",
exec: func(t *testing.T) {
// ARRANGE
filesLoaded := []string{}
defer State().Reset()
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
filesLoaded = append(filesLoaded, path)
switch path {
case ".env":
return nil, fs.ErrNotExist
case "test.env":
return fakeFile("VAR1=loaded-value-1\nVAR2=loaded-value-2"), nil
default:
panic("unexpected file path: " + path)
}
})()
os.Clearenv()
os.Setenv("VAR1", "env-value")
// ACT
err := Load("test.env")
// ASSERT
test.That(t, err).IsNil()
test.Slice(t, filesLoaded).Equals([]string{".env", "test.env"})
test.That(t, os.Getenv("VAR1")).Equals("loaded-value-1")
test.That(t, os.Getenv("VAR2")).Equals("loaded-value-2")
},
},
{scenario: "file path argument/valid file/.env exists",
exec: func(t *testing.T) {
// ARRANGE
filesLoaded := []string{}
defer State().Reset()
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
filesLoaded = append(filesLoaded, path)
switch path {
case ".env":
return fakeFile("VAR3=dotenv-value-3"), nil
case "test.env":
return fakeFile("VAR1=loaded-value-1\nVAR2=loaded-value-2"), nil
default:
panic("unexpected file path: " + path)
}
})()
os.Clearenv()
os.Setenv("VAR1", "env-value")
// ACT
err := Load("test.env")
// ASSERT
test.That(t, err).IsNil()
test.That(t, filesLoaded).Equals([]string{".env", "test.env"})
test.That(t, os.Getenv("VAR1")).Equals("loaded-value-1")
test.That(t, os.Getenv("VAR2")).Equals("loaded-value-2")
test.That(t, os.Getenv("VAR3")).Equals("dotenv-value-3")
},
},
{scenario: "file path argument/valid file/.env error",
exec: func(t *testing.T) {
// ARRANGE
dotenverr := errors.New("error reading .env")
filesLoaded := []string{}
defer State().Reset()
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
filesLoaded = append(filesLoaded, path)
switch path {
case ".env":
return nil, dotenverr
case "test.env":
return fakeFile("VAR1=loaded-value-1\nVAR2=loaded-value-2"), nil
default:
panic("unexpected file path: " + path)
}
})()
os.Clearenv()
os.Setenv("VAR1", "env-value")
// ACT
err := Load("test.env")
// ASSERT
test.Error(t, err).Is(dotenverr)
test.That(t, filesLoaded).Equals([]string{".env", "test.env"})
test.That(t, os.Getenv("VAR1")).Equals("loaded-value-1")
test.That(t, os.Getenv("VAR2")).Equals("loaded-value-2")
},
},
{scenario: "explicit .env/before other files",
exec: func(t *testing.T) {
// ARRANGE
filesLoaded := []string{}
defer State().Reset()
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
filesLoaded = append(filesLoaded, path)
switch path {
case ".env":
return fakeFile("VAR=dotenv-value"), nil
case "test.env":
return fakeFile("VAR=test-value"), nil
default:
panic("unexpected file path: " + path)
}
})()
os.Clearenv()
os.Setenv("VAR", "env-value")
// ACT
err := Load(".env", "test.env")
// ASSERT
test.That(t, err).IsNil()
test.That(t, filesLoaded).Equals([]string{".env", "test.env"})
test.That(t, os.Getenv("VAR")).Equals("test-value")
},
},
{scenario: "explicit .env/after other files",
exec: func(t *testing.T) {
// ARRANGE
filesLoaded := []string{}
defer State().Reset()
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
filesLoaded = append(filesLoaded, path)
switch path {
case ".env":
return fakeFile("VAR=dotenv-value"), nil
case "test.env":
return fakeFile("VAR=test-value"), nil
default:
panic("unexpected file path: " + path)
}
})()
os.Clearenv()
os.Setenv("VAR", "env-value")
// ACT
err := Load("test.env", ".env")
// ASSERT
test.That(t, err).IsNil()
test.That(t, filesLoaded).Equals([]string{"test.env", ".env"})
test.That(t, os.Getenv("VAR")).Equals("dotenv-value")
},
},
{scenario: "multiple errors",
exec: func(t *testing.T) {
// ARRANGE
dotenverr := errors.New("error reading .env")
testenverr := errors.New("error reading test.env")
defer test.Using(&newFileReader, func(path string) (fileReader, error) {
switch path {
case ".env":
return nil, dotenverr
case "test.env":
return nil, testenverr
default:
panic("unexpected file path: " + path)
}
})()
// ACT
err := Load("test.env")
// ASSERT
test.Error(t, err).Is(dotenverr)
test.Error(t, err).Is(testenverr)
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
tc.exec(t)
})
}
}
func TestLoadFile_WithEmptyLinesAndComments(t *testing.T) {
// ARRANGE
defer State().Reset()
defer test.Using(&newFileReader, func(string) (fileReader, error) {
return fakeFile("VAR1=value-1\n\n# comment\nVAR2=value-2=with-equals"), nil
})()
// ACT
err := loadFile("test.env")
// ASSERT
test.That(t, err).IsNil()
test.That(t, os.Getenv("VAR1")).Equals("value-1")
test.That(t, os.Getenv("VAR2")).Equals("value-2=with-equals")
}