Skip to content

Commit afbccd6

Browse files
committed
pythongh-108834: Refactor Regrtest.main()
* main() now calls _parse_args() and pass 'ns' to Regrtest constructor. Remove kwargs argument from Regrtest.main(). * _parse_args() checks ns.huntrleaks. * set_temp_dir() is now responsible to call expanduser(). * Regrtest.main() sets self.tests earlier. * Add TestsList type.
1 parent 1f7e421 commit afbccd6

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

Lib/test/libregrtest/cmdline.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,13 @@ def _parse_args(args, **kwargs):
448448
# --forever implies --failfast
449449
ns.failfast = True
450450

451+
if ns.huntrleaks:
452+
warmup, repetitions, _ = ns.huntrleaks
453+
if warmup < 1 or repetitions < 1:
454+
msg = ("Invalid values for the --huntrleaks/-R parameters. The "
455+
"number of warmups and repetitions must be at least 1 "
456+
"each (1:1).")
457+
print(msg, file=sys.stderr, flush=True)
458+
sys.exit(2)
459+
451460
return ns

Lib/test/libregrtest/main.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import tempfile
1010
import time
1111
import unittest
12-
from test.libregrtest.cmdline import _parse_args
12+
from test.libregrtest.cmdline import _parse_args, Namespace
1313
from test.libregrtest.runtest import (
1414
findtests, split_test_packages, runtest, abs_module_name,
15-
PROGRESS_MIN_TIME, State, MatchTestsDict, RunTests)
15+
PROGRESS_MIN_TIME, State, MatchTestsDict, RunTests, TestResult)
1616
from test.libregrtest.setup import setup_tests
1717
from test.libregrtest.pgo import setup_pgo_tests
1818
from test.libregrtest.utils import (strip_py_suffix, count, format_duration,
@@ -35,6 +35,9 @@
3535
EXITCODE_INTERRUPTED = 130
3636

3737

38+
TestsList = list[str]
39+
40+
3841
class Regrtest:
3942
"""Execute a test suite.
4043
@@ -58,24 +61,24 @@ class Regrtest:
5861
directly to set the values that would normally be set by flags
5962
on the command line.
6063
"""
61-
def __init__(self):
64+
def __init__(self, ns: Namespace):
6265
# Namespace of command line options
63-
self.ns = None
66+
self.ns: Namespace = ns
6467

6568
# tests
6669
self.tests = []
6770
self.selected = []
6871
self.all_runtests: list[RunTests] = []
6972

7073
# test results
71-
self.good: list[str] = []
72-
self.bad: list[str] = []
73-
self.rerun_bad: list[str] = []
74-
self.skipped: list[str] = []
75-
self.resource_denied: list[str] = []
76-
self.environment_changed: list[str] = []
77-
self.run_no_tests: list[str] = []
78-
self.rerun: list[str] = []
74+
self.good: TestsList = []
75+
self.bad: TestsList = []
76+
self.rerun_bad: TestsList = []
77+
self.skipped: TestsList = []
78+
self.resource_denied: TestsList = []
79+
self.environment_changed: TestsList = []
80+
self.run_no_tests: TestsList = []
81+
self.rerun: TestsList = []
7982

8083
self.need_rerun: list[TestResult] = []
8184
self.first_state: str | None = None
@@ -184,29 +187,7 @@ def display_progress(self, test_index, text):
184187
line = f"{line}/{fails}"
185188
self.log(f"[{line}] {text}")
186189

187-
def parse_args(self, kwargs):
188-
ns = _parse_args(sys.argv[1:], **kwargs)
189-
190-
if ns.xmlpath:
191-
support.junit_xml_list = self.testsuite_xml = []
192-
193-
strip_py_suffix(ns.args)
194-
195-
if ns.huntrleaks:
196-
warmup, repetitions, _ = ns.huntrleaks
197-
if warmup < 1 or repetitions < 1:
198-
msg = ("Invalid values for the --huntrleaks/-R parameters. The "
199-
"number of warmups and repetitions must be at least 1 "
200-
"each (1:1).")
201-
print(msg, file=sys.stderr, flush=True)
202-
sys.exit(2)
203-
204-
if ns.tempdir:
205-
ns.tempdir = os.path.expanduser(ns.tempdir)
206-
207-
self.ns = ns
208-
209-
def find_tests(self, tests):
190+
def find_tests(self):
210191
ns = self.ns
211192
single = ns.single
212193
fromfile = ns.fromfile
@@ -216,8 +197,6 @@ def find_tests(self, tests):
216197
starting_test = ns.start
217198
randomize = ns.randomize
218199

219-
self.tests = tests
220-
221200
if single:
222201
self.next_single_filename = os.path.join(self.tmp_dir, 'pynexttest')
223202
try:
@@ -737,8 +716,12 @@ def fix_umask(self):
737716
os.umask(old_mask)
738717

739718
def set_temp_dir(self):
740-
if self.ns.tempdir:
741-
self.tmp_dir = self.ns.tempdir
719+
ns = self.ns
720+
if ns.tempdir:
721+
ns.tempdir = os.path.expanduser(ns.tempdir)
722+
723+
if ns.tempdir:
724+
self.tmp_dir = ns.tempdir
742725

743726
if not self.tmp_dir:
744727
# When tests are run from the Python build directory, it is best practice
@@ -795,14 +778,20 @@ def cleanup(self):
795778
print("Remove file: %s" % name)
796779
os_helper.unlink(name)
797780

798-
def main(self, tests=None, **kwargs):
799-
self.parse_args(kwargs)
781+
def main(self, tests: TestsList | None = None):
782+
ns = self.ns
783+
self.tests = tests
784+
785+
if ns.xmlpath:
786+
support.junit_xml_list = self.testsuite_xml = []
787+
788+
strip_py_suffix(ns.args)
800789

801790
self.set_temp_dir()
802791

803792
self.fix_umask()
804793

805-
if self.ns.cleanup:
794+
if ns.cleanup:
806795
self.cleanup()
807796
sys.exit(0)
808797

@@ -817,9 +806,9 @@ def main(self, tests=None, **kwargs):
817806
# When using multiprocessing, worker processes will use test_cwd
818807
# as their parent temporary directory. So when the main process
819808
# exit, it removes also subdirectories of worker processes.
820-
self.ns.tempdir = test_cwd
809+
ns.tempdir = test_cwd
821810

822-
self._main(tests, kwargs)
811+
self._main()
823812
except SystemExit as exc:
824813
# bpo-38203: Python can hang at exit in Py_Finalize(), especially
825814
# on threading._shutdown() call: put a timeout
@@ -862,7 +851,7 @@ def action_run_tests(self):
862851
self.display_summary()
863852
self.finalize()
864853

865-
def _main(self, tests, kwargs):
854+
def _main(self):
866855
if self.is_worker():
867856
from test.libregrtest.runtest_mp import run_tests_worker
868857
run_tests_worker(self.ns.worker_args)
@@ -872,7 +861,7 @@ def _main(self, tests, kwargs):
872861
input("Press any key to continue...")
873862

874863
setup_tests(self.ns)
875-
self.find_tests(tests)
864+
self.find_tests()
876865

877866
exitcode = 0
878867
if self.ns.list_tests:
@@ -888,4 +877,5 @@ def _main(self, tests, kwargs):
888877

889878
def main(tests=None, **kwargs):
890879
"""Run the Python suite."""
891-
Regrtest().main(tests=tests, **kwargs)
880+
ns = _parse_args(sys.argv[1:], **kwargs)
881+
Regrtest(ns).main(tests=tests)

0 commit comments

Comments
 (0)