From 6e3bc499ef595929dec369e119697fcc28e1c935 Mon Sep 17 00:00:00 2001 From: JonasBa Date: Fri, 17 Jan 2025 10:40:48 -0500 Subject: [PATCH] watch: reload env file for --env-file-if-exists --- lib/internal/main/watch_mode.js | 2 +- test/sequential/test-watch-mode.mjs | 110 ++++++++++++++-------------- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index 6e2528e64737c7..60639efb45482d 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -33,7 +33,7 @@ markBootstrapComplete(); // TODO(MoLow): Make kill signal configurable const kKillSignal = 'SIGTERM'; const kShouldFilterModules = getOptionValue('--watch-path').length === 0; -const kEnvFile = getOptionValue('--env-file'); +const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists'); const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path)); const kPreserveOutput = getOptionValue('--watch-preserve-output'); const kCommand = ArrayPrototypeSlice(process.argv, 1); diff --git a/test/sequential/test-watch-mode.mjs b/test/sequential/test-watch-mode.mjs index 39bc7223dffdfc..12d3799a37fa92 100644 --- a/test/sequential/test-watch-mode.mjs +++ b/test/sequential/test-watch-mode.mjs @@ -104,7 +104,7 @@ async function runWriteSucceed({ if (watchFlag !== null) args.unshift(watchFlag); const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options }); let completes = 0; - let cancelRestarts = () => {}; + let cancelRestarts = () => { }; let stderr = ''; const stdout = []; @@ -141,7 +141,7 @@ async function runWriteSucceed({ async function failWriteSucceed({ file, watchedFile }) { const child = spawn(execPath, ['--watch', '--no-warnings', file], { encoding: 'utf8', stdio: 'pipe' }); - let cancelRestarts = () => {}; + let cancelRestarts = () => { }; try { // Break the chunks into lines @@ -164,9 +164,11 @@ tmpdir.refresh(); describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => { it('should watch changes to a file', async () => { const file = createTmpFile(); - const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, watchFlag: '--watch=true', options: { - timeout: 10000 - } }); + const { stderr, stdout } = await runWriteSucceed({ + file, watchedFile: file, watchFlag: '--watch=true', options: { + timeout: 10000 + } + }); assert.strictEqual(stderr, ''); assert.deepStrictEqual(stdout, [ @@ -192,55 +194,57 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00 ]); }); - it('should reload env variables when --env-file changes', async () => { - const envKey = `TEST_ENV_${Date.now()}`; - const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`); - const envFile = createTmpFile(`${envKey}=value1`, '.env'); - const { done, restart } = runInBackground({ args: ['--watch', `--env-file=${envFile}`, jsFile] }); - - try { - await restart(); - writeFileSync(envFile, `${envKey}=value2`); - - // Second restart, after env change - const { stdout, stderr } = await restart(); - - assert.strictEqual(stderr, ''); - assert.deepStrictEqual(stdout, [ - `Restarting ${inspect(jsFile)}`, - 'ENV: value2', - `Completed running ${inspect(jsFile)}`, - ]); - } finally { - await done(); - } - }); + for (const cmd in ['--env-file', '--env-file-if-exists']) { + it(`should reload env variables when ${cmd} changes`, async () => { + const envKey = `TEST_ENV_${Date.now()}`; + const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey});`); + const envFile = createTmpFile(`${envKey}=value1`, '.env'); + const { done, restart } = runInBackground({ args: ['--watch', `${cmd}=${envFile}`, jsFile] }); + + try { + await restart(); + writeFileSync(envFile, `${envKey}=value2`); + + // Second restart, after env change + const { stdout, stderr } = await restart(); + + assert.strictEqual(stderr, ''); + assert.deepStrictEqual(stdout, [ + `Restarting ${inspect(jsFile)}`, + 'ENV: value2', + `Completed running ${inspect(jsFile)}`, + ]); + } finally { + await done(); + } + }); - it('should load new env variables when --env-file changes', async () => { - const envKey = `TEST_ENV_${Date.now()}`; - const envKey2 = `TEST_ENV_2_${Date.now()}`; - const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`); - const envFile = createTmpFile(`${envKey}=value1`, '.env'); - const { done, restart } = runInBackground({ args: ['--watch', `--env-file=${envFile}`, jsFile] }); - - try { - await restart(); - writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`); - - // Second restart, after env change - const { stderr, stdout } = await restart(); - - assert.strictEqual(stderr, ''); - assert.deepStrictEqual(stdout, [ - `Restarting ${inspect(jsFile)}`, - 'ENV: value1', - 'ENV2: newValue', - `Completed running ${inspect(jsFile)}`, - ]); - } finally { - await done(); - } - }); + it(`should load new env variables when ${cmd} changes`, async () => { + const envKey = `TEST_ENV_${Date.now()}`; + const envKey2 = `TEST_ENV_2_${Date.now()}`; + const jsFile = createTmpFile(`console.log('ENV: ' + process.env.${envKey} + '\\n' + 'ENV2: ' + process.env.${envKey2});`); + const envFile = createTmpFile(`${envKey}=value1`, '.env'); + const { done, restart } = runInBackground({ args: ['--watch', `${cmd}=${envFile}`, jsFile] }); + + try { + await restart(); + writeFileSync(envFile, `${envKey}=value1\n${envKey2}=newValue`); + + // Second restart, after env change + const { stderr, stdout } = await restart(); + + assert.strictEqual(stderr, ''); + assert.deepStrictEqual(stdout, [ + `Restarting ${inspect(jsFile)}`, + 'ENV: value1', + 'ENV2: newValue', + `Completed running ${inspect(jsFile)}`, + ]); + } finally { + await done(); + } + }); + } it('should watch changes to a failing file', async () => { const file = createTmpFile('throw new Error("fails");');