8000 bpo-37359: Add --cleanup option to python3 -m test by vstinner · Pull Request #14332 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37359: Add --cleanup option to 8000 python3 -m test #14332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/test/libregrtest/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,10 @@ def _create_parser():
group.add_argument('--junit-xml', dest='xmlpath', metavar='FILENAME',
help='writes JUnit-style XML results to the specified '
'file')
group.add_argument('--tempdir', dest='tempdir', metavar='PATH',
group.add_argument('--tempdir', metavar='PATH',
help='override the working directory for the test run')
group.add_argument('--cleanup', action='store_true',
help='remove old test_python_* directories')
return parser


Expand Down
51 changes: 38 additions & 13 deletions Lib/test/libregrtest/main.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,19 @@ def parse_args(self, kwargs):
# Strip .py extensions.
removepy(ns.args)

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

if ns.tempdir:
ns.tempdir = os.path.expanduser(ns.tempdir)

self.ns = ns

def find_tests(self, tests):
self.tests = tests
Expand Down Expand Up @@ -537,7 +549,7 @@ def save_xml_result(self):
for s in ET.tostringlist(root):
f.write(s)

def create_temp_dir(self):
def set_temp_dir(self):
if self.ns.tempdir:
self.tmp_dir = self.ns.tempdir

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

self.tmp_dir = os.path.abspath(self.tmp_dir)

def create_temp_dir(self):
os.makedirs(self.tmp_dir, exist_ok=True)

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

def cleanup(self):
import glob
import shutil

path = os.path.join(self.tmp_dir, 'test_python_*')
print("Cleanup %s directory" % self.tmp_dir)
for name in glob.glob(path):
print("Remove directory: %s" % name)
if os.path.isdir(name):
support.rmtree(name)
else:
print("Remove file: %s" % name)
support.unlink(name)

def main(self, tests=None, **kwargs):
self.ns = self.parse_args(kwargs)
self.parse_args(kwargs)

self.set_temp_dir()

if self.ns.cleanup:
self.cleanup()
sys.exit(0)

test_cwd = self.create_temp_dir()

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

def _main(self, tests, kwargs):
if self.ns.huntrleaks:
warmup, repetitions, _ = self.ns.huntrleaks
if warmup < 1 or repetitions < 1:
msg = ("Invalid values for the --huntrleaks/-R parameters. The "
"number of warmups and repetitions must be at least 1 "
"each (1:1).")
print(msg, file=sys.stderr, flush=True)
sys.exit(2)

if self.worker_test_name is not None:
from test.libregrtest.runtest_mp import run_tests_worker
run_tests_worker(self.ns, self.worker_test_name)
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,21 @@ def test_unraisable_exc(self):
fail_env_changed=True)
self.assertIn("Warning -- Unraisable exception", output)

def test_cleanup(self):
dirname = os.path.join(self.tmptestdir, "test_python_123")
os.mkdir(dirname)
filename = os.path.join(self.tmptestdir, "test_python_456")
open(filename, "wb").close()
names = [dirname, filename]

cmdargs = ['-m', 'test',
'--tempdir=%s' % self.tmptestdir,
'--cleanup']
self.run_python(cmdargs)

for name in names:
self.assertFalse(os.path.exists(name), name)


class TestUtils(unittest.TestCase):
def test_format_duration(self):
Expand Down
5 changes: 5 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,11 @@ TESTTIMEOUT= 1200

.PHONY: test testall testuniversal buildbottest pythoninfo

# Remove "test_python_*" directories of previous failed test jobs.
# Pass TESTOPTS options because it can contain --tempdir option.
cleantest: build_all
$(TESTRUNNER) $(TESTOPTS) --cleanup

# Run a basic set of regression tests.
# This excludes some tests that are particularly resource-intensive.
test: @DEF_MAKE_RULE@ platform
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add --cleanup option to python3 -m test to remove ``test_python_*``
directories of previous failed jobs. Add "make cleantest" to run
``python3 -m test --cleanup``.

0