Debian Codegraph @

Lib/test/libregrtest/findtests.py

raw
1 import os
2 import sys
3 import unittest
4 from collections.abc import Container
5
6 from test import support
7
8 from .filter import match_test, set_match_tests
9 from .utils import (
10 StrPath, TestName, TestTuple, TestList, TestFilter,
11 abs_module_name, count, printlist)
12
13
14 # If these test directories are encountered recurse into them and treat each
15 # "test_*.py" file or each sub-directory as a separate test module. This can
16 # increase parallelism.
17 #
18 # Beware this can't generally be done for any directory with sub-tests as the
19 # __init__.py may do things which alter what tests are to be run.
20 SPLITTESTDIRS: set[TestName] = {
21 "test_asyncio",
22 "test_concurrent_futures",
23 "test_doctests",
24 "test_future_stmt",
25 "test_gdb",
26 "test_inspect",
27 "test_pydoc",
28 "test_multiprocessing_fork",
29 "test_multiprocessing_forkserver",
30 "test_multiprocessing_spawn",
31 }
32
33
34 def findtestdir(path: StrPath | None = None) -> StrPath:
35 return path or os.path.dirname(os.path.dirname(__file__)) or os.curdir
36
37
38 def findtests(*, testdir: StrPath | None = None, exclude: Container[str] = (),
39 split_test_dirs: set[TestName] = SPLITTESTDIRS,
40 base_mod: str = "") -> TestList:
41 """Return a list of all applicable test modules."""
42 testdir = findtestdir(testdir)
43 tests = []
44 for name in os.listdir(testdir):
45 mod, ext = os.path.splitext(name)
46 if (not mod.startswith("test_")) or (mod in exclude):
47 continue
48 if base_mod:
49 fullname = f"{base_mod}.{mod}"
50 else:
51 fullname = mod
52 if fullname in split_test_dirs:
53 subdir = os.path.join(testdir, mod)
54 if not base_mod:
55 fullname = f"test.{mod}"
56 tests.extend(findtests(testdir=subdir, exclude=exclude,
57 split_test_dirs=split_test_dirs,
58 base_mod=fullname))
59 elif ext in (".py", ""):
60 tests.append(fullname)
61 return sorted(tests)
62
63
64 def split_test_packages(tests, *, testdir: StrPath | None = None,
65 exclude: Container[str] = (),
66 split_test_dirs=SPLITTESTDIRS) -> list[TestName]:
67 testdir = findtestdir(testdir)
68 splitted = []
69 for name in tests:
70 if name in split_test_dirs:
71 subdir = os.path.join(testdir, name)
72 splitted.extend(findtests(testdir=subdir, exclude=exclude,
73 split_test_dirs=split_test_dirs,
74 base_mod=name))
75 else:
76 splitted.append(name)
77 return splitted
78
79
80 def _list_cases(suite: unittest.TestSuite) -> None:
81 for test in suite:
82 if isinstance(test, unittest.loader._FailedTest): # type: ignore[attr-defined]
83 continue
84 if isinstance(test, unittest.TestSuite):
85 _list_cases(test)
86 elif isinstance(test, unittest.TestCase):
87 if match_test(test):
88 print(test.id())
89
90 def list_cases(tests: TestTuple, *,
91 match_tests: TestFilter | None = None,
92 test_dir: StrPath | None = None) -> None:
93 support.verbose = False
94 set_match_tests(match_tests)
95
96 skipped = []
97 for test_name in tests:
98 module_name = abs_module_name(test_name, test_dir)
99 try:
100 suite = unittest.defaultTestLoader.loadTestsFromName(module_name)
101 _list_cases(suite)
102 except unittest.SkipTest:
103 skipped.append(test_name)
104
105 if skipped:
106 sys.stdout.flush()
107 stderr = sys.stderr
108 print(file=stderr)
109 print(count(len(skipped), "test"), "skipped:", file=stderr)
110 printlist(skipped, file=stderr)