forked from motdotla/dotenv
/
test-config.js
116 lines (84 loc) · 2.51 KB
/
test-config.js
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
/* @flow */
const fs = require('fs')
const sinon = require('sinon')
const t = require('tap')
const dotenv = require('../lib/main')
const mockParseResponse = { test: 'foo' }
let readFileSyncStub
let parseStub
t.plan(9)
t.beforeEach(done => {
readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
parseStub = sinon.stub(dotenv, 'parse').returns(mockParseResponse)
done()
})
t.afterEach(done => {
readFileSyncStub.restore()
parseStub.restore()
done()
})
t.test('takes option for path', ct => {
ct.plan(1)
const testPath = 'tests/.env'
dotenv.config({ path: testPath })
ct.equal(readFileSyncStub.args[0][0], testPath)
})
t.test('takes option for encoding', ct => {
ct.plan(1)
const testEncoding = 'latin1'
dotenv.config({ encoding: testEncoding })
ct.equal(readFileSyncStub.args[0][1].encoding, testEncoding)
})
t.test('takes option for debug', ct => {
ct.plan(1)
const logStub = sinon.stub(console, 'log')
dotenv.config({ debug: 'true' })
ct.ok(logStub.called)
logStub.restore()
})
t.test('takes option for multiline', ct => {
ct.plan(1)
const testMultiline = 'line-breaks'
dotenv.config({ multiline: testMultiline })
ct.equal(parseStub.args[0][1].multiline, testMultiline)
})
t.test('reads path with encoding, parsing output to process.env', ct => {
ct.plan(2)
const res = dotenv.config()
ct.same(res.parsed, mockParseResponse)
ct.equal(readFileSyncStub.callCount, 1)
})
t.test('does not write over keys already in process.env', ct => {
ct.plan(2)
const existing = 'bar'
process.env.test = existing
// 'foo' returned as value in `beforeEach`. should keep this 'bar'
const env = dotenv.config()
ct.equal(env.parsed && env.parsed.test, mockParseResponse.test)
ct.equal(process.env.test, existing)
})
t.test(
'does not write over keys already in process.env if the key has a falsy value',
ct => {
ct.plan(2)
const existing = ''
process.env.test = existing
// 'foo' returned as value in `beforeEach`. should keep this ''
const env = dotenv.config()
ct.equal(env.parsed && env.parsed.test, mockParseResponse.test)
// NB: process.env.test becomes undefined on Windows
ct.notOk(process.env.test)
}
)
t.test('returns parsed object', ct => {
ct.plan(2)
const env = dotenv.config()
ct.notOk(env.error)
ct.same(env.parsed, mockParseResponse)
})
t.test('returns any errors thrown from reading file or parsing', ct => {
ct.plan(1)
readFileSyncStub.throws()
const env = dotenv.config()
ct.type(env.error, Error)
})