Skip to content

Commit f78f47c

Browse files
pmarchinitargos
authored andcommitted
test: support standalone env comment in tests
PR-URL: #59546 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent cf84fff commit f78f47c

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

test/common/index.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,38 @@ if (process.argv.length === 2 &&
116116
require('cluster').isPrimary &&
117117
fs.existsSync(process.argv[1])) {
118118
const { flags, envs } = parseTestMetadata();
119-
for (const flag of flags) {
120-
if (!process.execArgv.includes(flag) &&
121-
// If the binary is build without `intl` the inspect option is
122-
// invalid. The test itself should handle this case.
123-
(process.features.inspector || !flag.startsWith('--inspect'))) {
124-
console.log(
125-
'NOTE: The test started as a child_process using these flags:',
126-
inspect(flags),
127-
'And these environment variables:',
128-
inspect(envs),
129-
'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.',
130-
);
131-
const { spawnSync } = require('child_process');
132-
const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
133-
const options = {
134-
encoding: 'utf8',
135-
stdio: 'inherit',
136-
env: {
137-
...process.env,
138-
...envs,
139-
},
140-
};
141-
const result = spawnSync(process.execPath, args, options);
142-
if (result.signal) {
143-
process.kill(0, result.signal);
144-
} else {
145-
process.exit(result.status);
146-
}
119+
120+
const flagsTriggerSpawn = flags.some((flag) => (
121+
!process.execArgv.includes(flag) &&
122+
// If the binary is build without `intl` the inspect option is
123+
// invalid. The test itself should handle this case.
124+
(process.features.inspector || !flag.startsWith('--inspect'))
125+
));
126+
const envsTriggerSpawn = Object.keys(envs).some((key) => process.env[key] !== envs[key]);
127+
128+
if (flagsTriggerSpawn || envsTriggerSpawn) {
129+
console.log(
130+
'NOTE: The test started as a child_process using these flags:',
131+
inspect(flags),
132+
'And these environment variables:',
133+
inspect(envs),
134+
'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.',
135+
);
136+
const { spawnSync } = require('child_process');
137+
const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
138+
const options = {
139+
encoding: 'utf8',
140+
stdio: 'inherit',
141+
env: {
142+
...process.env,
143+
...envs,
144+
},
145+
};
146+
const result = spawnSync(process.execPath, args, options);
147+
if (result.signal) {
148+
process.kill(0, result.signal);
149+
} else {
150+
process.exit(result.status);
147151
}
148152
}
149153
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
// Env: A_SET_ENV_VAR=A_SET_ENV_VAR_VALUE B_SET_ENV_VAR=B_SET_ENV_VAR_VALUE
4+
5+
require('../common');
6+
const assert = require('node:assert');
7+
const { describe, it } = require('node:test');
8+
9+
10+
// This test verifies that a test file that requires 'common' can set environment variables
11+
// via comments in the test file, similar to how we set flags via comments.
12+
// Ref: https://github.com/nodejs/node/issues/58179
13+
describe('env var via comment', () => {
14+
it('should set env var A_SET_ENV_VAR', () => {
15+
assert.strictEqual(process.env.A_SET_ENV_VAR, 'A_SET_ENV_VAR_VALUE');
16+
});
17+
it('should set env var B_SET_ENV_VAR', () => {
18+
assert.strictEqual(process.env.B_SET_ENV_VAR, 'B_SET_ENV_VAR_VALUE');
19+
});
20+
});

0 commit comments

Comments
 (0)