Skip to content

Commit

Permalink
src: fix test local edge case
Browse files Browse the repository at this point in the history
PR-URL: #52702
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
  • Loading branch information
ShogunPanda authored and targos committed May 8, 2024
1 parent a42b93b commit e1cba97
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions test/parallel/test-process-load-env-file.js
Expand Up @@ -4,6 +4,7 @@ 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');
Expand Down Expand Up @@ -48,10 +49,23 @@ describe('process.loadEnvFile()', () => {
}, { 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 () => {
assert.throws(() => {
process.loadEnvFile();
}, { code: 'ENOENT', syscall: 'open', path: '.env' });
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 () => {
Expand Down

0 comments on commit e1cba97

Please sign in to comment.