blob: 0d2fa8e15e0cd055d99396df3953f36b9953d4c0 [file] [log] [blame]
Barry Warsaw272c00b1996-12-10 19:51:10 +00001"""
2Automatic Python regression test.
3
4The list of individual tests is contained in the `testall' module.
5These test some (but not all) essential parts of the Python
6interpreter and built-in modules. When a test fails, an exception is
7raised and testing halts. When a test succeeds, it can produce quite
8a lot of output, which is compared against the output from a previous
9run. If a difference is noticed it raises an exception; if all is
10well, it prints nothing except 'All tests OK.' at the very end.
11
12The output from a previous run is supposed to be contained in separate
13files (one per test) in the `Output' subdirectory somewhere on the
14search path for modules (sys.path, initialized from $PYTHONPATH plus
15some default places).
16
17Of course, if the normal output of the tests is changed because the
18tests have been changed (rather than a test producing the wrong
19output), 'autotest' will fail as well. In this case, run 'autotest'
20with the -g option.
21
22Usage:
23
24 %s [-g] [-w] [-h] [test1 [test2 ...]]
25
26Options:
27
28 -g, --generate : generate the output files instead of verifying
29 the results
30
31 -w, --warn : warn about un-importable tests
32
33 -h, --help : print this message
34
35If individual tests are provided on the command line, only those tests
36will be performed or generated. Otherwise, all tests (as contained in
37testall.py) will be performed.
38
39"""
Guido van Rossum85f18201992-11-27 22:53:50 +000040
41import os
42import sys
Barry Warsaw272c00b1996-12-10 19:51:10 +000043import getopt
44import traceback
45from test_support import *
Guido van Rossum85f18201992-11-27 22:53:50 +000046
Barry Warsaw272c00b1996-12-10 19:51:10 +000047# Exception raised when the test failed (not the same as in test_support)
48TestFailed = 'autotest.TestFailed'
49
50# defaults
51generate = 0
52warn = 0
53
54
55
Guido van Rossum85f18201992-11-27 22:53:50 +000056# Function to find a file somewhere on sys.path
57def findfile(filename):
58 for dirname in sys.path:
59 fullname = os.path.join(dirname, filename)
60 if os.path.exists(fullname):
61 return fullname
62 return filename # Will cause exception later
63
Guido van Rossum85f18201992-11-27 22:53:50 +000064
Barry Warsaw272c00b1996-12-10 19:51:10 +000065
Guido van Rossum85f18201992-11-27 22:53:50 +000066# Class substituted for sys.stdout, to compare it with the given file
67class Compare:
Guido van Rossum7bc817d1993-12-17 15:25:27 +000068 def __init__(self, filename):
Guido van Rossum85f18201992-11-27 22:53:50 +000069 self.fp = open(filename, 'r')
Guido van Rossum85f18201992-11-27 22:53:50 +000070 def write(self, data):
71 expected = self.fp.read(len(data))
72 if data <> expected:
73 raise TestFailed, \
74 'Writing: '+`data`+', expected: '+`expected`
75 def close(self):
Barry Warsaw272c00b1996-12-10 19:51:10 +000076 leftover = self.fp.read()
77 if leftover:
78 raise TestFailed, 'Unread: '+`leftover`
Guido van Rossum85f18201992-11-27 22:53:50 +000079 self.fp.close()
80
Barry Warsaw272c00b1996-12-10 19:51:10 +000081
Guido van Rossum85f18201992-11-27 22:53:50 +000082# The main program
Barry Warsaw272c00b1996-12-10 19:51:10 +000083def usage(status):
84 print __doc__ % sys.argv[0]
85 sys.exit(status)
86
87
88
89def do_one_test(t, outdir):
90 filename = os.path.join(outdir, t)
Guido van Rossum85f18201992-11-27 22:53:50 +000091 real_stdout = sys.stdout
92 try:
Barry Warsaw272c00b1996-12-10 19:51:10 +000093 if generate:
94 print 'Generating:', filename
95 sys.stdout = open(filename, 'w')
96 else:
97 sys.stdout = Compare(filename)
98 print t
99 unload(t)
100 try:
101 __import__(t, globals(), locals())
102 except ImportError, msg:
103 if warn:
104 sys.stderr.write(msg+': Un-installed'
105 ' optional module?\n')
Guido van Rossum85f18201992-11-27 22:53:50 +0000106 finally:
Barry Warsaw272c00b1996-12-10 19:51:10 +0000107 sys.stdout.close()
Guido van Rossum85f18201992-11-27 22:53:50 +0000108 sys.stdout = real_stdout
Barry Warsaw272c00b1996-12-10 19:51:10 +0000109
110
111
112def main():
113 global generate
114 global warn
115 try:
116 opts, args = getopt.getopt(
117 sys.argv[1:], 'ghw',
118 ['generate', 'help', 'warn'])
119 except getopt.error, msg:
120 print msg
121 usage(1)
122 for opt, val in opts:
123 if opt in ['-h', '--help']:
124 usage(0)
125 elif opt in ['-g', '--generate']:
126 generate = 1
127 elif opt in ['-w', '--warn']:
128 warn = 1
129
130 # find the output directory
131 outdir = findfile('Output')
132 if args:
133 tests = args
134 else:
135 import testall
136 tests = testall.tests
137 for test in tests:
138 try:
139 do_one_test(test, outdir)
140 except TestFailed, msg:
141 print 'Failure of test:', test
142 traceback.print_exc()
Guido van Rossum85f18201992-11-27 22:53:50 +0000143 print 'All tests OK.'
144
145main()