-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotenvTest.js
More file actions
351 lines (278 loc) · 14.9 KB
/
Copy pathDotenvTest.js
File metadata and controls
351 lines (278 loc) · 14.9 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
const { mkdtempSync, rmdirSync, unlinkSync, writeFileSync } = require('fs');
const { expect } = require('chai');
const { randomBytes } = require('crypto');
const Dotenv = Jymfony.Component.Dotenv.Dotenv;
const FormatException = Jymfony.Component.Dotenv.Exception.FormatException;
const PathException = Jymfony.Component.Dotenv.Exception.PathException;
describe('[Dotenv] Dotenv', function () {
let i = 0;
const getEnvDataWithFormatErrors = function * () {
const tests = [
[ 'FOO=BAR BAZ', 'A value containing spaces must be surrounded by quotes in ".env" at line 1.\n...FOO=BAR BAZ...\n ^ line 1 offset 11' ],
[ 'FOO BAR=BAR', 'Whitespace characters are not supported after the variable name in ".env" at line 1.\n...FOO BAR=BAR...\n ^ line 1 offset 3' ],
[ 'FOO', 'Missing = in the environment variable declaration in ".env" at line 1.\n...FOO...\n ^ line 1 offset 3' ],
[ 'FOO="foo', 'Missing quote to end the value in ".env" at line 1.\n...FOO="foo...\n ^ line 1 offset 8' ],
[ 'FOO=\'foo', 'Missing quote to end the value in ".env" at line 1.\n...FOO=\'foo...\n ^ line 1 offset 8' ],
[ 'FOO="foo\nBAR="bar"', 'Missing quote to end the value in ".env" at line 1.\n...FOO="foo\\nBAR="bar"...\n ^ line 1 offset 18' ],
[ 'FOO=\'foo\n', 'Missing quote to end the value in ".env" at line 1.\n...FOO=\'foo\\n...\n ^ line 1 offset 9' ],
[ 'export FOO', 'Unable to unset an environment variable in ".env" at line 1.\n...export FOO...\n ^ line 1 offset 10' ],
[ 'FOO=${FOO', 'Unclosed braces on variable expansion in ".env" at line 1.\n...FOO=\${FOO...\n ^ line 1 offset 9' ],
[ 'FOO= BAR', 'Whitespace are not supported before the value in ".env" at line 1.\n...FOO= BAR...\n ^ line 1 offset 4' ],
[ 'Стасян', 'Invalid character in variable name in ".env" at line 1.\n...Стасян...\n ^ line 1 offset 0' ],
[ 'FOO!', 'Missing = in the environment variable declaration in ".env" at line 1.\n...FOO!...\n ^ line 1 offset 3' ],
[ 'FOO=$(echo foo', 'Missing closing parenthesis. in ".env" at line 1.\n...FOO=$(echo foo...\n ^ line 1 offset 14' ],
[ 'FOO=$(echo foo\n', 'Missing closing parenthesis. in ".env" at line 1.\n...FOO=$(echo foo\\n...\n ^ line 1 offset 14' ],
[ 'FOO=\nBAR=\${FOO:-\\\'a{a}a}', 'Unsupported character "\'" found in the default value of variable "\$FOO". in ".env" at line 2.\n...\\nBAR=\${FOO:-\\\'a{a}a}...\n ^ line 2 offset 24' ],
[ 'FOO=\nBAR=\${FOO:-a\$a}', 'Unsupported character "\$" found in the default value of variable "\$FOO". in ".env" at line 2.\n...FOO=\\nBAR=\${FOO:-a\$a}...\n ^ line 2 offset 20' ],
[ 'FOO=\nBAR=\${FOO:-a"a}', 'Unclosed braces on variable expansion in ".env" at line 2.\n...FOO=\\nBAR=\${FOO:-a"a}...\n ^ line 2 offset 17' ],
];
// If (__jymfony.Platform.isWindows()) {
// Tests.push([ 'FOO=$((1dd2))', 'Issue expanding a command (%s\n) in ".env" at line 1.\n...FOO=$((1dd2))...\n ^ line 1 offset 13' ]);
// }
yield * tests;
};
for (const [ data, error ] of getEnvDataWithFormatErrors()) {
const j = ++i;
it('should parse with format error #' + j, () => {
const dotenv = new Dotenv();
expect(() => dotenv.parse(data)).to.throw(FormatException, error);
});
}
const getEnvData = function * () {
process.env.LOCAL = 'local';
process.env.REMOTE = 'remote';
process.env.SERVERVAR = 'servervar';
const tests = [
// Backslashes
[ 'FOO=foo\\\\bar', {'FOO': 'foo\\bar'} ],
[ 'FOO=\'foo\\\\bar\'', {'FOO': 'foo\\\\bar'} ],
[ 'FOO="foo\\\\bar"', {'FOO': 'foo\\bar'} ],
// Escaped backslash in front of variable
[ 'BAR=bar\nFOO=foo\\\\\$BAR', {'BAR': 'bar', 'FOO': 'foo\\bar'} ],
[ 'BAR=bar\nFOO=\'foo\\\\\$BAR\'', {'BAR': 'bar', 'FOO': 'foo\\\\$BAR'} ],
[ 'BAR=bar\nFOO="foo\\\\\$BAR"', {'BAR': 'bar', 'FOO': 'foo\\bar'} ],
[ 'FOO=foo\\\\\\$BAR', {'FOO': 'foo\\$BAR'} ],
[ 'FOO=\'foo\\\\\\$BAR\'', {'FOO': 'foo\\\\\\$BAR'} ],
[ 'FOO="foo\\\\\\$BAR"', {'FOO': 'foo\\$BAR'} ],
// Spaces
[ 'FOO=bar', {'FOO': 'bar'} ],
[ ' FOO=bar ', {'FOO': 'bar'} ],
[ 'FOO=', {'FOO': ''} ],
[ 'FOO=\n\n\nBAR=bar', {'FOO': '', 'BAR': 'bar'} ],
[ 'FOO= ', {'FOO': ''} ],
[ 'FOO=\nBAR=bar', {'FOO': '', 'BAR': 'bar'} ],
// Newlines
[ '\n\nFOO=bar\r\n\n', {'FOO': 'bar'} ],
[ 'FOO=bar\r\nBAR=foo', {'FOO': 'bar', 'BAR': 'foo'} ],
[ 'FOO=bar\rBAR=foo', {'FOO': 'bar', 'BAR': 'foo'} ],
[ 'FOO=bar\nBAR=foo', {'FOO': 'bar', 'BAR': 'foo'} ],
// Quotes
[ 'FOO="bar"\n', {'FOO': 'bar'} ],
[ 'FOO="bar\'foo"\n', {'FOO': 'bar\'foo'} ],
[ 'FOO=\'bar\'\n', {'FOO': 'bar'} ],
[ 'FOO=\'bar"foo\'\n', {'FOO': 'bar"foo'} ],
[ 'FOO="bar\\"foo"\n', {'FOO': 'bar"foo'} ],
[ 'FOO="bar\\nfoo"', {'FOO': 'bar\nfoo'} ],
[ 'FOO="bar\\rfoo"', {'FOO': 'bar\rfoo'} ],
[ 'FOO=\'bar\\nfoo\'', {'FOO': 'bar\\nfoo'} ],
[ 'FOO=\'bar\\rfoo\'', {'FOO': 'bar\\rfoo'} ],
[ 'FOO=\'bar\nfoo\'', {'FOO': 'bar\nfoo'} ],
[ 'FOO=" FOO "', {'FOO': ' FOO '} ],
[ 'FOO=" "', {'FOO': ' '} ],
[ 'PATH="c:\\\\"', {'PATH': 'c:\\'} ],
[ 'FOO="bar\nfoo"', {'FOO': 'bar\nfoo'} ],
[ 'FOO=BAR\\"', {'FOO': 'BAR"'} ],
[ 'FOO=BAR\\\'BAZ', {'FOO': 'BAR\'BAZ'} ],
[ 'FOO=\\"BAR', {'FOO': '"BAR'} ],
// Concatenated values
[ 'FOO=\'bar\'\'foo\'\n', {'FOO': 'barfoo'} ],
[ 'FOO=\'bar \'\' baz\'', {'FOO': 'bar baz'} ],
[ 'FOO=bar\nBAR=\'baz\'"\$FOO"', {'FOO': 'bar', 'BAR': 'bazbar'} ],
[ 'FOO=\'bar \'\\\'\' baz\'', {'FOO': 'bar \' baz'} ],
// Comments
[ '#FOO=bar\nBAR=foo', {'BAR': 'foo'} ],
[ '#FOO=bar # Comment\nBAR=foo', {'BAR': 'foo'} ],
[ 'FOO=\'bar foo\' # Comment', {'FOO': 'bar foo'} ],
[ 'FOO=\'bar#foo\' # Comment', {'FOO': 'bar#foo'} ],
[ '# Comment\r\nFOO=bar\n# Comment\nBAR=foo', {'FOO': 'bar', 'BAR': 'foo'} ],
[ 'FOO=bar # Another comment\nBAR=foo', {'FOO': 'bar', 'BAR': 'foo'} ],
[ 'FOO=\n\n# comment\nBAR=bar', {'FOO': '', 'BAR': 'bar'} ],
[ 'FOO=NOT#COMMENT', {'FOO': 'NOT#COMMENT'} ],
[ 'FOO= # Comment', {'FOO': ''} ],
// Edge cases (no conversions, only strings as values)
[ 'FOO=0', {'FOO': '0'} ],
[ 'FOO=false', {'FOO': 'false'} ],
[ 'FOO=null', {'FOO': 'null'} ],
// Export
[ 'export FOO=bar', {'FOO': 'bar'} ],
[ ' export FOO=bar', {'FOO': 'bar'} ],
// Variable expansion
[ 'FOO=BAR\nBAR=\$FOO', {'FOO': 'BAR', 'BAR': 'BAR'} ],
[ 'FOO=BAR\nBAR="\$FOO"', {'FOO': 'BAR', 'BAR': 'BAR'} ],
[ 'FOO=BAR\nBAR=\'\$FOO\'', {'FOO': 'BAR', 'BAR': '$FOO'} ],
[ 'FOO_BAR9=BAR\nBAR=\$FOO_BAR9', {'FOO_BAR9': 'BAR', 'BAR': 'BAR'} ],
[ 'FOO=BAR\nBAR=\${FOO}Z', {'FOO': 'BAR', 'BAR': 'BARZ'} ],
[ 'FOO=BAR\nBAR=\$FOO}', {'FOO': 'BAR', 'BAR': 'BAR}'} ],
[ 'FOO=BAR\nBAR=\\\$FOO', {'FOO': 'BAR', 'BAR': '$FOO'} ],
[ 'FOO=" \\$ "', {'FOO': ' $ '} ],
[ 'FOO=" $ "', {'FOO': ' $ '} ],
[ 'BAR=$LOCAL', {'BAR': 'local'} ],
[ 'BAR=$REMOTE', {'BAR': 'remote'} ],
[ 'BAR=$SERVERVAR', {'BAR': 'servervar'} ],
[ 'FOO=$NOTDEFINED', {'FOO': ''} ],
[ 'FOO=BAR\nBAR=\${FOO:-TEST}', {'FOO': 'BAR', 'BAR': 'BAR'} ],
[ 'FOO=BAR\nBAR=\${NOTDEFINED:-TEST}', {'FOO': 'BAR', 'BAR': 'TEST'} ],
[ 'FOO=\nBAR=\${FOO:-TEST}', {'FOO': '', 'BAR': 'TEST'} ],
[ 'FOO=\nBAR=\$FOO:-TEST}', {'FOO': '', 'BAR': 'TEST}'} ],
[ 'FOO=BAR\nBAR=\${FOO:=TEST}', {'FOO': 'BAR', 'BAR': 'BAR'} ],
[ 'FOO=BAR\nBAR=\${NOTDEFINED:=TEST}', {'FOO': 'BAR', 'NOTDEFINED': 'TEST', 'BAR': 'TEST'} ],
[ 'FOO=\nBAR=\${FOO:=TEST}', {'FOO': 'TEST', 'BAR': 'TEST'} ],
[ 'FOO=\nBAR=\$FOO:=TEST}', {'FOO': 'TEST', 'BAR': 'TEST}'} ],
[ 'FOO=foo\nFOOBAR=\${FOO}\${BAR}', {'FOO': 'foo', 'FOOBAR': 'foo'} ],
];
// If (! __jymfony.Platform.isWindows()) {
// Tests.push(
// // command expansion
// ['FOO=$(echo foo)', {'FOO': 'foo'}],
// ['FOO=$((1+2))', {'FOO': '3'}],
// ['FOO=FOO$((1+2))BAR', {'FOO': 'FOO3BAR'}],
// ['FOO=$(echo "$(echo "$(echo "$(echo foo)")")")', {'FOO': 'foo'}],
// ["FOO=$(echo \"Quotes won't be a problem\")", {'FOO': 'Quotes won\'t be a problem'}],
// ["FOO=bar\nBAR=$(echo \"FOO is \$FOO\")", {'FOO': 'bar', 'BAR': 'FOO is bar'}],
// );
// }
yield * tests;
};
i = 0;
for (const [ data, expected ] of getEnvData()) {
const j = ++i;
it ('should parse correctly #' + j, () => {
const dotenv = new Dotenv();
expect(dotenv.parse(data)).to.be.deep.equal(expected);
});
}
it ('load should work', () => {
delete process.env.FOO;
delete process.env.BAR;
const tmpDir = mkdtempSync('jf-dotenv');
const path1 = tmpDir + '/jf-' + randomBytes(4).readUInt32LE(0);
const path2 = tmpDir + '/jf-' + randomBytes(4).readUInt32LE(0);
writeFileSync(path1, 'FOO=BAR');
writeFileSync(path2, 'BAR=BAZ');
(new Dotenv()).load(path1, path2);
const foo = process.env.FOO;
const bar = process.env.BAR;
delete process.env.FOO;
delete process.env.BAR;
unlinkSync(path1);
unlinkSync(path2);
rmdirSync(tmpDir);
expect(foo).to.be.equal('BAR');
expect(bar).to.be.equal('BAZ');
});
it ('loadEnv should work', () => {
delete process.env.FOO;
delete process.env.BAR;
const tmpDir = mkdtempSync('jf-dotenv');
const path = tmpDir + '/jf-' + randomBytes(4).readUInt32LE(0);
// .env
writeFileSync(path, 'FOO=BAR');
(new Dotenv()).loadEnv(path, 'TEST_APP_ENV');
expect(process.env.FOO).to.be.equal('BAR');
expect(process.env.TEST_APP_ENV).to.be.equal('dev');
// .env.local
process.env.TEST_APP_ENV = 'local';
writeFileSync(path + '.local', 'FOO=localBAR');
(new Dotenv()).loadEnv(path, 'TEST_APP_ENV');
expect(process.env.FOO).to.be.equal('localBAR');
// Special case for test
process.env.TEST_APP_ENV = 'test';
(new Dotenv()).loadEnv(path, 'TEST_APP_ENV');
expect(process.env.FOO).to.be.equal('BAR');
// .env.dev
delete process.env.TEST_APP_ENV;
writeFileSync(path + '.dev', 'FOO=devBAR');
(new Dotenv()).loadEnv(path, 'TEST_APP_ENV');
expect(process.env.FOO).to.be.equal('devBAR');
// .env.dev.local
writeFileSync(path + '.dev.local', 'FOO=devlocalBAR');
(new Dotenv()).loadEnv(path, 'TEST_APP_ENV');
expect(process.env.FOO).to.be.equal('devlocalBAR');
// .env.dist
unlinkSync(path);
writeFileSync(path + '.dist', 'BAR=distBAR');
(new Dotenv()).loadEnv(path, 'TEST_APP_ENV');
expect(process.env.BAR).to.be.equal('distBAR');
delete process.env.FOO;
delete process.env.BAR;
unlinkSync(path + '.dist');
unlinkSync(path + '.local');
unlinkSync(path + '.dev');
unlinkSync(path + '.dev.local');
rmdirSync(tmpDir);
});
it ('overload should work properly', () => {
process.env.FOO = 'initial_foo_value';
process.env.BAR = 'initial_bar_value';
const tmpDir = mkdtempSync('jf-dotenv');
const path1 = tmpDir + '/jf-' + randomBytes(4).readUInt32LE(0);
const path2 = tmpDir + '/jf-' + randomBytes(4).readUInt32LE(0);
writeFileSync(path1, 'FOO=BAR');
writeFileSync(path2, 'BAR=BAZ');
(new Dotenv()).overload(path1, path2);
const foo = process.env.FOO;
const bar = process.env.BAR;
delete process.env.FOO;
delete process.env.BAR;
unlinkSync(path1);
unlinkSync(path2);
rmdirSync(tmpDir);
expect(foo).to.be.equal('BAR');
expect(bar).to.be.equal('BAZ');
});
it ('trying to load non-file should throw', () => {
const dotenv = new Dotenv();
expect(() => dotenv.load(__dirname)).to.throw(PathException);
});
it ('should memorize loaded var names in special var', () => {
// Special variable not exists
delete process.env.JYMFONY_DOTENV_VARS;
delete process.env.APP_DEBUG;
delete process.env.DATABASE_URL;
let dotenv = new Dotenv();
dotenv.populate({ APP_DEBUG: '1', DATABASE_URL: 'mysql://root@localhost/db' });
expect(process.env.JYMFONY_DOTENV_VARS).to.be.equal('APP_DEBUG,DATABASE_URL');
// Special variable has a value
process.env.JYMFONY_DOTENV_VARS = 'APP_ENV';
process.env.APP_DEBUG = '1';
delete process.env.DATABASE_URL;
dotenv = new Dotenv();
dotenv.populate({ APP_DEBUG: '0', DATABASE_URL: 'mysql://root@localhost/db' });
dotenv.populate({ DATABASE_URL: 'sqlite:///somedb.sqlite' });
expect(process.env.JYMFONY_DOTENV_VARS).to.be.equal('APP_ENV,DATABASE_URL');
});
it ('should get variables from env first', () => {
delete process.env.JYMFONY_DOTENV_VARS;
process.env.APP_ENV = 'prod';
const dotenv = new Dotenv();
const test = 'APP_ENV=dev\nTEST1=foo1_\${APP_ENV}';
const values = dotenv.parse(test);
expect(values.TEST1).to.be.equal('foo1_prod');
});
it ('bootEnv should work', () => {
const tmpDir = mkdtempSync('jf-dotenv');
const path = tmpDir + '/jf-' + randomBytes(4).readUInt32LE(0);
writeFileSync(path, 'FOO=BAR');
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG')).bootEnv(path);
expect(process.env.FOO).to.be.equal('BAR');
delete process.env.FOO;
unlinkSync(path);
writeFileSync(path + '.local.js', 'export default { TEST_APP_ENV: "dev", FOO: "BAR" };');
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG')).bootEnv(path);
expect(process.env.FOO).to.be.equal('BAR');
expect(process.env.TEST_APP_DEBUG).to.be.equal('1');
delete process.env.FOO;
unlinkSync(path + '.local.js');
rmdirSync(tmpDir);
});
});