From 272dfe8ad70bee82c306ad0809926adea0ff0fb4 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Fri, 26 Apr 2024 09:04:08 +0200 Subject: [PATCH 1/2] src: fix test local edge case --- test/parallel/test-process-load-env-file.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-process-load-env-file.js b/test/parallel/test-process-load-env-file.js index 8aeaef42c97805..a84795c7e3f1d3 100644 --- a/test/parallel/test-process-load-env-file.js +++ b/test/parallel/test-process-load-env-file.js @@ -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'); @@ -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 () => { From d8d64f96d5f15d3b4d595711b3dc023209acfee4 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Sun, 5 May 2024 07:37:12 +0200 Subject: [PATCH 2/2] src: only apply fix in main thread --- test/parallel/test-process-load-env-file.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-process-load-env-file.js b/test/parallel/test-process-load-env-file.js index a84795c7e3f1d3..41e487b39b390c 100644 --- a/test/parallel/test-process-load-env-file.js +++ b/test/parallel/test-process-load-env-file.js @@ -58,13 +58,17 @@ describe('process.loadEnvFile()', () => { const originalCwd = process.cwd(); try { - process.chdir(join(originalCwd, 'lib')); + if (common.isMainThread) { + process.chdir(join(originalCwd, 'lib')); + } assert.throws(() => { process.loadEnvFile(); }, { code: 'ENOENT', syscall: 'open', path: '.env' }); } finally { - process.chdir(originalCwd); + if (common.isMainThread) { + process.chdir(originalCwd); + } } });