[Python-checkins] r46799 - python/trunk/Lib/test/regrtest.py

tim.peters python-checkins at python.org
Fri Jun 9 21:24:45 CEST 2006


Author: tim.peters
Date: Fri Jun  9 21:24:44 2006
New Revision: 46799

Modified:
   python/trunk/Lib/test/regrtest.py
Log:
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.


Modified: python/trunk/Lib/test/regrtest.py
==============================================================================
--- python/trunk/Lib/test/regrtest.py	(original)
+++ python/trunk/Lib/test/regrtest.py	Fri Jun  9 21:24:44 2006
@@ -496,14 +496,30 @@
 
 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 @@
         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


More information about the Python-checkins mailing list