Skip to content

Commit 6fd3ea2

Browse files
cjihrigRafaelGSS
authored andcommitted
test_runner: remove parseCommandLine() from test.js
The lib/internal/test_runner/test.js should not use the parseCommandLine() function. This commit refactors the code to avoid doing so. PR-URL: #54353 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent d024141 commit 6fd3ea2

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

lib/internal/test_runner/test.js

+22-25
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const {
4545
createDeferredCallback,
4646
countCompletedTest,
4747
isTestFailureError,
48-
parseCommandLine,
4948
} = require('internal/test_runner/utils');
5049
const {
5150
createDeferredPromise,
@@ -79,14 +78,6 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
7978
const kUnwrapErrors = new SafeSet()
8079
.add(kTestCodeFailure).add(kHookFailure)
8180
.add('uncaughtException').add('unhandledRejection');
82-
const {
83-
forceExit,
84-
sourceMaps,
85-
testNamePatterns,
86-
testSkipPatterns,
87-
only: testOnlyFlag,
88-
updateSnapshots,
89-
} = parseCommandLine();
9081
let kResistStopPropagation;
9182
let assertObj;
9283
let findSourceMap;
@@ -132,7 +123,7 @@ function lazyAssertObject(harness) {
132123
const { getOptionValue } = require('internal/options');
133124
if (getOptionValue('--experimental-test-snapshots')) {
134125
const { SnapshotManager } = require('internal/test_runner/snapshot');
135-
harness.snapshotManager = new SnapshotManager(updateSnapshots);
126+
harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
136127
assertObj.set('snapshot', harness.snapshotManager.createAssert());
137128
}
138129
}
@@ -406,16 +397,17 @@ class Test extends AsyncResource {
406397
this.filtered = false;
407398

408399
if (parent === null) {
400+
this.root = this;
401+
this.harness = options.harness;
402+
this.config = this.harness.config;
409403
this.concurrency = 1;
410404
this.nesting = 0;
411-
this.only = testOnlyFlag;
405+
this.only = this.config.only;
412406
this.reporter = new TestsStream();
413407
this.runOnlySubtests = this.only;
414408
this.childNumber = 0;
415409
this.timeout = kDefaultTimeout;
416410
this.entryFile = entryFile;
417-
this.root = this;
418-
this.harness = options.harness;
419411
this.hooks = {
420412
__proto__: null,
421413
before: [],
@@ -428,6 +420,9 @@ class Test extends AsyncResource {
428420
const nesting = parent.parent === null ? parent.nesting :
429421
parent.nesting + 1;
430422

423+
this.root = parent.root;
424+
this.harness = null;
425+
this.config = this.root.harness.config;
431426
this.concurrency = parent.concurrency;
432427
this.nesting = nesting;
433428
this.only = only ?? (parent.only && !parent.runOnlySubtests);
@@ -436,8 +431,6 @@ class Test extends AsyncResource {
436431
this.childNumber = parent.subtests.length + 1;
437432
this.timeout = parent.timeout;
438433
this.entryFile = parent.entryFile;
439-
this.root = parent.root;
440-
this.harness = null;
441434
this.hooks = {
442435
__proto__: null,
443436
before: [],
@@ -452,7 +445,7 @@ class Test extends AsyncResource {
452445
this.parent.filteredSubtestCount++;
453446
}
454447

455-
if (testOnlyFlag && only === false) {
448+
if (this.config.only && only === false) {
456449
fn = noop;
457450
}
458451
}
@@ -522,7 +515,7 @@ class Test extends AsyncResource {
522515
this.waitingOn = 0;
523516
this.finished = false;
524517

525-
if (!testOnlyFlag && (only || this.parent?.runOnlySubtests)) {
518+
if (!this.config.only && (only || this.parent?.runOnlySubtests)) {
526519
const warning =
527520
"'only' and 'runOnly' require the --test-only command-line option.";
528521
this.diagnostic(warning);
@@ -538,7 +531,7 @@ class Test extends AsyncResource {
538531
file: loc[2],
539532
};
540533

541-
if (sourceMaps === true) {
534+
if (this.config.sourceMaps === true) {
542535
const map = lazyFindSourceMap(this.loc.file);
543536
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
544537

@@ -556,7 +549,9 @@ class Test extends AsyncResource {
556549
}
557550

558551
willBeFiltered() {
559-
if (testOnlyFlag && !this.only) return true;
552+
if (this.config.only && !this.only) return true;
553+
554+
const { testNamePatterns, testSkipPatterns } = this.config;
560555

561556
if (testNamePatterns && !testMatchesPattern(this, testNamePatterns)) {
562557
return true;
@@ -920,7 +915,7 @@ class Test extends AsyncResource {
920915
// This helps catch any asynchronous activity that occurs after the tests
921916
// have finished executing.
922917
this.postRun();
923-
} else if (forceExit) {
918+
} else if (this.config.forceExit) {
924919
// This is the root test, and all known tests and hooks have finished
925920
// executing. If the user wants to force exit the process regardless of
926921
// any remaining ref'ed handles, then do that now. It is theoretically
@@ -1163,11 +1158,13 @@ class Suite extends Test {
11631158
constructor(options) {
11641159
super(options);
11651160

1166-
if (testNamePatterns !== null && testSkipPatterns !== null && !options.skip) {
1161+
if (this.config.testNamePatterns !== null &&
1162+
this.config.testSkipPatterns !== null &&
1163+
!options.skip) {
11671164
this.fn = options.fn || this.fn;
11681165
this.skipped = false;
11691166
}
1170-
this.runOnlySubtests = testOnlyFlag;
1167+
this.runOnlySubtests = this.config.only;
11711168

11721169
try {
11731170
const { ctx, args } = this.getRunArgs();
@@ -1198,9 +1195,9 @@ class Suite extends Test {
11981195
this.filtered = false;
11991196
this.parent.filteredSubtestCount--;
12001197
} else if (
1201-
testOnlyFlag &&
1202-
testNamePatterns == null &&
1203-
testSkipPatterns == null &&
1198+
this.config.only &&
1199+
this.config.testNamePatterns == null &&
1200+
this.config.testSkipPatterns == null &&
12041201
this.filteredSubtestCount === this.subtests.length
12051202
) {
12061203
// If no subtests are marked as "only", run them all

test/parallel/test-runner-v8-deserializer.mjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ describe('v8 deserializer', () => {
2626
let reported;
2727
beforeEach(() => {
2828
reported = [];
29-
fileTest = new runner.FileTest({ name: 'filetest' });
29+
fileTest = new runner.FileTest({
30+
name: 'filetest',
31+
harness: { config: {} },
32+
});
3033
fileTest.reporter.on('data', (data) => reported.push(data));
3134
assert(fileTest.isClearToSend());
3235
});

0 commit comments

Comments
 (0)