/
test-config.js
256 lines (182 loc) · 6.48 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
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
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')
t.beforeEach(() => {
delete process.env.BASIC // reset
})
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 existsSyncStub = sinon.stub(fs, 'existsSync').returns(true)
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(existsSyncStub.args[0][0], testPath)
ct.ok(existsSyncStub.called)
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 => {
const logStub = sinon.stub(console, 'log')
dotenv.config({ debug: 'true' })
ct.ok(logStub.called)
logStub.restore()
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)
const 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)
logStub.restore()
readFileSyncStub.restore()
})
t.test('logs any errors parsing when in debug and override mode', ct => {
ct.plan(1)
const logStub = sinon.stub(console, 'log')
dotenv.config({ debug: true, override: true })
ct.ok(logStub.called)
logStub.restore()
})