mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 00:20:08 +01:00
test: support standalone env comment in tests
PR-URL: https://github.com/nodejs/node/pull/59546 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
parent
56de98b13c
commit
08556f0555
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
20
test/parallel/test-parse-test-only-envs.js
Normal file
20
test/parallel/test-parse-test-only-envs.js
Normal file
|
|
@ -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…
Reference in New Issue
Block a user