-
-
Notifications
You must be signed in to change notification settings - Fork 946
Expand file tree
/
Copy pathtest-config.js
More file actions
504 lines (371 loc) · 13.2 KB
/
Copy pathtest-config.js
File metadata and controls
504 lines (371 loc) · 13.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
const fs = require('fs')
const os = require('os')
const path = require('path')
const sinon = require('sinon')
const t = require('tap')
const dotenv = require('../lib/main')
let logStub
t.beforeEach(() => {
logStub = null
delete process.env.BASIC // reset
})
t.afterEach(() => {
if (logStub) logStub.restore()
})
t.test('takes string for path option', ct => {
const testPath = 'tests/.env'
const env = dotenv.config({ path: testPath })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, 'basic')
ct.end()
})
t.test('takes array for path option', ct => {
const testPath = ['tests/.env']
const env = dotenv.config({ path: testPath })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, 'basic')
ct.end()
})
t.test('takes two or more files in the array for path option', ct => {
const testPath = ['tests/.env.local', 'tests/.env']
const env = dotenv.config({ path: testPath })
ct.equal(env.parsed.BASIC, 'local_basic')
ct.equal(process.env.BASIC, 'local_basic')
ct.end()
})
t.test('sets values from both .env.local and .env. first file key wins.', ct => {
delete process.env.SINGLE_QUOTES
const testPath = ['tests/.env.local', 'tests/.env']
const env = dotenv.config({ path: testPath })
// in both files - first file wins (.env.local)
ct.equal(env.parsed.BASIC, 'local_basic')
ct.equal(process.env.BASIC, 'local_basic')
// in .env.local only
ct.equal(env.parsed.LOCAL, 'local')
ct.equal(process.env.LOCAL, 'local')
// in .env only
ct.equal(env.parsed.SINGLE_QUOTES, 'single_quotes')
ct.equal(process.env.SINGLE_QUOTES, 'single_quotes')
ct.end()
})
t.test('sets values from both .env.local and .env. but none is used as value existed in process.env.', ct => {
const testPath = ['tests/.env.local', 'tests/.env']
process.env.BASIC = 'existing'
const env = dotenv.config({ path: testPath })
// does not override process.env
ct.equal(env.parsed.BASIC, 'local_basic')
ct.equal(process.env.BASIC, 'existing')
ct.end()
})
t.test('takes URL for path option', ct => {
const envPath = path.resolve(__dirname, '.env')
const fileUrl = new URL(`file://${envPath}`)
const env = dotenv.config({ path: fileUrl })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, 'basic')
ct.end()
})
t.test('takes option for path along with home directory char ~', ct => {
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
const mockedHomedir = '/Users/dummy'
const homedirStub = sinon.stub(os, 'homedir').returns(mockedHomedir)
const testPath = '~/.env'
dotenv.config({ path: testPath })
ct.equal(readFileSyncStub.args[0][0], path.join(mockedHomedir, '.env'))
ct.ok(homedirStub.called)
homedirStub.restore()
readFileSyncStub.restore()
ct.end()
})
t.test('takes option for encoding', ct => {
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
const testEncoding = 'latin1'
dotenv.config({ encoding: testEncoding })
ct.equal(readFileSyncStub.args[0][1].encoding, testEncoding)
readFileSyncStub.restore()
ct.end()
})
t.test('takes option for debug', ct => {
logStub = sinon.stub(console, 'log')
dotenv.config({ debug: 'true' })
ct.ok(logStub.called)
ct.end()
})
t.test('reads path with encoding, parsing output to process.env', ct => {
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('BASIC=basic')
const parseStub = sinon.stub(dotenv, 'parse').returns({ BASIC: 'basic' })
const res = dotenv.config()
ct.same(res.parsed, { BASIC: 'basic' })
ct.equal(readFileSyncStub.callCount, 1)
readFileSyncStub.restore()
parseStub.restore()
ct.end()
})
t.test('does not write over keys already in process.env', ct => {
const testPath = 'tests/.env'
const existing = 'bar'
process.env.BASIC = existing
const env = dotenv.config({ path: testPath })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, existing)
ct.end()
})
t.test('does write over keys already in process.env if override turned on', ct => {
const testPath = 'tests/.env'
const existing = 'bar'
process.env.BASIC = existing
const env = dotenv.config({ path: testPath, override: true })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, 'basic')
ct.end()
})
t.test('does not write over keys already in process.env if the key has a falsy value', ct => {
const testPath = 'tests/.env'
const existing = ''
process.env.BASIC = existing
const env = dotenv.config({ path: testPath })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, '')
ct.end()
})
t.test('does write over keys already in process.env if the key has a falsy value but override is set to true', ct => {
const testPath = 'tests/.env'
const existing = ''
process.env.BASIC = existing
const env = dotenv.config({ path: testPath, override: true })
ct.equal(env.parsed.BASIC, 'basic')
ct.equal(process.env.BASIC, 'basic')
ct.end()
})
t.test('can write to a different object rather than process.env', ct => {
const testPath = 'tests/.env'
process.env.BASIC = 'other' // reset process.env
const myObject = {}
const env = dotenv.config({ path: testPath, processEnv: myObject })
ct.equal(env.parsed.BASIC, 'basic')
console.log('logging', process.env.BASIC)
ct.equal(process.env.BASIC, 'other')
ct.equal(myObject.BASIC, 'basic')
ct.end()
})
t.test('returns parsed object', ct => {
const testPath = 'tests/.env'
const env = dotenv.config({ path: testPath })
ct.notOk(env.error)
ct.equal(env.parsed.BASIC, 'basic')
ct.end()
})
t.test('returns any errors thrown from reading file or parsing', ct => {
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
readFileSyncStub.throws()
const env = dotenv.config()
ct.type(env.error, Error)
readFileSyncStub.restore()
ct.end()
})
t.test('logs any errors thrown from reading file or parsing when in debug mode', ct => {
ct.plan(2)
logStub = sinon.stub(console, 'log')
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
readFileSyncStub.throws()
const env = dotenv.config({ debug: true })
ct.ok(logStub.called)
ct.type(env.error, Error)
readFileSyncStub.restore()
})
t.test('logs any errors parsing when in debug and override mode', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config({ debug: true, override: true })
ct.ok(logStub.called)
})
t.test('deals with file:// path', ct => {
logStub = sinon.stub(console, 'log')
const testPath = 'file:///tests/.env'
const env = dotenv.config({ path: testPath })
ct.equal(env.parsed.BASIC, undefined)
ct.equal(process.env.BASIC, undefined)
ct.equal(env.error.message, "ENOENT: no such file or directory, open 'file:///tests/.env'")
ct.ok(logStub.called)
ct.end()
})
t.test('deals with file:// path and debug true', ct => {
logStub = sinon.stub(console, 'log')
const testPath = 'file:///tests/.env'
const env = dotenv.config({ path: testPath, debug: true })
ct.equal(env.parsed.BASIC, undefined)
ct.equal(process.env.BASIC, undefined)
ct.equal(env.error.message, "ENOENT: no such file or directory, open 'file:///tests/.env'")
ct.ok(logStub.called)
ct.end()
})
t.test('path.relative fails somehow', ct => {
logStub = sinon.stub(console, 'log')
const pathRelativeStub = sinon.stub(path, 'relative').throws(new Error('fail'))
const testPath = 'file:///tests/.env'
const env = dotenv.config({ path: testPath, debug: true })
ct.equal(env.parsed.BASIC, undefined)
ct.equal(process.env.BASIC, undefined)
ct.equal(env.error.message, 'fail')
ct.ok(logStub.called)
pathRelativeStub.restore()
ct.end()
})
t.test('displays random tips from the tips array', ct => {
ct.plan(2)
const originalTTY = process.stdout.isTTY
process.stdout.isTTY = true
logStub = sinon.stub(console, 'log')
const testPath = 'tests/.env'
// Test that tips are displayed (run config multiple times to see variation)
dotenv.config({ path: testPath })
dotenv.config({ path: testPath })
dotenv.config({ path: testPath })
// Should have at least one call that contains a tip
let foundTip = false
for (const call of logStub.getCalls()) {
if (call.args[0] && call.args[0].includes('tip:')) {
foundTip = true
break
}
}
ct.ok(foundTip, 'Should display a tip')
// Test that the tip contains one of our expected tip messages
let foundExpectedTip = false
const expectedTips = [
'🔐 encrypt with dotenvx: https://dotenvx.com',
'🔐 prevent committing .env to code: https://dotenvx.com/precommit',
'🔐 prevent building .env in docker: https://dotenvx.com/prebuild',
'📡 observe env with Radar: https://dotenvx.com/radar',
'📡 auto-backup env with Radar: https://dotenvx.com/radar',
'📡 version env with Radar: https://dotenvx.com/radar',
'🛠️ run anywhere with `dotenvx run -- yourcommand`',
'⚙️ specify custom .env file path with { path: \'/custom/path/.env\' }',
'⚙️ enable debug logging with { debug: true }',
'⚙️ override existing env vars with { override: true }',
'⚙️ suppress all logs with { quiet: true }',
'⚙️ write to custom object with { processEnv: myObject }',
'⚙️ load multiple .env files with { path: [\'.env.local\', \'.env\'] }'
]
for (const call of logStub.getCalls()) {
if (call.args[0] && call.args[0].includes('tip:')) {
for (const expectedTip of expectedTips) {
if (call.args[0].includes(expectedTip)) {
foundExpectedTip = true
break
}
}
}
}
ct.ok(foundExpectedTip, 'Should display one of the expected tips')
// Restore
process.stdout.isTTY = originalTTY
ct.end()
})
t.test('displays random tips from the tips array with fallback for isTTY false', ct => {
ct.plan(2)
const originalTTY = process.stdout.isTTY
process.stdout.isTTY = undefined
logStub = sinon.stub(console, 'log')
const testPath = 'tests/.env'
// Test that tips are displayed (run config multiple times to see variation)
dotenv.config({ path: testPath })
dotenv.config({ path: testPath })
dotenv.config({ path: testPath })
// Should have at least one call that contains a tip
let foundTip = false
for (const call of logStub.getCalls()) {
if (call.args[0] && call.args[0].includes('tip:')) {
foundTip = true
break
}
}
ct.ok(foundTip, 'Should display a tip')
// Test that the tip contains one of our expected tip messages
let foundExpectedTip = false
const expectedTips = [
'🔐 encrypt with dotenvx: https://dotenvx.com',
'🔐 prevent committing .env to code: https://dotenvx.com/precommit',
'🔐 prevent building .env in docker: https://dotenvx.com/prebuild',
'🛠️ run anywhere with `dotenvx run -- yourcommand`',
'⚙️ specify custom .env file path with { path: \'/custom/path/.env\' }',
'⚙️ enable debug logging with { debug: true }',
'⚙️ override existing env vars with { override: true }',
'⚙️ suppress all logs with { quiet: true }',
'⚙️ write to custom object with { processEnv: myObject }',
'⚙️ load multiple .env files with { path: [\'.env.local\', \'.env\'] }'
]
for (const call of logStub.getCalls()) {
if (call.args[0] && call.args[0].includes('tip:')) {
for (const expectedTip of expectedTips) {
if (call.args[0].includes(expectedTip)) {
foundExpectedTip = true
break
}
}
}
}
ct.ok(foundExpectedTip, 'Should display one of the expected tips')
// Restore
process.stdout.isTTY = originalTTY
ct.end()
})
t.test('logs when no path is set', ct => {
ct.plan(1)
logStub = sinon.stub(console, 'log')
dotenv.config()
ct.ok(logStub.called)
})
t.test('does log by default', ct => {
ct.plan(1)
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath })
ct.ok(logStub.called)
})
t.test('does not log if quiet flag passed true', ct => {
ct.plan(1)
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath, quiet: true })
ct.ok(logStub.notCalled)
})
t.test('does not log if process.env.DOTENV_CONFIG_QUIET is true', ct => {
ct.plan(1)
process.env.DOTENV_CONFIG_QUIET = 'true'
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath })
ct.ok(logStub.notCalled)
delete process.env.DOTENV_CONFIG_QUIET
})
t.test('does log if quiet flag false', ct => {
ct.plan(1)
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath, quiet: false })
ct.ok(logStub.called)
})
t.test('does log if process.env.DOTENV_CONFIG_QUIET is false', ct => {
ct.plan(1)
process.env.DOTENV_CONFIG_QUIET = 'false'
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath })
ct.ok(logStub.called)
delete process.env.DOTENV_CONFIG_QUIET
})
t.test('does log if quiet flag present and undefined/null', ct => {
ct.plan(1)
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath, quiet: undefined })
ct.ok(logStub.called)
})
t.test('logs if debug set', ct => {
ct.plan(1)
const testPath = 'tests/.env'
logStub = sinon.stub(console, 'log')
dotenv.config({ path: testPath, debug: true })
ct.ok(logStub.called)
})