Skip to content

Commit b533d0d

Browse files
author
Krsna Suraj
committed
test_runner: capture process state in entry point instead of runner
The run() function in the test runner directly accesses process.execArgv, process.cwd(), and process.env to configure child processes. This is problematic because run() is part of the public API and should not depend on ambient process state. This commit moves those reads to the CLI entry point (test_runner.js) and passes them as explicit options to run(): - main/test_runner.js: capture process.execArgv, process.env, process.cwd() - runner.js getRunArgs(): use execArgv parameter instead of process.execArgv - runner.js run(): default env to process.env for backward compat - runner.js runTestFile(): remove process.env fallback (handled by run()) - runner.js run(): check env.NODE_TEST_CONTEXT for recursive run detection Refs: #53867
1 parent 7c53fb5 commit b533d0d

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

lib/internal/main/test_runner.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ if (isUsingInspector() && options.isolation === 'process') {
3131
}
3232

3333
options.globPatterns = ArrayPrototypeSlice(process.argv, 1);
34+
options.execArgv = ArrayPrototypeSlice(process.execArgv);
35+
options.env = process.env;
36+
options.cwd = process.cwd();
3437

3538
debug('test runner configuration:', options);
3639
run(options).on('test:summary', (data) => {

lib/internal/test_runner/runner.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ function getRunArgs(path, { forceExit,
206206
* An example of such option are --allow-natives-syntax and --expose-gc
207207
*/
208208
const nodeOptionsSet = new SafeSet(processNodeOptions);
209+
const sourceExecArgv = execArgv.length > 0 ? execArgv : process.execArgv;
209210
const unknownProcessExecArgv = ArrayPrototypeFilter(
210-
process.execArgv,
211+
sourceExecArgv,
211212
(arg, i, arr) => !nodeOptionsSet.has(arg) && filterExecArgv(arg, i, arr),
212213
);
213214
ArrayPrototypePushApply(runArgs, unknownProcessExecArgv);
@@ -500,7 +501,7 @@ function runTestFile(path, filesWatcher, opts) {
500501
const subtest = opts.root.createSubtest(FileTest, testPath, testOpts, async (t) => {
501502
const args = getRunArgs(path, opts);
502503
const stdio = ['pipe', 'pipe', 'pipe'];
503-
const env = { __proto__: null, NODE_TEST_CONTEXT: 'child-v8', ...(opts.env || process.env) };
504+
const env = { __proto__: null, NODE_TEST_CONTEXT: 'child-v8', ...(opts.env ?? {}) };
504505

505506
// Acquire a worker ID from the pool for process isolation mode
506507
let workerId;
@@ -732,7 +733,7 @@ function run(options = kEmptyObject) {
732733
argv = [],
733734
cwd = process.cwd(),
734735
rerunFailuresFilePath,
735-
env,
736+
env = process.env,
736737
} = options;
737738

738739
if (files != null) {
@@ -907,7 +908,7 @@ function run(options = kEmptyObject) {
907908
validatePath(globalSetupPath, 'options.globalSetupPath');
908909
}
909910

910-
if (env != null) {
911+
if ('env' in options) {
911912
validateObject(env);
912913

913914
if (isolation === 'none') {
@@ -997,7 +998,7 @@ function run(options = kEmptyObject) {
997998
};
998999

9991000
if (isolation === 'process') {
1000-
if (process.env.NODE_TEST_CONTEXT !== undefined) {
1001+
if ((env?.NODE_TEST_CONTEXT ?? process.env.NODE_TEST_CONTEXT) !== undefined) {
10011002
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');
10021003
root.postRun();
10031004
return root.reporter;

0 commit comments

Comments
 (0)