Skip to content

Commit 9d55bf4

Browse files
authored
bpo-37359: Add --cleanup option to python3 -m test (GH-14332) (GH-14333)
* regrtest: Add --cleanup option to remove "test_python_*" directories of previous failed test jobs. * Add "make cleantest" to run "python -m test --cleanup". (cherry picked from commit 47fbc4e)
1 parent 0346448 commit 9d55bf4

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Lib/test/regrtest.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
375375
'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
376376
'multiprocess=', 'slaveargs=', 'forever', 'header', 'pgo',
377377
'failfast', 'match=', 'testdir=', 'list-tests', 'list-cases',
378-
'coverage', 'matchfile=', 'fail-env-changed'])
378+
'coverage', 'matchfile=', 'fail-env-changed', 'cleanup'])
379379
except getopt.error, msg:
380380
usage(2, msg)
381381

@@ -388,6 +388,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
388388
list_tests = False
389389
list_cases_opt = False
390390
fail_env_changed = False
391+
cleanup_tests = False
391392
for o, a in opts:
392393
if o in ('-h', '--help'):
393394
usage(0)
@@ -490,6 +491,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
490491
list_cases_opt = True
491492
elif o == '--fail-env-changed':
492493
fail_env_changed = True
494+
elif o == '--cleanup':
495+
cleanup_tests = True
493496
else:
494497
print >>sys.stderr, ("No handler for option {}. Please "
495498
"report this as a bug at http://bugs.python.org.").format(o)
@@ -527,6 +530,22 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
527530
print >>sys.stderr, msg
528531
sys.exit(2)
529532

533+
if cleanup_tests:
534+
import glob
535+
536+
os.chdir(support.SAVEDCWD)
537+
path = os.path.join(TEMPDIR, 'test_python_*')
538+
print("Cleanup %s directory" % TEMPDIR)
539+
for name in glob.glob(path):
540+
if os.path.isdir(name):
541+
print("Remove directory: %s" % name)
542+
support.rmtree(name)
543+
else:
544+
print("Remove file: %s" % name)
545+
support.unlink(name)
546+
sys.exit(0)
547+
548+
530549
if slaveargs is not None:
531550
args, kwargs = json.loads(slaveargs)
532551
if testdir:

Makefile.pre.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,12 @@ $(LIBRARY_OBJS) $(MODOBJS) Modules/python.o: $(PYTHON_HEADERS)
855855
TESTOPTS= -l $(EXTRATESTOPTS)
856856
TESTPROG= $(srcdir)/Lib/test/regrtest.py
857857
TESTPYTHON= $(RUNSHARED) ./$(BUILDPYTHON) -Wd -3 -E -tt $(TESTPYTHONOPTS)
858+
859+
# Remove "test_python_*" directories of previous failed test jobs.
860+
# Pass TESTOPTS options because it can contain --tempdir option.
861+
cleantest: build_all
862+
$(TESTPYTHON) $(TESTPROG) $(TESTOPTS) --cleanup
863+
858864
test: @DEF_MAKE_RULE@ platform
859865
-find $(srcdir)/Lib -name '*.py[co]' -print | xargs rm -f
860866
-$(TESTPYTHON) $(TESTPROG) $(TESTOPTS)
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)