Skip to content
Open
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
15 changes: 9 additions & 6 deletions Lib/test/libregrtest/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -72,21 +73,23 @@ 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 = ()
_test_patterns = ()
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 = ()
Expand All @@ -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__
Expand Down
1 change: 1 addition & 0 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Lib/test/libregrtest/run_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Lib/test/libregrtest/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/libregrtest/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading