1996-12-19 23:12:20 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
"""Regression test.
|
|
|
|
|
|
|
|
This will find all modules whose name is "test_*" in the test
|
|
|
|
directory, and run them. Various command line options provide
|
|
|
|
additional facilities.
|
|
|
|
|
|
|
|
Command line options:
|
|
|
|
|
2000-08-03 12:50:37 -03:00
|
|
|
-v: verbose -- run tests in verbose mode with output to stdout
|
|
|
|
-q: quiet -- don't print anything except if a test fails
|
|
|
|
-g: generate -- write the output file for a test instead of comparing it
|
|
|
|
-x: exclude -- arguments are tests to *exclude*
|
|
|
|
-s: single -- run only a single test (see below)
|
|
|
|
-r: random -- randomize test execution order
|
2000-10-12 22:32:42 -03:00
|
|
|
-l: findleaks -- if GC is available detect tests that leak memory
|
2000-08-11 16:02:59 -03:00
|
|
|
--have-resources -- run tests that require large resources (time/space)
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
If non-option arguments are present, they are names for tests to run,
|
|
|
|
unless -x is given, in which case they are names for tests not to run.
|
|
|
|
If no test names are given, all tests are run.
|
1997-03-07 17:04:33 -04:00
|
|
|
|
1997-08-18 17:08:24 -03:00
|
|
|
-v is incompatible with -g and does not compare test output files.
|
1999-01-28 15:51:51 -04:00
|
|
|
|
|
|
|
-s means to run only a single test and exit. This is useful when Purifying
|
|
|
|
the Python interpreter. The file /tmp/pynexttest is read to find the next
|
|
|
|
test to run. If this file is missing, the first test_*.py file in testdir or
|
|
|
|
on the command line is used. (actually tempfile.gettempdir() is used instead
|
|
|
|
of /tmp).
|
|
|
|
|
1996-12-19 23:12:20 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
import os
|
|
|
|
import getopt
|
1997-07-15 22:56:13 -03:00
|
|
|
import traceback
|
2000-06-30 13:39:27 -03:00
|
|
|
import random
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
import test_support
|
|
|
|
|
2000-06-30 13:39:27 -03:00
|
|
|
def main(tests=None, testdir=None, verbose=0, quiet=0, generate=0,
|
2000-09-22 12:29:28 -03:00
|
|
|
exclude=0, single=0, randomize=0, findleaks=0,
|
2000-08-11 16:02:59 -03:00
|
|
|
use_large_resources=0):
|
1998-08-01 14:04:08 -03:00
|
|
|
"""Execute a test suite.
|
|
|
|
|
2000-07-16 09:04:32 -03:00
|
|
|
This also parses command-line options and modifies its behavior
|
2000-10-23 14:22:08 -03:00
|
|
|
accordingly.
|
1998-08-01 14:04:08 -03:00
|
|
|
|
|
|
|
tests -- a list of strings containing test names (optional)
|
|
|
|
testdir -- the directory in which to look for tests (optional)
|
|
|
|
|
|
|
|
Users other than the Python test suite will certainly want to
|
|
|
|
specify testdir; if it's omitted, the directory containing the
|
2000-10-23 14:22:08 -03:00
|
|
|
Python test suite is searched for.
|
1998-08-01 14:04:08 -03:00
|
|
|
|
|
|
|
If the tests argument is omitted, the tests listed on the
|
|
|
|
command-line will be used. If that's empty, too, then all *.py
|
|
|
|
files beginning with test_ will be used.
|
2000-06-30 13:39:27 -03:00
|
|
|
|
2000-08-03 12:50:37 -03:00
|
|
|
The other seven default arguments (verbose, quiet, generate, exclude,
|
2000-09-22 12:29:28 -03:00
|
|
|
single, randomize, and findleaks) allow programmers calling main()
|
2000-08-03 12:50:37 -03:00
|
|
|
directly to set the values that would normally be set by flags on the
|
|
|
|
command line.
|
|
|
|
|
1998-08-01 14:04:08 -03:00
|
|
|
"""
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-19 23:12:20 -04:00
|
|
|
try:
|
2000-08-11 16:02:59 -03:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'vgqxsrl', ['have-resources'])
|
1996-12-19 23:12:20 -04:00
|
|
|
except getopt.error, msg:
|
1998-03-26 15:42:58 -04:00
|
|
|
print msg
|
|
|
|
print __doc__
|
|
|
|
return 2
|
1996-12-19 23:12:20 -04:00
|
|
|
for o, a in opts:
|
1998-03-26 15:42:58 -04:00
|
|
|
if o == '-v': verbose = verbose+1
|
|
|
|
if o == '-q': quiet = 1; verbose = 0
|
|
|
|
if o == '-g': generate = 1
|
|
|
|
if o == '-x': exclude = 1
|
1999-01-28 15:51:51 -04:00
|
|
|
if o == '-s': single = 1
|
2000-06-30 13:39:27 -03:00
|
|
|
if o == '-r': randomize = 1
|
2000-09-22 12:29:28 -03:00
|
|
|
if o == '-l': findleaks = 1
|
2000-08-11 16:02:59 -03:00
|
|
|
if o == '--have-resources': use_large_resources = 1
|
1997-08-18 17:08:24 -03:00
|
|
|
if generate and verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print "-g and -v don't go together!"
|
|
|
|
return 2
|
1996-12-19 23:12:20 -04:00
|
|
|
good = []
|
|
|
|
bad = []
|
|
|
|
skipped = []
|
1999-01-28 15:51:51 -04:00
|
|
|
|
2000-09-22 12:29:28 -03:00
|
|
|
if findleaks:
|
2000-08-03 12:50:37 -03:00
|
|
|
try:
|
|
|
|
import gc
|
|
|
|
except ImportError:
|
2000-10-12 22:32:42 -03:00
|
|
|
print 'No GC available, disabling findleaks.'
|
2000-09-22 12:29:28 -03:00
|
|
|
findleaks = 0
|
2000-08-03 12:50:37 -03:00
|
|
|
else:
|
2000-10-12 22:32:42 -03:00
|
|
|
# Uncomment the line below to report garbage that is not
|
|
|
|
# freeable by reference counting alone. By default only
|
|
|
|
# garbage that is not collectable by the GC is reported.
|
|
|
|
#gc.set_debug(gc.DEBUG_SAVEALL)
|
2000-09-22 12:29:28 -03:00
|
|
|
found_garbage = []
|
2000-08-03 12:50:37 -03:00
|
|
|
|
1999-01-28 15:51:51 -04:00
|
|
|
if single:
|
|
|
|
from tempfile import gettempdir
|
|
|
|
filename = os.path.join(gettempdir(), 'pynexttest')
|
|
|
|
try:
|
|
|
|
fp = open(filename, 'r')
|
|
|
|
next = string.strip(fp.read())
|
|
|
|
tests = [next]
|
|
|
|
fp.close()
|
|
|
|
except IOError:
|
|
|
|
pass
|
1997-08-18 17:08:24 -03:00
|
|
|
for i in range(len(args)):
|
1998-03-26 15:42:58 -04:00
|
|
|
# Strip trailing ".py" from arguments
|
|
|
|
if args[i][-3:] == '.py':
|
|
|
|
args[i] = args[i][:-3]
|
1998-08-25 09:29:08 -03:00
|
|
|
stdtests = STDTESTS[:]
|
|
|
|
nottests = NOTTESTS[:]
|
1996-12-19 23:12:20 -04:00
|
|
|
if exclude:
|
1998-08-25 09:29:08 -03:00
|
|
|
for arg in args:
|
|
|
|
if arg in stdtests:
|
|
|
|
stdtests.remove(arg)
|
|
|
|
nottests[:0] = args
|
1998-03-26 15:42:58 -04:00
|
|
|
args = []
|
1998-08-24 10:48:36 -03:00
|
|
|
tests = tests or args or findtests(testdir, stdtests, nottests)
|
1999-01-28 15:51:51 -04:00
|
|
|
if single:
|
|
|
|
tests = tests[:1]
|
2000-06-30 13:39:27 -03:00
|
|
|
if randomize:
|
|
|
|
random.shuffle(tests)
|
1998-03-26 15:42:58 -04:00
|
|
|
test_support.verbose = verbose # Tell tests to be moderately quiet
|
2000-08-11 16:02:59 -03:00
|
|
|
test_support.use_large_resources = use_large_resources
|
2000-04-21 18:35:06 -03:00
|
|
|
save_modules = sys.modules.keys()
|
1996-12-19 23:12:20 -04:00
|
|
|
for test in tests:
|
1998-03-26 15:42:58 -04:00
|
|
|
if not quiet:
|
|
|
|
print test
|
2000-08-11 16:02:59 -03:00
|
|
|
ok = runtest(test, generate, verbose, quiet, testdir)
|
1998-03-26 15:42:58 -04:00
|
|
|
if ok > 0:
|
|
|
|
good.append(test)
|
|
|
|
elif ok == 0:
|
|
|
|
bad.append(test)
|
|
|
|
else:
|
|
|
|
skipped.append(test)
|
2000-09-22 12:29:28 -03:00
|
|
|
if findleaks:
|
|
|
|
gc.collect()
|
|
|
|
if gc.garbage:
|
2000-10-12 22:32:42 -03:00
|
|
|
print "Warning: test created", len(gc.garbage),
|
|
|
|
print "uncollectable object(s)."
|
|
|
|
# move the uncollectable objects somewhere so we don't see
|
|
|
|
# them again
|
2000-09-22 12:29:28 -03:00
|
|
|
found_garbage.extend(gc.garbage)
|
|
|
|
del gc.garbage[:]
|
2000-04-21 18:35:06 -03:00
|
|
|
# Unload the newly imported modules (best effort finalization)
|
|
|
|
for module in sys.modules.keys():
|
2000-05-05 11:27:39 -03:00
|
|
|
if module not in save_modules and module.startswith("test."):
|
2000-04-21 18:35:06 -03:00
|
|
|
test_support.unload(module)
|
1996-12-19 23:12:20 -04:00
|
|
|
if good and not quiet:
|
1998-03-26 15:42:58 -04:00
|
|
|
if not bad and not skipped and len(good) > 1:
|
|
|
|
print "All",
|
|
|
|
print count(len(good), "test"), "OK."
|
2000-12-30 18:21:22 -04:00
|
|
|
if verbose:
|
|
|
|
print "CAUTION: stdout isn't compared in verbose mode: a test"
|
|
|
|
print "that passes in verbose mode may fail without it."
|
1996-12-19 23:12:20 -04:00
|
|
|
if bad:
|
1998-03-26 15:42:58 -04:00
|
|
|
print count(len(bad), "test"), "failed:",
|
|
|
|
print string.join(bad)
|
1996-12-19 23:12:20 -04:00
|
|
|
if skipped and not quiet:
|
1998-03-26 15:42:58 -04:00
|
|
|
print count(len(skipped), "test"), "skipped:",
|
|
|
|
print string.join(skipped)
|
1999-01-28 15:51:51 -04:00
|
|
|
|
|
|
|
if single:
|
|
|
|
alltests = findtests(testdir, stdtests, nottests)
|
|
|
|
for i in range(len(alltests)):
|
|
|
|
if tests[0] == alltests[i]:
|
|
|
|
if i == len(alltests) - 1:
|
|
|
|
os.unlink(filename)
|
|
|
|
else:
|
|
|
|
fp = open(filename, 'w')
|
|
|
|
fp.write(alltests[i+1] + '\n')
|
|
|
|
fp.close()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
os.unlink(filename)
|
|
|
|
|
1997-08-14 16:40:34 -03:00
|
|
|
return len(bad) > 0
|
1996-12-19 23:12:20 -04:00
|
|
|
|
1998-08-01 14:04:08 -03:00
|
|
|
STDTESTS = [
|
1996-12-19 23:12:20 -04:00
|
|
|
'test_grammar',
|
|
|
|
'test_opcodes',
|
|
|
|
'test_operations',
|
|
|
|
'test_builtin',
|
|
|
|
'test_exceptions',
|
|
|
|
'test_types',
|
|
|
|
]
|
|
|
|
|
1998-08-01 14:04:08 -03:00
|
|
|
NOTTESTS = [
|
1996-12-19 23:12:20 -04:00
|
|
|
'test_support',
|
|
|
|
'test_b1',
|
|
|
|
'test_b2',
|
|
|
|
]
|
|
|
|
|
1998-08-01 14:04:08 -03:00
|
|
|
def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
|
1996-12-19 23:12:20 -04:00
|
|
|
"""Return a list of all applicable test modules."""
|
1998-08-01 14:04:08 -03:00
|
|
|
if not testdir: testdir = findtestdir()
|
1996-12-19 23:12:20 -04:00
|
|
|
names = os.listdir(testdir)
|
|
|
|
tests = []
|
|
|
|
for name in names:
|
1998-03-26 15:42:58 -04:00
|
|
|
if name[:5] == "test_" and name[-3:] == ".py":
|
|
|
|
modname = name[:-3]
|
|
|
|
if modname not in stdtests and modname not in nottests:
|
|
|
|
tests.append(modname)
|
1996-12-19 23:12:20 -04:00
|
|
|
tests.sort()
|
|
|
|
return stdtests + tests
|
|
|
|
|
2000-08-11 16:02:59 -03:00
|
|
|
def runtest(test, generate, verbose, quiet, testdir = None):
|
1998-08-01 14:04:08 -03:00
|
|
|
"""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
|
|
|
|
verbose -- if true, print more messages
|
2000-08-11 16:02:59 -03:00
|
|
|
quiet -- if true, don't print 'skipped' messages (probably redundant)
|
1998-08-01 14:04:08 -03:00
|
|
|
testdir -- test directory
|
|
|
|
"""
|
1996-12-19 23:12:20 -04:00
|
|
|
test_support.unload(test)
|
1998-08-01 14:04:08 -03:00
|
|
|
if not testdir: testdir = findtestdir()
|
1996-12-19 23:12:20 -04:00
|
|
|
outputdir = os.path.join(testdir, "output")
|
|
|
|
outputfile = os.path.join(outputdir, test)
|
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
if generate:
|
|
|
|
cfp = open(outputfile, "w")
|
|
|
|
elif verbose:
|
|
|
|
cfp = sys.stdout
|
|
|
|
else:
|
|
|
|
cfp = Compare(outputfile)
|
1996-12-19 23:12:20 -04:00
|
|
|
except IOError:
|
1998-03-26 15:42:58 -04:00
|
|
|
cfp = None
|
|
|
|
print "Warning: can't open", outputfile
|
1996-12-19 23:12:20 -04:00
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
save_stdout = sys.stdout
|
|
|
|
try:
|
|
|
|
if cfp:
|
|
|
|
sys.stdout = cfp
|
|
|
|
print test # Output file starts with test name
|
|
|
|
__import__(test, globals(), locals(), [])
|
2000-07-11 12:15:31 -03:00
|
|
|
if cfp and not (generate or verbose):
|
|
|
|
cfp.close()
|
1998-03-26 15:42:58 -04:00
|
|
|
finally:
|
|
|
|
sys.stdout = save_stdout
|
2000-08-04 10:17:51 -03:00
|
|
|
except (ImportError, test_support.TestSkipped), msg:
|
2000-08-11 16:02:59 -03:00
|
|
|
if not quiet:
|
|
|
|
print "test", test,
|
|
|
|
print "skipped -- ", msg
|
1998-03-26 15:42:58 -04:00
|
|
|
return -1
|
2000-08-18 13:04:05 -03:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
1996-12-19 23:12:20 -04:00
|
|
|
except test_support.TestFailed, msg:
|
1998-03-26 15:42:58 -04:00
|
|
|
print "test", test, "failed --", msg
|
|
|
|
return 0
|
1997-07-15 22:56:13 -03:00
|
|
|
except:
|
1998-03-26 15:42:58 -04:00
|
|
|
type, value = sys.exc_info()[:2]
|
2000-08-23 17:34:40 -03:00
|
|
|
print "test", test, "crashed --", str(type) + ":", value
|
1998-03-26 15:42:58 -04:00
|
|
|
if verbose:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
return 0
|
1996-12-19 23:12:20 -04:00
|
|
|
else:
|
1998-03-26 15:42:58 -04:00
|
|
|
return 1
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
def findtestdir():
|
|
|
|
if __name__ == '__main__':
|
1998-03-26 15:42:58 -04:00
|
|
|
file = sys.argv[0]
|
1996-12-19 23:12:20 -04:00
|
|
|
else:
|
1998-03-26 15:42:58 -04:00
|
|
|
file = __file__
|
1996-12-19 23:12:20 -04:00
|
|
|
testdir = os.path.dirname(file) or os.curdir
|
|
|
|
return testdir
|
|
|
|
|
|
|
|
def count(n, word):
|
|
|
|
if n == 1:
|
1998-03-26 15:42:58 -04:00
|
|
|
return "%d %s" % (n, word)
|
1996-12-19 23:12:20 -04:00
|
|
|
else:
|
1998-03-26 15:42:58 -04:00
|
|
|
return "%d %ss" % (n, word)
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
class Compare:
|
|
|
|
|
|
|
|
def __init__(self, filename):
|
1998-03-26 15:42:58 -04:00
|
|
|
self.fp = open(filename, 'r')
|
2000-12-30 18:21:22 -04:00
|
|
|
self.stuffthatmatched = []
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
def write(self, data):
|
1998-03-26 15:42:58 -04:00
|
|
|
expected = self.fp.read(len(data))
|
2000-12-30 18:21:22 -04:00
|
|
|
if data == expected:
|
|
|
|
self.stuffthatmatched.append(expected)
|
|
|
|
else:
|
|
|
|
# This Compare instance is spoofing stdout, so we need to write
|
|
|
|
# to stderr instead.
|
|
|
|
from sys import stderr as e
|
|
|
|
print >> e, "The actual stdout doesn't match the expected stdout."
|
|
|
|
if self.stuffthatmatched:
|
|
|
|
print >> e, "This much did match (between asterisk lines):"
|
|
|
|
print >> e, "*" * 70
|
|
|
|
good = "".join(self.stuffthatmatched)
|
|
|
|
e.write(good)
|
|
|
|
if not good.endswith("\n"):
|
|
|
|
e.write("\n")
|
|
|
|
print >> e, "*" * 70
|
|
|
|
print >> e, "Then ..."
|
|
|
|
else:
|
|
|
|
print >> e, "The first write to stdout clashed:"
|
|
|
|
# Note that the prompts are the same length in next two lines.
|
|
|
|
# This is so what we expected and what we got line up.
|
|
|
|
print >> e, "We expected (repr):", `expected`
|
|
|
|
print >> e, "But instead we got:", `data`
|
|
|
|
raise test_support.TestFailed('Writing: ' + `data`+
|
|
|
|
', expected: ' + `expected`)
|
1996-12-19 23:12:20 -04:00
|
|
|
|
1998-04-23 10:33:21 -03:00
|
|
|
def writelines(self, listoflines):
|
|
|
|
map(self.write, listoflines)
|
|
|
|
|
1997-07-17 11:51:37 -03:00
|
|
|
def flush(self):
|
1998-03-26 15:42:58 -04:00
|
|
|
pass
|
1997-07-17 11:51:37 -03:00
|
|
|
|
1996-12-19 23:12:20 -04:00
|
|
|
def close(self):
|
1998-03-26 15:42:58 -04:00
|
|
|
leftover = self.fp.read()
|
|
|
|
if leftover:
|
2000-12-30 18:21:22 -04:00
|
|
|
raise test_support.TestFailed('Tail of expected stdout unseen: ' +
|
|
|
|
`leftover`)
|
1998-03-26 15:42:58 -04:00
|
|
|
self.fp.close()
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
def isatty(self):
|
1998-03-26 15:42:58 -04:00
|
|
|
return 0
|
1996-12-19 23:12:20 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
1997-08-14 16:40:34 -03:00
|
|
|
sys.exit(main())
|