Skip to content

Commit 5b2fcfa

Browse files
committed
test_runner: avoid reading process.argv and process.cwd() in run()
Capture process state in main/test_runner.js and pass it down to run() as options, so the public run() API doesn't depend on process state during execution. Fixes: #53867
1 parent 150d154 commit 5b2fcfa

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

lib/internal/main/test_runner.js

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

33+
// Capture process state before passing to run() to avoid reading from process.cwd() inside run()
34+
options.cwd = process.cwd();
3335
options.globPatterns = ArrayPrototypeSlice(process.argv, 1);
3436

3537
debug('test runner configuration:', options);

lib/internal/test_runner/runner.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212
ArrayPrototypePush,
1313
ArrayPrototypePushApply,
1414
ArrayPrototypeShift,
15-
ArrayPrototypeSlice,
1615
ArrayPrototypeSome,
1716
ArrayPrototypeSort,
1817
ObjectAssign,
@@ -151,7 +150,8 @@ function getRunArgs(path, { forceExit,
151150
execArgv,
152151
rerunFailuresFilePath,
153152
root: { timeout },
154-
cwd }) {
153+
cwd,
154+
globPatterns }) {
155155
const processNodeOptions = getOptionsAsFlagsFromBinding();
156156
const runArgs = ArrayPrototypeFilter(processNodeOptions, filterExecArgv);
157157

@@ -196,7 +196,7 @@ function getRunArgs(path, { forceExit,
196196

197197
if (path === kIsolatedProcessName) {
198198
ArrayPrototypePush(runArgs, '--test');
199-
ArrayPrototypePushApply(runArgs, ArrayPrototypeSlice(process.argv, 1));
199+
ArrayPrototypePushApply(runArgs, globPatterns);
200200
} else {
201201
ArrayPrototypePush(runArgs, path);
202202
}
@@ -888,7 +888,11 @@ function run(options = kEmptyObject) {
888888
await root.harness.bootstrapPromise;
889889
}
890890
if (typeof setup === 'function') {
891+
<<<<<<< HEAD
891892
await setup(root.reporter);
893+
=======
894+
await setup(root.reporter, cwd);
895+
>>>>>>> af2db6ac72 (test_runner: avoid reading process.argv and process.cwd() in run())
892896
}
893897

894898
await runFiles();

lib/internal/test_runner/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function parsePreviousRuns(rerunFailuresFilePath) {
165165
return JSONParse(data);
166166
}
167167

168-
async function getReportersMap(reporters, destinations) {
168+
async function getReportersMap(reporters, destinations, cwd) {
169169
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
170170
const destination = kBuiltinDestinations.get(destinations[i]) ??
171171
createWriteStream(destinations[i], { __proto__: null, flush: true });
@@ -177,7 +177,7 @@ async function getReportersMap(reporters, destinations) {
177177
let parentURL;
178178

179179
try {
180-
parentURL = pathToFileURL(process.cwd() + '/').href;
180+
parentURL = pathToFileURL((cwd ?? process.cwd()) + '/').href;
181181
} catch {
182182
parentURL = 'file:///';
183183
}
@@ -328,8 +328,8 @@ function parseCommandLine() {
328328
validatePath(rerunFailuresFilePath, '--test-rerun-failures');
329329
}
330330

331-
const setup = reporterScope.bind(async (rootReporter) => {
332-
const reportersMap = await getReportersMap(reporters, destinations);
331+
const setup = reporterScope.bind(async (rootReporter, cwd) => {
332+
const reportersMap = await getReportersMap(reporters, destinations, cwd);
333333
for (let i = 0; i < reportersMap.length; i++) {
334334
const { reporter, destination } = reportersMap[i];
335335
compose(rootReporter, reporter).pipe(destination);

0 commit comments

Comments
 (0)