Skip to content

Commit b4afa69

Browse files
Krsna SurajkrsnaSuraj
authored andcommitted
test_runner: capture process state in entry point instead of runner
Move process.* reads (process.argv, process.env, process.cwd) from runner.js to the CLI entry point (main/test_runner.js), so that runner.js can be loaded without side effects in snapshot environments. The execArgv case is handled via a fallback in getRunArgs(): when no execArgv is passed in options, process.execArgv is still read directly to propagate V8-only flags to child processes. PR-URL: https://github.com/nodejs/node/pull/XXXXX Refs: #56131
1 parent ee5a070 commit b4afa69

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

lib/internal/main/test_runner.js

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

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

3537
debug('test runner configuration:', options);
3638
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;
@@ -733,7 +734,7 @@ function run(options = kEmptyObject) {
733734
argv = [],
734735
cwd = process.cwd(),
735736
rerunFailuresFilePath,
736-
env,
737+
env = process.env,
737738
} = options;
738739

739740
if (files != null) {
@@ -909,7 +910,7 @@ function run(options = kEmptyObject) {
909910
validatePath(globalSetupPath, 'options.globalSetupPath');
910911
}
911912

912-
if (env != null) {
913+
if ('env' in options) {
913914
validateObject(env, 'options.env');
914915

915916
if (isolation === 'none') {
@@ -1000,7 +1001,7 @@ function run(options = kEmptyObject) {
10001001
};
10011002

10021003
if (isolation === 'process') {
1003-
if (process.env.NODE_TEST_CONTEXT !== undefined) {
1004+
if ((env?.NODE_TEST_CONTEXT ?? process.env.NODE_TEST_CONTEXT) !== undefined) {
10041005
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');
10051006
root.postRun();
10061007
return root.reporter;

0 commit comments

Comments
 (0)