Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b374559

Browse files
committedSep 11, 2023
pythongh-109162: libregrtest: move code around
* Move Regrtest.display_header() to utils.py. * Move cleanup_temp_dir() to utils.py. * Move list_cases() to findtests.py.
1 parent 3b2ecbc commit b374559

File tree

3 files changed

+105
-93
lines changed

3 files changed

+105
-93
lines changed
 

‎Lib/test/libregrtest/findtests.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import os
2+
import sys
3+
import unittest
24

3-
from .utils import StrPath, TestName, TestList
5+
from test import support
6+
7+
from .utils import (
8+
StrPath, TestName, TestTuple, TestList, FilterTuple,
9+
abs_module_name, count, printlist)
410

511

612
# If these test directories are encountered recurse into them and treat each
@@ -56,3 +62,37 @@ def split_test_packages(tests, *, testdir: StrPath | None = None, exclude=(),
5662
else:
5763
splitted.append(name)
5864
return splitted
65+
66+
67+
def _list_cases(suite):
68+
for test in suite:
69+
if isinstance(test, unittest.loader._FailedTest):
70+
continue
71+
if isinstance(test, unittest.TestSuite):
72+
_list_cases(test)
73+
elif isinstance(test, unittest.TestCase):
74+
if support.match_test(test):
75+
print(test.id())
76+
77+
def list_cases(tests: TestTuple, *,
78+
match_tests: FilterTuple | None = None,
79+
ignore_tests: FilterTuple | None = None,
80+
test_dir: StrPath | None = None):
81+
support.verbose = False
82+
support.set_match_tests(match_tests, ignore_tests)
83+
84+
skipped = []
85+
for test_name in tests:
86+
module_name = abs_module_name(test_name, test_dir)
87+
try:
88+
suite = unittest.defaultTestLoader.loadTestsFromName(module_name)
89+
_list_cases(suite)
90+
except unittest.SkipTest:
91+
skipped.append(test_name)
92+
93+
if skipped:
94+
sys.stdout.flush()
95+
stderr = sys.stderr
96+
print(file=stderr)
97+
print(count(len(skipped), "test"), "skipped:", file=stderr)
98+
printlist(skipped, file=stderr)

‎Lib/test/libregrtest/main.py

+9-92
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import locale
21
import os
3-
import platform
42
import random
53
import re
64
import sys
75
import time
8-
import unittest
96

107
from test import support
118
from test.support import os_helper
129

1310
from .cmdline import _parse_args, Namespace
14-
from .findtests import findtests, split_test_packages
11+
from .findtests import findtests, split_test_packages, list_cases
1512
from .logger import Logger
1613
from .result import State
1714
from .runtests import RunTests, HuntRefleak
@@ -22,8 +19,8 @@
2219
from .utils import (
2320
StrPath, StrJSON, TestName, TestList, TestTuple, FilterTuple,
2421
strip_py_suffix, count, format_duration,
25-
printlist, get_build_info, get_temp_dir, get_work_dir, exit_timeout,
26-
abs_module_name)
22+
printlist, get_temp_dir, get_work_dir, exit_timeout,
23+
display_header, cleanup_temp_dir)
2724

2825

2926
class Regrtest:
@@ -214,36 +211,6 @@ def list_tests(tests: TestTuple):
214211
for name in tests:
215212
print(name)
216213

217-
def _list_cases(self, suite):
218-
for test in suite:
219-
if isinstance(test, unittest.loader._FailedTest):
220-
continue
221-
if isinstance(test, unittest.TestSuite):
222-
self._list_cases(test)
223-
elif isinstance(test, unittest.TestCase):
224-
if support.match_test(test):
225-
print(test.id())
226-
227-
def list_cases(self, tests: TestTuple):
228-
support.verbose = False
229-
support.set_match_tests(self.match_tests, self.ignore_tests)
230-
231-
skipped = []
232-
for test_name in tests:
233-
module_name = abs_module_name(test_name, self.test_dir)
234-
try:
235-
suite = unittest.defaultTestLoader.loadTestsFromName(module_name)
236-
self._list_cases(suite)
237-
except unittest.SkipTest:
238-
skipped.append(test_name)
239-
240-
if skipped:
241-
sys.stdout.flush()
242-
stderr = sys.stderr
243-
print(file=stderr)
244-
print(count(len(skipped), "test"), "skipped:", file=stderr)
245-
printlist(skipped, file=stderr)
246-
247214
def _rerun_failed_tests(self, runtests: RunTests):
248215
# Configure the runner to re-run tests
249216
if self.num_workers == 0:
@@ -363,45 +330,6 @@ def run_tests_sequentially(self, runtests):
363330

364331
return tracer
365332

366-
@staticmethod
367-
def display_header():
368-
# Print basic platform information
369-
print("==", platform.python_implementation(), *sys.version.split())
370-
print("==", platform.platform(aliased=True),
371-
"%s-endian" % sys.byteorder)
372-
print("== Python build:", ' '.join(get_build_info()))
373-
print("== cwd:", os.getcwd())
374-
cpu_count = os.cpu_count()
375-
if cpu_count:
376-
print("== CPU count:", cpu_count)
377-
print("== encodings: locale=%s, FS=%s"
378-
% (locale.getencoding(), sys.getfilesystemencoding()))
379-
380-
# This makes it easier to remember what to set in your local
381-
# environment when trying to reproduce a sanitizer failure.
382-
asan = support.check_sanitizer(address=True)
383-
msan = support.check_sanitizer(memory=True)
384-
ubsan = support.check_sanitizer(ub=True)
385-
sanitizers = []
386-
if asan:
387-
sanitizers.append("address")
388-
if msan:
389-
sanitizers.append("memory")
390-
if ubsan:
391-
sanitizers.append("undefined behavior")
392-
if not sanitizers:
393-
return
394-
395-
print(f"== sanitizers: {', '.join(sanitizers)}")
396-
for sanitizer, env_var in (
397-
(asan, "ASAN_OPTIONS"),
398-
(msan, "MSAN_OPTIONS"),
399-
(ubsan, "UBSAN_OPTIONS"),
400-
):
401-
options= os.environ.get(env_var)
402-
if sanitizer and options is not None:
403-
print(f"== {env_var}={options!r}")
404-
405333
def get_state(self):
406334
state = self.results.get_state(self.fail_env_changed)
407335
if self.first_state:
@@ -445,20 +373,6 @@ def display_summary(self):
445373
state = self.get_state()
446374
print(f"Result: {state}")
447375

448-
@staticmethod
449-
def cleanup_temp_dir(tmp_dir: StrPath):
450-
import glob
451-
452-
path = os.path.join(glob.escape(tmp_dir), 'test_python_*')
453-
print("Cleanup %s directory" % tmp_dir)
454-
for name in glob.glob(path):
455-
if os.path.isdir(name):
456-
print("Remove directory: %s" % name)
457-
os_helper.rmtree(name)
458-
else:
459-
print("Remove file: %s" % name)
460-
os_helper.unlink(name)
461-
462376
def create_run_tests(self, tests: TestTuple):
463377
return RunTests(
464378
tests,
@@ -496,7 +410,7 @@ def _run_tests(self, selected: TestTuple, tests: TestList | None) -> int:
496410
if (self.want_header
497411
or not(self.pgo or self.quiet or self.single_test_run
498412
or tests or self.cmdline_args)):
499-
self.display_header()
413+
display_header()
500414

501415
if self.randomize:
502416
print("Using random seed", self.random_seed)
@@ -554,7 +468,7 @@ def main(self, tests: TestList | None = None):
554468
self.tmp_dir = get_temp_dir(self.tmp_dir)
555469

556470
if self.want_cleanup:
557-
self.cleanup_temp_dir(self.tmp_dir)
471+
cleanup_temp_dir(self.tmp_dir)
558472
sys.exit(0)
559473

560474
if self.want_wait:
@@ -567,7 +481,10 @@ def main(self, tests: TestList | None = None):
567481
if self.want_list_tests:
568482
self.list_tests(selected)
569483
elif self.want_list_cases:
570-
self.list_cases(selected)
484+
list_cases(selected,
485+
match_tests=self.match_tests,
486+
ignore_tests=self.ignore_tests,
487+
test_dir=self.test_dir)
571488
else:
572489
exitcode = self.run_tests(selected, tests)
573490

‎Lib/test/libregrtest/utils.py

+55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import atexit
22
import contextlib
33
import faulthandler
4+
import locale
45
import math
56
import os.path
7+
import platform
68
import random
79
import sys
810
import sysconfig
@@ -524,3 +526,56 @@ def adjust_rlimit_nofile():
524526
except (ValueError, OSError) as err:
525527
print_warning(f"Unable to raise RLIMIT_NOFILE from {fd_limit} to "
526528
f"{new_fd_limit}: {err}.")
529+
530+
531+
def display_header():
532+
# Print basic platform information
533+
print("==", platform.python_implementation(), *sys.version.split())
534+
print("==", platform.platform(aliased=True),
535+
"%s-endian" % sys.byteorder)
536+
print("== Python build:", ' '.join(get_build_info()))
537+
print("== cwd:", os.getcwd())
538+
cpu_count = os.cpu_count()
539+
if cpu_count:
540+
print("== CPU count:", cpu_count)
541+
print("== encodings: locale=%s, FS=%s"
542+
% (locale.getencoding(), sys.getfilesystemencoding()))
543+
544+
# This makes it easier to remember what to set in your local
545+
# environment when trying to reproduce a sanitizer failure.
546+
asan = support.check_sanitizer(address=True)
547+
msan = support.check_sanitizer(memory=True)
548+
ubsan = support.check_sanitizer(ub=True)
549+
sanitizers = []
550+
if asan:
551+
sanitizers.append("address")
552+
if msan:
553+
sanitizers.append("memory")
554+
if ubsan:
555+
sanitizers.append("undefined behavior")
556+
if not sanitizers:
557+
return
558+
559+
print(f"== sanitizers: {', '.join(sanitizers)}")
560+
for sanitizer, env_var in (
561+
(asan, "ASAN_OPTIONS"),
562+
(msan, "MSAN_OPTIONS"),
563+
(ubsan, "UBSAN_OPTIONS"),
564+
):
565+
options= os.environ.get(env_var)
566+
if sanitizer and options is not None:
567+
print(f"== {env_var}={options!r}")
568+
569+
570+
def cleanup_temp_dir(tmp_dir: StrPath):
571+
import glob
572+
573+
path = os.path.join(glob.escape(tmp_dir), 'test_python_*')
574+
print("Cleanup %s directory" % tmp_dir)
575+
for name in glob.glob(path):
576+
if os.path.isdir(name):
577+
print("Remove directory: %s" % name)
578+
os_helper.rmtree(name)
579+
else:
580+
print("Remove file: %s" % name)
581+
os_helper.unlink(name)

0 commit comments

Comments
 (0)
Please sign in to comment.