1996-12-10 15:51:10 -04:00
|
|
|
|
"""
|
|
|
|
|
Automatic Python regression test.
|
|
|
|
|
|
|
|
|
|
The list of individual tests is contained in the `testall' module.
|
|
|
|
|
These test some (but not all) essential parts of the Python
|
|
|
|
|
interpreter and built-in modules. When a test fails, an exception is
|
|
|
|
|
raised and testing halts. When a test succeeds, it can produce quite
|
|
|
|
|
a lot of output, which is compared against the output from a previous
|
|
|
|
|
run. If a difference is noticed it raises an exception; if all is
|
|
|
|
|
well, it prints nothing except 'All tests OK.' at the very end.
|
|
|
|
|
|
|
|
|
|
The output from a previous run is supposed to be contained in separate
|
1996-12-10 19:19:14 -04:00
|
|
|
|
files (one per test) in the `output' subdirectory somewhere on the
|
1996-12-10 15:51:10 -04:00
|
|
|
|
search path for modules (sys.path, initialized from $PYTHONPATH plus
|
|
|
|
|
some default places).
|
|
|
|
|
|
|
|
|
|
Of course, if the normal output of the tests is changed because the
|
|
|
|
|
tests have been changed (rather than a test producing the wrong
|
|
|
|
|
output), 'autotest' will fail as well. In this case, run 'autotest'
|
|
|
|
|
with the -g option.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
|
|
%s [-g] [-w] [-h] [test1 [test2 ...]]
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
|
|
-g, --generate : generate the output files instead of verifying
|
|
|
|
|
the results
|
|
|
|
|
|
|
|
|
|
-w, --warn : warn about un-importable tests
|
|
|
|
|
|
|
|
|
|
-h, --help : print this message
|
|
|
|
|
|
|
|
|
|
If individual tests are provided on the command line, only those tests
|
|
|
|
|
will be performed or generated. Otherwise, all tests (as contained in
|
|
|
|
|
testall.py) will be performed.
|
|
|
|
|
|
|
|
|
|
"""
|
1992-11-27 18:53:50 -04:00
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
1996-12-10 15:51:10 -04:00
|
|
|
|
import getopt
|
|
|
|
|
import traceback
|
1996-12-19 23:03:01 -04:00
|
|
|
|
import test_support
|
|
|
|
|
test_support.verbose = 0
|
1996-12-10 15:51:10 -04:00
|
|
|
|
from test_support import *
|
|
|
|
|
|
|
|
|
|
# Exception raised when the test failed (not the same as in test_support)
|
|
|
|
|
TestFailed = 'autotest.TestFailed'
|
1996-12-12 18:34:26 -04:00
|
|
|
|
TestMissing = 'autotest.TestMissing'
|
1996-12-10 15:51:10 -04:00
|
|
|
|
|
|
|
|
|
# defaults
|
|
|
|
|
generate = 0
|
|
|
|
|
warn = 0
|
1992-11-27 18:53:50 -04:00
|
|
|
|
|
1996-12-10 15:51:10 -04:00
|
|
|
|
|
|
|
|
|
|
1992-11-27 18:53:50 -04:00
|
|
|
|
# Function to find a file somewhere on sys.path
|
|
|
|
|
def findfile(filename):
|
|
|
|
|
for dirname in sys.path:
|
|
|
|
|
fullname = os.path.join(dirname, filename)
|
|
|
|
|
if os.path.exists(fullname):
|
|
|
|
|
return fullname
|
|
|
|
|
return filename # Will cause exception later
|
|
|
|
|
|
|
|
|
|
|
1996-12-10 15:51:10 -04:00
|
|
|
|
|
1992-11-27 18:53:50 -04:00
|
|
|
|
# Class substituted for sys.stdout, to compare it with the given file
|
|
|
|
|
class Compare:
|
1993-12-17 11:25:27 -04:00
|
|
|
|
def __init__(self, filename):
|
1992-11-27 18:53:50 -04:00
|
|
|
|
self.fp = open(filename, 'r')
|
|
|
|
|
def write(self, data):
|
|
|
|
|
expected = self.fp.read(len(data))
|
|
|
|
|
if data <> expected:
|
|
|
|
|
raise TestFailed, \
|
|
|
|
|
'Writing: '+`data`+', expected: '+`expected`
|
|
|
|
|
def close(self):
|
1996-12-10 15:51:10 -04:00
|
|
|
|
leftover = self.fp.read()
|
|
|
|
|
if leftover:
|
|
|
|
|
raise TestFailed, 'Unread: '+`leftover`
|
1992-11-27 18:53:50 -04:00
|
|
|
|
self.fp.close()
|
|
|
|
|
|
1996-12-10 15:51:10 -04:00
|
|
|
|
|
1992-11-27 18:53:50 -04:00
|
|
|
|
# The main program
|
1996-12-10 15:51:10 -04:00
|
|
|
|
def usage(status):
|
|
|
|
|
print __doc__ % sys.argv[0]
|
|
|
|
|
sys.exit(status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_one_test(t, outdir):
|
|
|
|
|
filename = os.path.join(outdir, t)
|
1992-11-27 18:53:50 -04:00
|
|
|
|
real_stdout = sys.stdout
|
1996-12-11 12:54:54 -04:00
|
|
|
|
if generate:
|
|
|
|
|
print 'Generating:', filename
|
|
|
|
|
fake_stdout = open(filename, 'w')
|
|
|
|
|
else:
|
1996-12-12 18:21:10 -04:00
|
|
|
|
try:
|
|
|
|
|
fake_stdout = Compare(filename)
|
|
|
|
|
except IOError:
|
1996-12-12 18:34:26 -04:00
|
|
|
|
raise TestMissing
|
1992-11-27 18:53:50 -04:00
|
|
|
|
try:
|
1996-12-11 12:54:54 -04:00
|
|
|
|
sys.stdout = fake_stdout
|
1996-12-10 15:51:10 -04:00
|
|
|
|
print t
|
|
|
|
|
unload(t)
|
|
|
|
|
try:
|
|
|
|
|
__import__(t, globals(), locals())
|
|
|
|
|
except ImportError, msg:
|
|
|
|
|
if warn:
|
|
|
|
|
sys.stderr.write(msg+': Un-installed'
|
|
|
|
|
' optional module?\n')
|
1996-12-18 12:39:31 -04:00
|
|
|
|
try:
|
|
|
|
|
fake_stdout.close()
|
|
|
|
|
except TestFailed:
|
|
|
|
|
pass
|
|
|
|
|
fake_stdout = None
|
1992-11-27 18:53:50 -04:00
|
|
|
|
finally:
|
|
|
|
|
sys.stdout = real_stdout
|
1996-12-18 12:39:31 -04:00
|
|
|
|
if fake_stdout:
|
|
|
|
|
fake_stdout.close()
|
1996-12-10 15:51:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
global generate
|
|
|
|
|
global warn
|
|
|
|
|
try:
|
|
|
|
|
opts, args = getopt.getopt(
|
|
|
|
|
sys.argv[1:], 'ghw',
|
|
|
|
|
['generate', 'help', 'warn'])
|
|
|
|
|
except getopt.error, msg:
|
|
|
|
|
print msg
|
|
|
|
|
usage(1)
|
|
|
|
|
for opt, val in opts:
|
|
|
|
|
if opt in ['-h', '--help']:
|
|
|
|
|
usage(0)
|
|
|
|
|
elif opt in ['-g', '--generate']:
|
|
|
|
|
generate = 1
|
|
|
|
|
elif opt in ['-w', '--warn']:
|
|
|
|
|
warn = 1
|
|
|
|
|
|
|
|
|
|
# find the output directory
|
1996-12-10 19:19:14 -04:00
|
|
|
|
outdir = findfile('output')
|
1996-12-10 15:51:10 -04:00
|
|
|
|
if args:
|
|
|
|
|
tests = args
|
|
|
|
|
else:
|
|
|
|
|
import testall
|
|
|
|
|
tests = testall.tests
|
1996-12-18 12:39:31 -04:00
|
|
|
|
failed = 0
|
1996-12-10 15:51:10 -04:00
|
|
|
|
for test in tests:
|
1996-12-18 12:39:31 -04:00
|
|
|
|
print 'testing:', test
|
1996-12-10 15:51:10 -04:00
|
|
|
|
try:
|
|
|
|
|
do_one_test(test, outdir)
|
|
|
|
|
except TestFailed, msg:
|
1996-12-18 12:39:31 -04:00
|
|
|
|
print 'test', test, 'failed'
|
|
|
|
|
failed = failed + 1
|
|
|
|
|
if not failed:
|
1996-12-12 18:34:26 -04:00
|
|
|
|
print 'All tests OK.'
|
1996-12-18 12:39:31 -04:00
|
|
|
|
else:
|
|
|
|
|
print failed, 'tests failed'
|
1996-12-12 18:34:26 -04:00
|
|
|
|
|
1992-11-27 18:53:50 -04:00
|
|
|
|
main()
|