From e55848695347c67effeb08aedcf2ce3d256524cd Mon Sep 17 00:00:00 2001 From: Tim Peters Date: Fri, 9 Jun 2006 19:24:44 +0000 Subject: [PATCH] Implementing a happy idea from Georg Brandl: make runtest() try to clean up files and directories the tests often leave behind by mistake. This is the first time in history I don't have a bogus "db_home" directory after running the tests ;-) Also worked on runtest's docstring, to say something about all the arguments, and to document the non-obvious return values. New functions runtest_inner() and cleanup_test_droppings() in support of the above. --- Lib/test/regrtest.py | 49 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index ca4a3b515fa..023ad9a0265 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -496,14 +496,30 @@ def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS): def runtest(test, generate, verbose, quiet, testdir=None, huntrleaks=False): """Run a single test. + test -- the name of the test generate -- if true, generate output, instead of running the test - and comparing it to a previously created output file + and comparing it to a previously created output file verbose -- if true, print more messages quiet -- if true, don't print 'skipped' messages (probably redundant) testdir -- test directory + huntrleaks -- run multiple times to test for leaks; requires a debug + build; a triple corresponding to -R's three arguments + Return: + -2 test skipped because resource denied + -1 test skipped for some other reason + 0 test failed + 1 test passed """ + try: + return runtest_inner(test, generate, verbose, quiet, testdir, + huntrleaks) + finally: + cleanup_test_droppings(test, verbose) + +def runtest_inner(test, generate, verbose, quiet, + testdir=None, huntrleaks=False): test_support.unload(test) if not testdir: testdir = findtestdir() @@ -595,6 +611,37 @@ def runtest(test, generate, verbose, quiet, testdir=None, huntrleaks=False): sys.stdout.flush() return 0 +def cleanup_test_droppings(testname, verbose): + import shutil + + # Try to clean up junk commonly left behind. While tests shouldn't leave + # any files or directories behind, when a test fails that can be tedious + # for it to arrange. The consequences can be especially nasty on Windows, + # since if a test leaves a file open, it cannot be deleted by name (while + # there's nothing we can do about that here either, we can display the + # name of the offending test, which is a real help). + for name in (test_support.TESTFN, + "db_home", + ): + if not os.path.exists(name): + continue + + if os.path.isdir(name): + kind, nuker = "directory", shutil.rmtree + elif os.path.isfile(name): + kind, nuker = "file", os.unlink + else: + raise SystemError("os.path says %r exists but is neither " + "directory nor file" % name) + + if verbose: + print "%r left behind %s %r" % (testname, kind, name) + try: + nuker(name) + except Exception, msg: + print >> sys.stderr, ("%r left behind %s %r and it couldn't be " + "removed: %s" % (testname, kind, name, msg)) + def dash_R(the_module, test, indirect_test, huntrleaks): # This code is hackish and inelegant, but it seems to do the job. import copy_reg