Skip to content

Commit 4cec842

Browse files
committed
test_runner: consolidate option parsing
This commit consolidates all option parsing for the test runner in the parseCommandLine() internal helper function. The exception is a couple of temporary flags used for feature gating which will eventually become no-ops. This consolidation is prep work for supporting running test files in the test runner process.
1 parent 7d07c95 commit 4cec842

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

lib/internal/main/test_runner.js

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
'use strict';
22

3-
const {
4-
NumberParseInt,
5-
RegExpPrototypeExec,
6-
StringPrototypeSplit,
7-
} = primordials;
8-
93
const {
104
prepareMainThreadExecution,
115
markBootstrapComplete,
126
} = require('internal/process/pre_execution');
13-
const { getOptionValue } = require('internal/options');
147
const { isUsingInspector } = require('internal/util/inspector');
158
const { run } = require('internal/test_runner/runner');
16-
const { setupTestReporters } = require('internal/test_runner/utils');
17-
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
189
const {
19-
codes: {
20-
ERR_INVALID_ARG_VALUE,
21-
},
22-
} = require('internal/errors');
23-
10+
parseCommandLine,
11+
setupTestReporters,
12+
} = require('internal/test_runner/utils');
13+
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
2414
let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => {
2515
debug = fn;
2616
});
2717

2818
prepareMainThreadExecution(false);
2919
markBootstrapComplete();
3020

31-
let concurrency = getOptionValue('--test-concurrency') || true;
21+
const {
22+
perFileTimeout,
23+
runnerConcurrency,
24+
shard,
25+
watchMode,
26+
} = parseCommandLine();
27+
28+
let concurrency = runnerConcurrency;
3229
let inspectPort;
3330

3431
if (isUsingInspector()) {
@@ -38,39 +35,12 @@ if (isUsingInspector()) {
3835
inspectPort = process.debugPort;
3936
}
4037

41-
let shard;
42-
const shardOption = getOptionValue('--test-shard');
43-
if (shardOption) {
44-
if (!RegExpPrototypeExec(/^\d+\/\d+$/, shardOption)) {
45-
process.exitCode = kGenericUserError;
46-
47-
throw new ERR_INVALID_ARG_VALUE(
48-
'--test-shard',
49-
shardOption,
50-
'must be in the form of <index>/<total>',
51-
);
52-
}
53-
54-
const { 0: indexStr, 1: totalStr } = StringPrototypeSplit(shardOption, '/');
55-
56-
const index = NumberParseInt(indexStr, 10);
57-
const total = NumberParseInt(totalStr, 10);
58-
59-
shard = {
60-
__proto__: null,
61-
index,
62-
total,
63-
};
64-
}
65-
66-
const timeout = getOptionValue('--test-timeout') || Infinity;
67-
6838
const options = {
6939
concurrency,
7040
inspectPort,
71-
watch: getOptionValue('--watch'),
41+
watch: watchMode,
7242
setup: setupTestReporters,
73-
timeout,
43+
timeout: perFileTimeout,
7444
shard,
7545
};
7646
debug('test runner configuration:', options);

lib/internal/test_runner/utils.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
MathFloor,
1010
MathMax,
1111
MathMin,
12+
NumberParseInt,
1213
NumberPrototypeToFixed,
1314
ObjectGetOwnPropertyDescriptor,
1415
RegExp,
@@ -19,6 +20,7 @@ const {
1920
StringPrototypePadStart,
2021
StringPrototypeRepeat,
2122
StringPrototypeSlice,
23+
StringPrototypeSplit,
2224
} = primordials;
2325

2426
const { AsyncResource } = require('async_hooks');
@@ -196,13 +198,17 @@ function parseCommandLine() {
196198
const forceExit = getOptionValue('--test-force-exit');
197199
const sourceMaps = getOptionValue('--enable-source-maps');
198200
const updateSnapshots = getOptionValue('--test-update-snapshots');
201+
const watchMode = getOptionValue('--watch');
199202
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
200203
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
201204
let destinations;
205+
let perFileTimeout;
202206
let reporters;
207+
let runnerConcurrency;
203208
let testNamePatterns;
204209
let testSkipPatterns;
205210
let testOnlyFlag;
211+
let shard;
206212

207213
if (isChildProcessV8) {
208214
kBuiltinReporters.set('v8-serializer', 'internal/test_runner/reporter/v8-serializer');
@@ -232,9 +238,31 @@ function parseCommandLine() {
232238
}
233239

234240
if (isTestRunner) {
241+
perFileTimeout = getOptionValue('--test-timeout') || Infinity;
242+
runnerConcurrency = getOptionValue('--test-concurrency') || true;
235243
testOnlyFlag = false;
236244
testNamePatterns = null;
245+
246+
const shardOption = getOptionValue('--test-shard');
247+
if (shardOption) {
248+
if (!RegExpPrototypeExec(/^\d+\/\d+$/, shardOption)) {
249+
throw new ERR_INVALID_ARG_VALUE(
250+
'--test-shard',
251+
shardOption,
252+
'must be in the form of <index>/<total>',
253+
);
254+
}
255+
256+
const indexAndTotal = StringPrototypeSplit(shardOption, '/');
257+
shard = {
258+
__proto__: null,
259+
index: NumberParseInt(indexAndTotal[0], 10),
260+
total: NumberParseInt(indexAndTotal[1], 10),
261+
};
262+
}
237263
} else {
264+
perFileTimeout = Infinity;
265+
runnerConcurrency = 1;
238266
const testNamePatternFlag = getOptionValue('--test-name-pattern');
239267
testOnlyFlag = getOptionValue('--test-only');
240268
testNamePatterns = testNamePatternFlag?.length > 0 ?
@@ -252,13 +280,17 @@ function parseCommandLine() {
252280
isTestRunner,
253281
coverage,
254282
forceExit,
283+
perFileTimeout,
284+
runnerConcurrency,
285+
shard,
255286
sourceMaps,
256287
testOnlyFlag,
257288
testNamePatterns,
258289
testSkipPatterns,
259290
updateSnapshots,
260291
reporters,
261292
destinations,
293+
watchMode,
262294
};
263295

264296
return globalTestOptions;

0 commit comments

Comments
 (0)