Skip to content

Commit 9a8b0bb

Browse files
committed
test: support standalone env comment in tests
1 parent ebbdea2 commit 9a8b0bb

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

test/common/index.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,36 @@ 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 containsAdditionalFlags = flags.some((flag) => (
121+
!process.execArgv.includes(flag) &&
122+
(process.features.inspector || !flag.startsWith('--inspect'))
123+
));
124+
const containsAdditionalEnvs = Object.keys(envs).some((key) => process.env[key] !== envs[key]);
125+
126+
if (containsAdditionalFlags || containsAdditionalEnvs) {
127+
console.log(
128+
'NOTE: The test started as a child_process using these flags:',
129+
inspect(flags),
130+
'And these environment variables:',
131+
inspect(envs),
132+
'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.',
133+
);
134+
const { spawnSync } = require('child_process');
135+
const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
136+
const options = {
137+
encoding: 'utf8',
138+
stdio: 'inherit',
139+
env: {
140+
...process.env,
141+
...envs,
142+
},
143+
};
144+
const result = spawnSync(process.execPath, args, options);
145+
if (result.signal) {
146+
process.kill(0, result.signal);
147+
} else {
148+
process.exit(result.status);
147149
}
148150
}
149151
}
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)