Skip to content
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
Merge branch 'main' into libregrtest-labels
  • Loading branch information
serhiy-storchaka committed Oct 29, 2023
commit 4f41c7568cca534d14a15c9875b51c2d24c9beb1
36 changes: 1 addition & 35 deletions Doc/library/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -514,48 +514,14 @@ The :mod:`test.support` module defines the following functions:

.. function:: match_test(test)

Determine whether *test* matches the patterns set in :func:`set_match_tests`
and labels set in :func:`set_match_tests2`.
Determine whether *test* matches the patterns set in :func:`set_match_tests`.


.. function:: set_match_tests(accept_patterns=None, ignore_patterns=None)

Define match patterns on test filenames and test method names for filtering tests.


.. function:: set_match_tests2(accept_labels=None, ignore_labels=None)

Define labels on tests for filtering.


.. function:: run_unittest(*classes)

Execute :class:`unittest.TestCase` subclasses passed to the function. The
function scans the classes for methods starting with the prefix ``test_``
and executes the tests individually.

It is also legal to pass strings as parameters; these should be keys in
``sys.modules``. Each associated module will be scanned by
``unittest.TestLoader.loadTestsFromModule()``. This is usually seen in the
following :func:`test_main` function::

def test_main():
support.run_unittest(__name__)

This will run all tests defined in the named module.


.. function:: run_doctest(module, verbosity=None, optionflags=0)

Run :func:`doctest.testmod` on the given *module*. Return
``(failure_count, test_count)``.

If *verbosity* is ``None``, :func:`doctest.testmod` is run with verbosity
set to :data:`verbose`. Otherwise, it is run with verbosity set to
``None``. *optionflags* is passed as ``optionflags`` to
:func:`doctest.testmod`.


.. function:: get_pagesize()

Get size of a page in bytes.
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/libregrtest/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ def __init__(self, **kwargs) -> None:
self.forever = False
self.header = False
self.failfast = False
self.match_tests = None
self.ignore_tests = None
self.match_tests = []
self.accept_labels = None
self.ignore_labels = None
self.pgo = False
Expand Down
53 changes: 52 additions & 1 deletion Lib/test/libregrtest/filter.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import itertools
import operator
import re
import sys


# By default, don't filter tests
_test_matchers = ()
_test_patterns = ()
_match_test_func2 = None


def match_test(test):
def match_test1(test):
# Function used by support.run_unittest() and regrtest --list-cases
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 _is_full_match_test(pattern):
# If a pattern contains at least one dot, it's considered
Expand Down Expand Up @@ -44,6 +50,51 @@ 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
9 changes: 4 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
from .filter import match_test, set_match_tests, set_match_tests2
from .utils import (
StrPath, TestName, TestTuple, TestList, TestFilter,
abs_module_name, count, printlist)
Expand Down Expand Up @@ -84,14 +84,13 @@ def _list_cases(suite):
print(test.id())

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

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

# Select tests
if ns.match_tests:
self.match_tests: FilterTuple = tuple(ns.match_tests)
else:
self.match_tests = None
if ns.ignore_tests:
self.ignore_tests: FilterTuple = tuple(ns.ignore_tests)
else:
self.ignore_tests = None
self.match_tests: TestFilter = ns.match_tests
if ns.accept_labels:
self.accept_labels: tuple[str, ...] = tuple(ns.accept_labels)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe set[str]?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reasons they should be immutable, as well as match_tests and ignore_tests.

In any case, I am planning to rewrite filtering by names. Instead of two tuples there will be more complex structure where order matters. And the same can be used for labels.

else:
Expand Down Expand Up @@ -415,6 +408,8 @@ 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_tests_dict=None,
rerun=False,
forever=self.forever,
Expand Down Expand Up @@ -667,7 +662,6 @@ def main(self, tests: TestList | None = None):
elif self.want_list_cases:
list_cases(selected,
match_tests=self.match_tests,
ignore_tests=self.ignore_tests,
accept_labels=self.accept_labels,
ignore_labels=self.ignore_labels,
test_dir=self.test_dir)
Expand Down
46 changes: 24 additions & 22 deletions Lib/test/libregrtest/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,30 @@ class HuntRefleak:
@dataclasses.dataclass(slots=True, frozen=True)
class RunTests:
tests: TestTuple
fail_fast: bool = False
fail_env_changed: bool = False
match_tests: FilterTuple | None = None
ignore_tests: FilterTuple | None = None
accept_labels: tuple[str, ...] | None = None
ignore_labels: tuple[str, ...] | None = None
match_tests_dict: FilterDict | None = None
rerun: bool = False
forever: bool = False
pgo: bool = False
pgo_extended: bool = False
output_on_failure: bool = False
timeout: float | None = None
verbose: int = 0
quiet: bool = False
hunt_refleak: HuntRefleak | None = None
test_dir: StrPath | None = None
use_junit: bool = False
memory_limit: str | None = None
gc_threshold: int | None = None
use_resources: list[str] = dataclasses.field(default_factory=list)
python_cmd: list[str] | None = None
fail_fast: bool
fail_env_changed: bool
match_tests: TestFilter
accept_labels: tuple[str, ...] | None
ignore_labels: tuple[str, ...] | None
match_tests_dict: FilterDict | None
rerun: bool
forever: bool
pgo: bool
pgo_extended: bool
output_on_failure: bool
timeout: float | None
verbose: int
quiet: bool
hunt_refleak: HuntRefleak | None
test_dir: StrPath | None
use_junit: bool
memory_limit: str | None
gc_threshold: int | None
use_resources: tuple[str, ...]
python_cmd: tuple[str, ...] | None
randomize: bool
random_seed: int | str
json_file: JsonFile | None

def copy(self, **override):
state = dataclasses.asdict(self)
Expand Down
6 changes: 3 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
from .filter import set_match_tests, set_match_tests2
from .runtests import RunTests
from .utils import (
setup_unraisable_hook, setup_threading_excepthook, fix_umask,
Expand Down Expand Up @@ -93,8 +93,8 @@ def setup_tests(runtests: RunTests):
support.PGO = runtests.pgo
support.PGO_EXTENDED = runtests.pgo_extended

support.set_match_tests(runtests.match_tests, runtests.ignore_tests)
support.set_match_tests2(runtests.accept_labels, runtests.ignore_labels)
set_match_tests(runtests.match_tests)
set_match_tests2(runtests.accept_labels, runtests.ignore_labels)

if runtests.use_junit:
support.junit_xml_list = []
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.