From 2f0cef42d109f17126892ae7849bb75b80b90aaf Mon Sep 17 00:00:00 2001 From: Vyron Vasileiadis Date: Wed, 2 Dec 2026 21:29:59 +0200 Subject: [PATCH] gh-163034: Match the exact case id in regrtest --single-process-per-case --- Lib/test/libregrtest/filter.py | 15 ++++++----- Lib/test/libregrtest/main.py | 1 + Lib/test/libregrtest/run_workers.py | 1 + Lib/test/libregrtest/runtests.py | 1 + Lib/test/libregrtest/setup.py | 3 ++- Lib/test/test_regrtest.py | 27 +++++++++++++++++++ ...-12-02-19-29-59.gh-issue-163034.D6BS6a.rst | 3 +++ 7 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2026-12-02-19-29-59.gh-issue-163034.D6BS6a.rst diff --git a/Lib/test/libregrtest/filter.py b/Lib/test/libregrtest/filter.py index a190a97fd9e3ae0..2f96345149c5ac3 100644 --- a/Lib/test/libregrtest/filter.py +++ b/Lib/test/libregrtest/filter.py @@ -7,6 +7,7 @@ # By default, don't filter tests _test_matchers = () _test_patterns = () +_test_exact = False _match_labels = () # Sentinel returned by _get_label() when the test has no such label. @@ -72,8 +73,8 @@ def get_match_tests(): return _test_patterns -def set_match_tests(patterns=None, match_labels=None): - global _test_matchers, _test_patterns, _match_labels +def set_match_tests(patterns=None, match_labels=None, *, exact=False): + global _test_matchers, _test_patterns, _test_exact, _match_labels if not patterns: _test_matchers = () @@ -81,12 +82,14 @@ def set_match_tests(patterns=None, match_labels=None): else: itemgetter = operator.itemgetter patterns = tuple(patterns) - if patterns != _test_patterns: + if patterns != _test_patterns or exact != _test_exact: _test_matchers = [ - (_compile_match_function(map(itemgetter(0), it)), result) + (_compile_match_function(map(itemgetter(0), it), exact), + result) for result, it in itertools.groupby(patterns, itemgetter(1)) ] _test_patterns = patterns + _test_exact = exact if not match_labels: _match_labels = () @@ -100,10 +103,10 @@ def set_match_tests(patterns=None, match_labels=None): ) -def _compile_match_function(patterns): +def _compile_match_function(patterns, exact=False): patterns = list(patterns) - if all(map(_is_full_match_test, patterns)): + if exact or all(map(_is_full_match_test, patterns)): # Simple case: all patterns are full test identifier. # The test.bisect_cmd utility only uses such full test identifiers. return set(patterns).__contains__ diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 6c8a1f9a089ed3b..f46b99f254e2190 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -540,6 +540,7 @@ def create_run_tests(self, tests: TestTuple) -> RunTests: parallel_threads=self.parallel_threads, single_process_per_case=self.want_single_process_per_case, case_groups=None, + match_exact=False, ) def _run_tests(self, selected: TestTuple, tests: TestList | None) -> int: diff --git a/Lib/test/libregrtest/run_workers.py b/Lib/test/libregrtest/run_workers.py index 7118a67a2336c6f..94cf8b71c8cf8ae 100644 --- a/Lib/test/libregrtest/run_workers.py +++ b/Lib/test/libregrtest/run_workers.py @@ -281,6 +281,7 @@ def create_worker_runtests(self, test_name: TestName, if module_name is not None and test_name != module_name: tests = (module_name,) kwargs['match_tests'] = [(test_name, True)] + kwargs['match_exact'] = True else: tests = (test_name,) if self.runtests.rerun: diff --git a/Lib/test/libregrtest/runtests.py b/Lib/test/libregrtest/runtests.py index bb2e651e8671119..1a86547cb0b8887 100644 --- a/Lib/test/libregrtest/runtests.py +++ b/Lib/test/libregrtest/runtests.py @@ -106,6 +106,7 @@ class RunTests: parallel_threads: int | None single_process_per_case: bool case_groups: tuple[tuple[TestName, tuple[TestName, ...]], ...] | None + match_exact: bool def copy(self, **override) -> 'RunTests': state = dataclasses.asdict(self) diff --git a/Lib/test/libregrtest/setup.py b/Lib/test/libregrtest/setup.py index e1a9b231f8c15b9..70fdeabfb222a2e 100644 --- a/Lib/test/libregrtest/setup.py +++ b/Lib/test/libregrtest/setup.py @@ -112,7 +112,8 @@ def setup_tests(runtests: RunTests) -> None: support.PGO = runtests.pgo support.PGO_EXTENDED = runtests.pgo_extended - set_match_tests(runtests.match_tests, runtests.match_labels) + set_match_tests(runtests.match_tests, runtests.match_labels, + exact=runtests.match_exact) reporter = get_reporter() reporter.run_started(runtests) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index cd2cd8a21e8ea7f..8b4c7e3a204a14a 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -2572,6 +2572,33 @@ def test_single_process_per_case_skipped_module(self): self.check_executed_tests(output, [testname], skipped=[testname], stats=0) + def test_single_process_per_case_exact_id(self): + # Each worker selects its case by the exact id: brackets are not + # fnmatch patterns and a dot-free id does not match id components. + code = textwrap.dedent(""" + import doctest + import unittest + + class Tests(unittest.TestCase): + def test_one(self): + pass + + Bracketed = type('Tests[x]', (Tests,), {}) + + def load_tests(loader, tests, ignore): + parser = doctest.DocTestParser() + for name in ('example', 'example.a', 'example.b'): + test = parser.get_doctest('>>> 1 + 1\\n2\\n', {}, name, + None, 0) + tests.addTest(doctest.DocTestCase(test)) + return tests + """) + testname = self.create_test(code=code) + + output = self.run_tests('--single-process-per-case', testname) + self.check_line(output, 'All 5 tests OK.', regex=False) + self.check_line(output, 'Total tests: run=5', regex=False) + def test_verbose3(self): code = textwrap.dedent(r""" diff --git a/Misc/NEWS.d/next/Tests/2026-12-02-19-29-59.gh-issue-163034.D6BS6a.rst b/Misc/NEWS.d/next/Tests/2026-12-02-19-29-59.gh-issue-163034.D6BS6a.rst new file mode 100644 index 000000000000000..2b5a93bd7f4f4c2 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-12-02-19-29-59.gh-issue-163034.D6BS6a.rst @@ -0,0 +1,3 @@ +Fix ``python -m test --single-process-per-case`` running no test for a case id +containing brackets, and several tests for a case id without a dot. The case is +now selected by its exact id.