Skip to content

Commit 47fbc4e

Browse files
authored
bpo-37359: Add --cleanup option to python3 -m test (GH-14332)
* regrtest: Add --cleanup option to remove "test_python_*" directories of previous failed test jobs. * Add "make cleantest" to run "python3 -m test --cleanup".
1 parent 2ac3bab commit 47fbc4e

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

Lib/test/libregrtest/cmdline.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,10 @@ def _create_parser():
272272
group.add_argument('--junit-xml', dest='xmlpath', metavar='FILENAME',
273273
help='writes JUnit-style XML results to the specified '
274274
'file')
275-
group.add_argument('--tempdir', dest='tempdir', metavar='PATH',
275+
group.add_argument('--tempdir', metavar='PATH',
276276
help='override the working directory for the test run')
277+
group.add_argument('--cleanup', action='store_true',
278+
help='remove old test_python_* directories')
277279
return parser
278280

279281

Lib/test/libregrtest/main.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,19 @@ def parse_args(self, kwargs):
173173
# Strip .py extensions.
174174
removepy(ns.args)
175175

176-
return ns
176+
if ns.huntrleaks:
177+
warmup, repetitions, _ = ns.huntrleaks
178+
if warmup < 1 or repetitions < 1:
179+
msg = ("Invalid values for the --huntrleaks/-R parameters. The "
180+
"number of warmups and repetitions must be at least 1 "
181+
"each (1:1).")
182+
print(msg, file=sys.stderr, flush=True)
183+
sys.exit(2)
184+
185+
if ns.tempdir:
186+
ns.tempdir = os.path.expanduser(ns.tempdir)
187+
188+
self.ns = ns
177189

178190
def find_tests(self, tests):
179191
self.tests = tests
@@ -537,7 +549,7 @@ def save_xml_result(self):
537549
for s in ET.tostringlist(root):
538550
f.write(s)
539551

540-
def create_temp_dir(self):
552+
def set_temp_dir(self):
541553
if self.ns.tempdir:
542554
self.tmp_dir = self.ns.tempdir
543555

@@ -558,21 +570,43 @@ def create_temp_dir(self):
558570
self.tmp_dir = tempfile.gettempdir()
559571

560572
self.tmp_dir = os.path.abspath(self.tmp_dir)
573+
574+
def create_temp_dir(self):
561575
os.makedirs(self.tmp_dir, exist_ok=True)
562576

563577
# Define a writable temp dir that will be used as cwd while running
564578
# the tests. The name of the dir includes the pid to allow parallel
565579
# testing (see the -j option).
566580
pid = os.getpid()
567581
if self.worker_test_name is not None:
568-
test_cwd = 'worker_{}'.format(pid)
582+
test_cwd = 'test_python_worker_{}'.format(pid)
569583
else:
570584
test_cwd = 'test_python_{}'.format(pid)
571585
test_cwd = os.path.join(self.tmp_dir, test_cwd)
572586
return test_cwd
573587

588+
def cleanup(self):
589+
import glob
590+
import shutil
591+
592+
path = os.path.join(self.tmp_dir, 'test_python_*')
593+
print("Cleanup %s directory" % self.tmp_dir)
594+
for name in glob.glob(path):
595+
print("Remove directory: %s" % name)
596+
if os.path.isdir(name):
597+
support.rmtree(name)
598+
else:
599+
print("Remove file: %s" % name)
600+
support.unlink(name)
601+
574602
def main(self, tests=None, **kwargs):
575-
self.ns = self.parse_args(kwargs)
603+
self.parse_args(kwargs)
604+
605+
self.set_temp_dir()
606+
607+
if self.ns.cleanup:
608+
self.cleanup()
609+
sys.exit(0)
576610

577611
test_cwd = self.create_temp_dir()
578612

@@ -597,15 +631,6 @@ def getloadavg(self):
597631
return None
598632

599633
def _main(self, tests, kwargs):
600-
if self.ns.huntrleaks:
601-
warmup, repetitions, _ = self.ns.huntrleaks
602-
if warmup < 1 or repetitions < 1:
603-
msg = ("Invalid values for the --huntrleaks/-R parameters. The "
604-
"number of warmups and repetitions must be at least 1 "
605-
"each (1:1).")
606-
print(msg, file=sys.stderr, flush=True)
607-
sys.exit(2)
608-
609634
if self.worker_test_name is not None:
610635
from test.libregrtest.runtest_mp import run_tests_worker
611636
run_tests_worker(self.ns, self.worker_test_name)

Lib/test/test_regrtest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,21 @@ def test_unraisable_exc(self):
11561156
fail_env_changed=True)
11571157
self.assertIn("Warning -- Unraisable exception", output)
11581158

1159+
def test_cleanup(self):
1160+
dirname = os.path.join(self.tmptestdir, "test_python_123")
1161+
os.mkdir(dirname)
1162+
filename = os.path.join(self.tmptestdir, "test_python_456")
1163+
open(filename, "wb").close()
1164+
names = [dirname, filename]
1165+
1166+
cmdargs = ['-m', 'test',
1167+
'--tempdir=%s' % self.tmptestdir,
1168+
'--cleanup']
1169+
self.run_python(cmdargs)
1170+
1171+
for name in names:
1172+
self.assertFalse(os.path.exists(name), name)
1173+
11591174

11601175
class TestUtils(unittest.TestCase):
11611176
def test_format_duration(self):

Makefile.pre.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@ TESTTIMEOUT= 1200
11041104

11051105
.PHONY: test testall testuniversal buildbottest pythoninfo
11061106

1107+
# Remove "test_python_*" directories of previous failed test jobs.
1108+
# Pass TESTOPTS options because it can contain --tempdir option.
1109+
cleantest: build_all
1110+
$(TESTRUNNER) $(TESTOPTS) --cleanup
1111+
11071112
# Run a basic set of regression tests.
11081113
# This excludes some tests that are particularly resource-intensive.
11091114
test: @DEF_MAKE_RULE@ platform
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add --cleanup option to python3 -m test to remove ``test_python_*``
2+
directories of previous failed jobs. Add "make cleantest" to run
3+
``python3 -m test --cleanup``.
4+

0 commit comments

Comments
 (0)