Skip to content

test_runner: make --test-name-pattern recursive #48401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,12 @@ class Test extends AsyncResource {
}
}

matchesTestNamePatterns() {
return ArrayPrototypeSome(testNamePatterns, (re) => RegExpPrototypeExec(re, this.name) !== null) ||
this.parent?.matchesTestNamePatterns();
matchesTestNamePatterns(name = this.name) {
if (name == null || this.parent === null) {
return false;
}
return ArrayPrototypeSome(testNamePatterns, (re) => RegExpPrototypeExec(re, name) !== null) ||
this.parent.matchesTestNamePatterns() || this.parent.matchesTestNamePatterns(`${this.parent.name} > ${name}`);
}

hasConcurrency() {
Expand Down
41 changes: 37 additions & 4 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,43 @@ function parseTestFlags(filename = process.argv[1]) {
if (source[flagEnd - 1] === '\r') {
flagEnd--;
}
return source
.substring(flagStart, flagEnd)
.split(/\s+/)
.filter(Boolean);
const str = source.substring(flagStart, flagEnd);
const args = [];
let i = 0;
let start = 0;
let inString = false;
let inArg = false;

function unwrapArg(arg) {
const shouldTrim = (arg[0] === '"' && arg[arg.length - 1] === '"') ||
(arg[0] === '\'' && arg[arg.length - 1] === '\'');
return shouldTrim ? arg.slice(1, -1) : arg;
}
while (i < str.length) {
const c = str[i];
if (c === '=' && !inString) {
args.push(str.substring(start, i));
start = i + 1;
} else if (c === ' ' && !inString) {
if (inArg) {
args.push(unwrapArg(str.substring(start, i)));
inArg = false;
}
start = i + 1;
} else if (!inString && (c === '"' || c === '\'')) {
inString = c;
} else if (inString && (c === inString)) {
inString = false;
} else {
inArg = true;
}
i++;
}
if (inArg) {
args.push(unwrapArg(str.substring(start, i)));
}

return args;
}

// Check for flags. Skip this for workers (both, the `cluster` module and
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/test-runner/output/name_pattern_nesting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Flags: --no-warnings --test-name-pattern="first parent > enabled > third level"
'use strict';
const common = require('../../../common');
const { test, describe } = require('node:test');

console.log(process.execArgv);
describe('first parent', common.mustCall(() => {
test('enabled', common.mustCall(() => {
test('third level', common.mustCall());
test('disabled', common.mustNotCall());
}));
test('disabled', common.mustNotCall());
}));

test('second parent', common.mustNotCall());
42 changes: 42 additions & 0 deletions test/fixtures/test-runner/output/name_pattern_nesting.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
TAP version 13
# Subtest: first parent
# Subtest: enabled
ok 1 - enabled # SKIP test name does not match pattern
---
duration_ms: *
...
# Subtest: disabled
ok 2 - disabled # SKIP test name does not match pattern
---
duration_ms: *
...
1..2
ok 1 - first parent
---
duration_ms: *
type: 'suite'
...
# Subtest: second parent
ok 2 - second parent # SKIP test name does not match pattern
---
duration_ms: *
...
1..2
# tests 3
# suites 1
# pass 0
# fail 0
# cancelled 0
# skipped 3
# todo 0
# duration_ms *
Mismatched <anonymous> function calls. Expected exactly 1, actual 0.
*
*
*
*
*
*
*
*
*
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const tests = [
{ name: 'test-runner/output/output_cli.js' },
{ name: 'test-runner/output/name_pattern.js' },
{ name: 'test-runner/output/name_pattern_with_only.js' },
{ name: 'test-runner/output/name_pattern_nesting.js' },
{ name: 'test-runner/output/unresolved_promise.js' },
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
{ name: 'test-runner/output/arbitrary-output.js' },
Expand Down