-
Notifications
You must be signed in to change notification settings - Fork 28.3k
/
test-process-load-env-file.js
103 lines (92 loc) Β· 3.48 KB
/
test-process-load-env-file.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
'use strict';
const common = require('../common');
const fixtures = require('../../test/common/fixtures');
const assert = require('node:assert');
const { describe, it } = require('node:test');
const { join } = require('node:path');
const basicValidEnvFilePath = fixtures.path('dotenv/basic-valid.env');
const validEnvFilePath = fixtures.path('dotenv/valid.env');
const missingEnvFile = fixtures.path('dotenv/non-existent-file.env');
describe('process.loadEnvFile()', () => {
it('supports passing path', async () => {
const code = `
process.loadEnvFile(${JSON.stringify(validEnvFilePath)});
const assert = require('assert');
assert.strictEqual(process.env.BASIC, 'basic');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ '--eval', code ],
{ cwd: __dirname },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('supports not-passing a path', async () => {
// Uses `../fixtures/dotenv/.env` file.
const code = `
process.loadEnvFile();
const assert = require('assert');
assert.strictEqual(process.env.BASIC, 'basic');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ '--eval', code ],
{ cwd: fixtures.path('dotenv/') },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('should throw when file does not exist', async () => {
assert.throws(() => {
process.loadEnvFile(missingEnvFile);
}, { code: 'ENOENT', syscall: 'open', path: missingEnvFile });
});
// The whole chdir flow here is to address a case where a developer
// has a .env in the worktree which is probably in the global .gitignore.
// In that case this test would fail. To avoid confusion, chdir to lib will
// make this way less likely to happen. Probably a temporary directory would
// be the best choice but given how edge this case is this is fine.
it('should throw when `.env` does not exist', async () => {
const originalCwd = process.cwd();
try {
process.chdir(join(originalCwd, 'lib'));
assert.throws(() => {
process.loadEnvFile();
}, { code: 'ENOENT', syscall: 'open', path: '.env' });
} finally {
process.chdir(originalCwd);
}
});
it('should check for permissions', async () => {
const code = `
process.loadEnvFile(${JSON.stringify(missingEnvFile)});
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ '--eval', code, '--experimental-permission' ],
{ cwd: __dirname },
);
assert.match(child.stderr, /Error: Access to this API has been restricted/);
assert.match(child.stderr, /code: 'ERR_ACCESS_DENIED'/);
assert.match(child.stderr, /permission: 'FileSystemRead'/);
if (!common.isWindows) {
assert(child.stderr.includes(`resource: '${JSON.stringify(missingEnvFile).replaceAll('"', '')}'`));
}
assert.strictEqual(child.code, 1);
});
it('loadEnvFile does not mutate --env-file output', async () => {
const code = `
process.loadEnvFile(${JSON.stringify(basicValidEnvFilePath)});
require('assert')(process.env.BASIC === 'basic');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${validEnvFilePath}`, '--eval', code ],
{ cwd: __dirname },
);
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});