Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix test local edge case #52702

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions test/parallel/test-process-load-env-file.js
Original file line number Diff line number Diff line change
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,27 @@ 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 {
if (common.isMainThread) {
process.chdir(join(originalCwd, 'lib'));
}

assert.throws(() => {
process.loadEnvFile();
}, { code: 'ENOENT', syscall: 'open', path: '.env' });
} finally {
if (common.isMainThread) {
process.chdir(originalCwd);
}
}
});

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