Skip to content

Commit 24a021f

Browse files
committed
test_runner: preserve user flags in argv
This commit fixes an issue where user-specified flags were being stripped from process.argv when running with the --test flag. It ensures that arguments following '--' or starting with '-' (that are not consumed by Node.js options) are correctly passed to the test process. Fixes: #61852
1 parent ca63b7b commit 24a021f

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/internal/main/test_runner.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const {
4-
ArrayPrototypeSlice,
4+
ArrayPrototypePush,
5+
StringPrototypeStartsWith,
56
} = primordials;
67

78
const {
@@ -30,7 +31,26 @@ if (isUsingInspector() && options.isolation === 'process') {
3031
options.inspectPort = process.debugPort;
3132
}
3233

33-
options.globPatterns = ArrayPrototypeSlice(process.argv, 1);
34+
const userArgs = [];
35+
const globPatterns = [];
36+
let isArg = false;
37+
38+
for (let i = 1; i < process.argv.length; i++) {
39+
const arg = process.argv[i];
40+
if (isArg) {
41+
ArrayPrototypePush(userArgs, arg);
42+
} else if (arg === '--') {
43+
isArg = true;
44+
ArrayPrototypePush(userArgs, arg);
45+
} else if (StringPrototypeStartsWith(arg, '-')) {
46+
ArrayPrototypePush(userArgs, arg);
47+
} else {
48+
ArrayPrototypePush(globPatterns, arg);
49+
}
50+
}
51+
52+
options.globPatterns = globPatterns;
53+
options.argv = userArgs;
3454

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

test/fixtures/test-runner/argv.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(JSON.stringify(process.argv));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { spawnSync } = require('child_process');
5+
const fixtures = require('../common/fixtures');
6+
7+
const testFixture = fixtures.path('test-runner/argv.js');
8+
9+
{
10+
const args = ['--test', testFixture, '--hello'];
11+
const child = spawnSync(process.execPath, args);
12+
13+
assert.strictEqual(child.stderr.toString(), '');
14+
const stdout = child.stdout.toString();
15+
assert.match(stdout, /"--hello"\]/);
16+
assert.strictEqual(child.status, 0);
17+
}
18+
19+
{
20+
const args = ['--test', testFixture, '--', '--hello', '--world'];
21+
const child = spawnSync(process.execPath, args);
22+
23+
assert.strictEqual(child.stderr.toString(), '');
24+
const stdout = child.stdout.toString();
25+
assert.match(stdout, /"--","--hello","--world"\]/);
26+
assert.strictEqual(child.status, 0);
27+
}
28+
29+
{
30+
const args = ['--test', testFixture, '--', 'foo.js'];
31+
const child = spawnSync(process.execPath, args);
32+
33+
assert.strictEqual(child.stderr.toString(), '');
34+
const stdout = child.stdout.toString();
35+
assert.match(stdout, /"--","foo\.js"\]/);
36+
assert.strictEqual(child.status, 0);
37+
}
38+
39+
{
40+
const args = ['--test', testFixture, '--hello', '--', 'world'];
41+
const child = spawnSync(process.execPath, args);
42+
43+
assert.strictEqual(child.stderr.toString(), '');
44+
const stdout = child.stdout.toString();
45+
assert.match(stdout, /"--hello","--","world"\]/);
46+
assert.strictEqual(child.status, 0);
47+
}

0 commit comments

Comments
 (0)