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.
This commit is contained in:
Tim Peters 2006-06-09 19:24:44 +00:00
parent a04d118586
commit e558486953
1 changed files with 48 additions and 1 deletions

View File

@ -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