-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotenv_test.go
More file actions
341 lines (299 loc) · 7.61 KB
/
dotenv_test.go
File metadata and controls
341 lines (299 loc) · 7.61 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
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
package dotenv
import (
"strings"
"testing"
)
func TestBasicParsing(t *testing.T) {
content := `KEY1=value1
KEY2=value2
KEY3=`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"KEY1": "value1",
"KEY2": "value2",
"KEY3": "",
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%s, got %s=%s", k, v, k, env[k])
}
}
}
func TestExportSyntax(t *testing.T) {
content := `export KEY1=value1
export KEY2="value2"
KEY3=value3`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"KEY1": "value1",
"KEY2": "value2",
"KEY3": "value3",
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%s, got %s=%s", k, v, k, env[k])
}
}
}
func TestUnquotedValuesWithSpaces(t *testing.T) {
content := `KEY1=some value with spaces
KEY2=value with trailing spaces
KEY3= value with leading spaces`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"KEY1": "some value with spaces",
"KEY2": "value with trailing spaces", // trailing spaces should be trimmed
"KEY3": "value with leading spaces", // leading spaces after = are skipped by whitespace parsing
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
}
func TestQuotedStrings(t *testing.T) {
content := `DOUBLE_QUOTED="value with spaces"
SINGLE_QUOTED='value with spaces'
DOUBLE_WITH_ESCAPES="line1\nline2\ttab"
SINGLE_NO_ESCAPES='line1\nline2\ttab'
EMPTY_DOUBLE=""
EMPTY_SINGLE=''`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"DOUBLE_QUOTED": "value with spaces",
"SINGLE_QUOTED": "value with spaces",
"DOUBLE_WITH_ESCAPES": "line1\nline2\ttab",
"SINGLE_NO_ESCAPES": "line1\\nline2\\ttab", // escapes should be literal in single quotes
"EMPTY_DOUBLE": "",
"EMPTY_SINGLE": "",
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
}
func TestInlineComments(t *testing.T) {
content := `KEY1=value # this is a comment
KEY2="quoted value" # this is also a comment
KEY3='single quoted' # comment after single quotes
KEY4="string with # hash inside"
KEY5='string with # hash inside single quotes'
# Full line comment
KEY6=value_without_comment`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"KEY1": "value",
"KEY2": "quoted value",
"KEY3": "single quoted",
"KEY4": "string with # hash inside",
"KEY5": "string with # hash inside single quotes",
"KEY6": "value_without_comment",
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
}
func TestEscapeSequences(t *testing.T) {
content := `NEWLINE="line1\nline2"
TAB="col1\tcol2"
BACKSLASH="path\\to\\file"
QUOTE="say \"hello\""
UNKNOWN_ESCAPE="test\x"`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"NEWLINE": "line1\nline2",
"TAB": "col1\tcol2",
"BACKSLASH": "path\\to\\file",
"QUOTE": "say \"hello\"",
"UNKNOWN_ESCAPE": "test\\x", // unknown escapes should preserve backslash
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
}
func TestVariableExpansion(t *testing.T) {
content := `BASE_DIR=/app
HOME_DIR=/home/user
PATH_ORIG=/usr/bin
PATH="${HOME_DIR}/bin:${PATH_ORIG}"
CONFIG_PATH="${HOME_DIR}/config"
NESTED="${PATH}:${BASE_DIR}/bin"
NO_EXPAND='$HOME_DIR/literal'`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"BASE_DIR": "/app",
"HOME_DIR": "/home/user",
"PATH": "/home/user/bin:/usr/bin", // Should expand HOME_DIR and PATH_ORIG
"CONFIG_PATH": "/home/user/config", // Should expand HOME_DIR
"NESTED": "/home/user/bin:/usr/bin:/app/bin", // Should expand PATH and BASE_DIR
"NO_EXPAND": "$HOME_DIR/literal", // Single quotes should prevent expansion
"PATH_ORIG": "/usr/bin",
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
}
func TestEmptyValues(t *testing.T) {
content := `EMPTY1=
EMPTY2=""
EMPTY3=''
WHITESPACE_ONLY= `
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expected := map[string]string{
"EMPTY1": "",
"EMPTY2": "",
"EMPTY3": "",
"WHITESPACE_ONLY": "", // unquoted whitespace-only should be trimmed to empty
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
}
func TestErrorCases(t *testing.T) {
testCases := []struct {
name string
content string
wantErr bool
}{
{
name: "missing equals",
content: "KEY_WITHOUT_EQUALS",
wantErr: true,
},
{
name: "unterminated double quote",
content: `KEY="unterminated`,
wantErr: true,
},
{
name: "unterminated single quote",
content: `KEY='unterminated`,
wantErr: true,
},
{
name: "invalid key",
content: "123INVALID=value",
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
parser := NewParser(tc.content)
_, err := parser.Parse()
if tc.wantErr && err == nil {
t.Errorf("Expected error but got none")
}
if !tc.wantErr && err != nil {
t.Errorf("Unexpected error: %v", err)
}
})
}
}
func TestComplexRealWorldExample(t *testing.T) {
content := `# Database configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=user
DB_PASSWORD="p@ssw0rd with spaces"
# API Configuration
export API_KEY="abc123def456"
API_URL="https://api.example.com/v1"
API_TIMEOUT=30
# File paths
LOG_DIR=/var/log/myapp
CONFIG_FILE="${LOG_DIR}/config.json"
BACKUP_PATH='$HOME/backups' # Single quotes = literal
# Feature flags
ENABLE_DEBUG=true
ENABLE_CACHE=false
# Empty and commented
OPTIONAL_KEY=
# COMMENTED_OUT=value
# Multi-line simulation with escapes
MESSAGE="Line 1\nLine 2\tTabbed"
`
parser := NewParser(content)
env, err := parser.Parse()
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
// Verify some key entries
expectedValues := map[string]string{
"DB_HOST": "localhost",
"DB_PASSWORD": "p@ssw0rd with spaces",
"API_KEY": "abc123def456",
"CONFIG_FILE": "/var/log/myapp/config.json",
"BACKUP_PATH": "$HOME/backups",
"ENABLE_DEBUG": "true",
"OPTIONAL_KEY": "",
"MESSAGE": "Line 1\nLine 2\tTabbed",
}
for k, v := range expectedValues {
if env[k] != v {
t.Errorf("Expected %s=%q, got %s=%q", k, v, k, env[k])
}
}
// Verify commented out key doesn't exist
if _, exists := env["COMMENTED_OUT"]; exists {
t.Errorf("COMMENTED_OUT should not exist in parsed environment")
}
}
func TestLoadFromReader(t *testing.T) {
content := "KEY1=value1\nKEY2=value2"
reader := strings.NewReader(content)
env, err := LoadFromReader(reader)
if err != nil {
t.Fatalf("LoadFromReader failed: %v", err)
}
expected := map[string]string{
"KEY1": "value1",
"KEY2": "value2",
}
for k, v := range expected {
if env[k] != v {
t.Errorf("Expected %s=%s, got %s=%s", k, v, k, env[k])
}
}
}