diff --git a/test/common/index.js b/test/common/index.js index 67323cd21fa846..0f06c480ff1556 100755 --- a/test/common/index.js +++ b/test/common/index.js @@ -116,34 +116,38 @@ if (process.argv.length === 2 && require('cluster').isPrimary && fs.existsSync(process.argv[1])) { const { flags, envs } = parseTestMetadata(); - for (const flag of flags) { - if (!process.execArgv.includes(flag) && - // If the binary is build without `intl` the inspect option is - // invalid. The test itself should handle this case. - (process.features.inspector || !flag.startsWith('--inspect'))) { - console.log( - 'NOTE: The test started as a child_process using these flags:', - inspect(flags), - 'And these environment variables:', - inspect(envs), - 'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.', - ); - const { spawnSync } = require('child_process'); - const args = [...flags, ...process.execArgv, ...process.argv.slice(1)]; - const options = { - encoding: 'utf8', - stdio: 'inherit', - env: { - ...process.env, - ...envs, - }, - }; - const result = spawnSync(process.execPath, args, options); - if (result.signal) { - process.kill(0, result.signal); - } else { - process.exit(result.status); - } + + const flagsTriggerSpawn = flags.some((flag) => ( + !process.execArgv.includes(flag) && + // If the binary is build without `intl` the inspect option is + // invalid. The test itself should handle this case. + (process.features.inspector || !flag.startsWith('--inspect')) + )); + const envsTriggerSpawn = Object.keys(envs).some((key) => process.env[key] !== envs[key]); + + if (flagsTriggerSpawn || envsTriggerSpawn) { + console.log( + 'NOTE: The test started as a child_process using these flags:', + inspect(flags), + 'And these environment variables:', + inspect(envs), + 'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.', + ); + const { spawnSync } = require('child_process'); + const args = [...flags, ...process.execArgv, ...process.argv.slice(1)]; + const options = { + encoding: 'utf8', + stdio: 'inherit', + env: { + ...process.env, + ...envs, + }, + }; + const result = spawnSync(process.execPath, args, options); + if (result.signal) { + process.kill(0, result.signal); + } else { + process.exit(result.status); } } } diff --git a/test/parallel/test-parse-test-only-envs.js b/test/parallel/test-parse-test-only-envs.js new file mode 100644 index 00000000000000..0eafec719325db --- /dev/null +++ b/test/parallel/test-parse-test-only-envs.js @@ -0,0 +1,20 @@ +'use strict'; + +// Env: A_SET_ENV_VAR=A_SET_ENV_VAR_VALUE B_SET_ENV_VAR=B_SET_ENV_VAR_VALUE + +require('../common'); +const assert = require('node:assert'); +const { describe, it } = require('node:test'); + + +// This test verifies that a test file that requires 'common' can set environment variables +// via comments in the test file, similar to how we set flags via comments. +// Ref: https://github.com/nodejs/node/issues/58179 +describe('env var via comment', () => { + it('should set env var A_SET_ENV_VAR', () => { + assert.strictEqual(process.env.A_SET_ENV_VAR, 'A_SET_ENV_VAR_VALUE'); + }); + it('should set env var B_SET_ENV_VAR', () => { + assert.strictEqual(process.env.B_SET_ENV_VAR, 'B_SET_ENV_VAR_VALUE'); + }); +});