8000 gh-108828: Support selecting tests by labels by serhiy-storchaka · Pull Request #108829 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-108828: Support selecting tests by labels #108829

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make the order of options matter.
  • Loading branch information
serhiy-storchaka committed Oct 29, 2023
commit c884b553b799565bbd67f9261ddc860d50f251f4
7 changes: 3 additions & 4 deletions Lib/test/libregrtest/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def __init__(self, **kwargs) -> None:
self.header = False
self.failfast = False
self.match_tests = []
self.accept_labels = None
self.ignore_labels = None
self.match_labels = []
self.pgo = False
self.pgo_extended = False
self.worker_json = None
Expand Down Expand Up @@ -273,10 +272,10 @@ def _create_parser():
dest='match_tests', action=FilterAction, const=False,
help='ignore test cases and methods with glob pattern PAT')
group.add_argument('--label', metavar='NAME',
dest='accept_labels', action='append',
dest='match_labels', action=FilterAction, const=True,
help='match test cases and methods with label NAME')
group.add_argument('--no-label', metavar='NAME',
dest='ignore_labels', action='append',
dest='match_labels', action=FilterAction, const=False,
help='ignore test cases and methods with label NAME')
group.add_argument('--matchfile', metavar='FILENAME',
dest='match_tests',
Expand Down
82 changes: 30 additions & 52 deletions Lib/test/libregrtest/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,44 @@
# By default, don't filter tests
_test_matchers = ()
_test_patterns = ()
_match_test_func2 = None
_match_labels = ()


def match_test1(test):
def match_test(test):
# Function used by support.run_unittest() and regrtest --list-cases
return match_test_id(test) and match_test_label(test)

def match_test_id(test):
result = False
for matcher, result in reversed(_test_matchers):
if matcher(test.id()):
return result
return not result

def match_test(test):
# Function used by support.run_unittest() and regrtest --list-cases
return (match_test1(test) and
(_match_test_func2 is None or _match_test_func2(test)))
def match_test_label(test):
result = False
for label, result in reversed(_match_labels):
if _has_label(test, label):
return result
return not result

def _has_label(test, label):
attrname = f'_label_{label}'
if hasattr(test, attrname):
return True
testMethod = getattr(test, test._testMethodName)
while testMethod is not None:
if hasattr(testMethod, attrname):
return True
testMethod = getattr(testMethod, '__wrapped__', None)
try:
module = sys.modules[test.__class__.__module__]
if hasattr(module, attrname):
return True
except KeyError:
pass
return False


def _is_full_match_test(pattern):
# If a pattern contains at least one dot, it's considered
Expand All @@ -33,7 +56,7 @@ def _is_full_match_test(pattern):
return ('.' in pattern) and (not re.search(r'[?*\[\]]', pattern))


def set_match_tests(patterns):
def set_match_tests(patterns=None, match_labels=None):
global _test_matchers, _test_patterns

if not patterns:
Expand All @@ -50,51 +73,6 @@ def set_match_tests(patterns):
_test_patterns = patterns


def _check_obj_labels(obj, labels):
for label in labels:
if hasattr(obj, f'_label_{label}'):
return True
return False

def _check_test_labels(test, labels):
if _check_obj_labels(test, labels):
return True
testMethod = getattr(test, test._testMethodName)
while testMethod is not None:
if _check_obj_labels(testMethod, labels):
return True
testMethod = getattr(testMethod, '__wrapped__', None)
try:
module = sys.modules[test.__class__.__module__]
if _check_obj_labels(module, labels):
return True
except KeyError:
pass
return False

def set_match_tests2(accept_labels=None, ignore_labels=None):
global _match_test_func2

if accept_labels is None:
accept_labels = ()
if ignore_labels is None:
ignore_labels = ()
# Create a copy since label lists can be mutable and so modified later
accept_labels = tuple(accept_labels)
ignore_labels = tuple(ignore_labels)

def match_function(test):
accept = True
ignore = False
if accept_labels:
accept = _check_test_labels(test, accept_labels)
if ignore_labels:
ignore = _check_test_labels(test, ignore_labels)
return accept and not ignore

_match_test_func2 = match_function


def _compile_match_function(patterns):
patterns = list(patterns)

Expand Down
8 changes: 3 additions & 5 deletions Lib/test/libregrtest/findtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from test import support

from .filter import match_test, set_match_tests, set_match_tests2
from .filter import match_test, set_match_tests
from .utils import (
StrPath, TestName, TestTuple, TestList, TestFilter,
abs_module_name, count, printlist)
Expand Down Expand Up @@ -85,12 +85,10 @@ def _list_cases(suite):

def list_cases(tests: TestTuple, *,
match_tests: TestFilter | None = None,
accept_labels: tuple[str, ...] | None = None,
ignore_labels: tuple[str, ...] | None = None,
match_labels: TestFilter | None = None,
test_dir: StrPath | None = None):
support.verbose = False
set_match_tests(match_tests)
set_match_tests2(accept_labels, ignore_labels)
set_match_tests(match_tests, match_labels)

skipped = []
for test_name in tests:
Expand Down
15 changes: 3 additions & 12 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):

# Select tests
self.match_tests: TestFilter = ns.match_tests
if ns.accept_labels:
self.accept_labels: tuple[str, ...] = tuple(ns.accept_labels)
else:
self.accept_labels = None
if ns.ignore_labels:
self.ignore_labels: tuple[str, ...] = tuple(ns.ignore_labels)
else:
self.ignore_labels = None
self.match_labels: TestFilter = ns.match_labels
self.exclude: bool = ns.exclude
self.fromfile: StrPath | None = ns.fromfile
self.starting_test: TestName | None = ns.start
Expand Down Expand Up @@ -408,8 +401,7 @@ def create_run_tests(self, tests: TestTuple):
fail_fast=self.fail_fast,
fail_env_changed=self.fail_env_changed,
match_tests=self.match_tests,
accept_labels=self.accept_labels,
ignore_labels=self.ignore_labels,
match_labels=self.match_labels,
match_tests_dict=None,
rerun=False,
forever=self.forever,
Expand Down Expand Up @@ -662,8 +654,7 @@ def main(self, tests: TestList | None = None):
elif self.want_list_cases:
list_cases(selected,
match_tests=self.match_tests,
accept_labels=self.accept_labels,
ignore_labels=self.ignore_labels,
match_labels=self.match_labels,
test_dir=self.test_dir)
else:
exitcode = self.run_tests(selected, tests)
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/libregrtest/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ class RunTests:
fail_fast: bool
fail_env_changed: bool
match_tests: TestFilter
accept_labels: tuple[str, ...] | None
ignore_labels: tuple[str, ...] | None
match_labels: TestFilter
match_tests_dict: FilterDict | None
rerun: bool
forever: bool
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/libregrtest/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from test import support
from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII

from .filter import set_match_tests, set_match_tests2
from .filter import set_match_tests
from .runtests import RunTests
from .utils import (
setup_unraisable_hook, setup_threading_excepthook, fix_umask,
Expand Down Expand Up @@ -93,8 +93,7 @@ def setup_tests(runtests: RunTests):
support.PGO = runtests.pgo
support.PGO_EXTENDED = runtests.pgo_extended

set_match_tests(runtests.match_tests)
set_match_tests2(runtests.accept_labels, runtests.ignore_labels)
set_match_tests(runtests.match_tests, runtests.match_labels)

if runtests.use_junit:
support.junit_xml_list = []
Expand Down
0