Skip to content

Commit 5d3871f

Browse files
committed
src: fix test local edge case
1 parent 1728203 commit 5d3871f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

test/parallel/test-process-load-env-file.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44
const fixtures = require('../../test/common/fixtures');
55
const assert = require('node:assert');
66
const { describe, it } = require('node:test');
7+
const { join } = require('node:path');
78

89
const basicValidEnvFilePath = fixtures.path('dotenv/basic-valid.env');
910
const validEnvFilePath = fixtures.path('dotenv/valid.env');
@@ -48,10 +49,20 @@ describe('process.loadEnvFile()', () => {
4849
}, { code: 'ENOENT', syscall: 'open', path: missingEnvFile });
4950
});
5051

52+
// The whole chdir flow here is to address a case where a developer
53+
// has a .env in the worktree which is probably in the global .gitignore.
54+
// In that case this test would fail. To avoid confusion, chdir to lib will
55+
// make this way less likely to happen. Probably a temporary directory would
56+
// be the best choice but given how edge this case is this is fine.
5157
it('should throw when `.env` does not exist', async () => {
58+
const originalCwd = process.cwd();
59+
process.chdir(join(originalCwd, 'lib'));
60+
5261
assert.throws(() => {
5362
process.loadEnvFile();
5463
}, { code: 'ENOENT', syscall: 'open', path: '.env' });
64+
65+
process.chdir(originalCwd);
5566
});
5667

5768
it('should check for permissions', async () => {

0 commit comments

Comments
 (0)