Skip to content
Merged
Show file tree
Hide file tree
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
60 changes: 32 additions & 28 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-parse-test-only-envs.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
Loading