blob: 94ae9c11377f3a1710469954b878469574eae92c [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
Barry Warsawe4a252e1996-12-10 23:19:14 +000013files (one per test) in the `output' subdirectory somewhere on the
Barry Warsaw272c00b1996-12-10 19:51:10 +000014search 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
Guido van Rossum0a94cc71996-12-20 03:03:01 +000045import test_support
46test_support.verbose = 0
Barry Warsaw272c00b1996-12-10 19:51:10 +000047from test_support import *
Guido van Rossum85f18201992-11-27 22:53:50 +000048
Barry Warsaw272c00b1996-12-10 19:51:10 +000049# Exception raised when the test failed (not the same as in test_support)
50TestFailed = 'autotest.TestFailed'
Barry Warsawcb17a461996-12-12 22:34:26 +000051TestMissing = 'autotest.TestMissing'
Barry Warsaw272c00b1996-12-10 19:51:10 +000052
53# defaults
54generate = 0
55warn = 0
56
57
58
Guido van Rossum85f18201992-11-27 22:53:50 +000059# Function to find a file somewhere on sys.path
60def findfile(filename):
61 for dirname in sys.path:
62 fullname = os.path.join(dirname, filename)
63 if os.path.exists(fullname):
64 return fullname
65 return filename # Will cause exception later
66
Guido van Rossum85f18201992-11-27 22:53:50 +000067
Barry Warsaw272c00b1996-12-10 19:51:10 +000068
Guido van Rossum85f18201992-11-27 22:53:50 +000069# Class substituted for sys.stdout, to compare it with the given file
70class Compare:
Guido van Rossum7bc817d1993-12-17 15:25:27 +000071 def __init__(self, filename):
Guido van Rossum85f18201992-11-27 22:53:50 +000072 self.fp = open(filename, 'r')
Guido van Rossum85f18201992-11-27 22:53:50 +000073 def write(self, data):
74 expected = self.fp.read(len(data))
75 if data <> expected:
76 raise TestFailed, \
77 'Writing: '+`data`+', expected: '+`expected`
78 def close(self):
Barry Warsaw272c00b1996-12-10 19:51:10 +000079 leftover = self.fp.read()
80 if leftover:
81 raise TestFailed, 'Unread: '+`leftover`
Guido van Rossum85f18201992-11-27 22:53:50 +000082 self.fp.close()
83
Barry Warsaw272c00b1996-12-10 19:51:10 +000084
Guido van Rossum85f18201992-11-27 22:53:50 +000085# The main program
Barry Warsaw272c00b1996-12-10 19:51:10 +000086def usage(status):
87 print __doc__ % sys.argv[0]
88 sys.exit(status)
89
90
91
92def do_one_test(t, outdir):
93 filename = os.path.join(outdir, t)
Guido van Rossum85f18201992-11-27 22:53:50 +000094 real_stdout = sys.stdout
Guido van Rossumdbfed711996-12-11 16:54:54 +000095 if generate:
96 print 'Generating:', filename
97 fake_stdout = open(filename, 'w')
98 else:
Barry Warsaw1c92eba1996-12-12 22:21:10 +000099 try:
100 fake_stdout = Compare(filename)
101 except IOError:
Barry Warsawcb17a461996-12-12 22:34:26 +0000102 raise TestMissing
Guido van Rossum85f18201992-11-27 22:53:50 +0000103 try:
Guido van Rossumdbfed711996-12-11 16:54:54 +0000104 sys.stdout = fake_stdout
Barry Warsaw272c00b1996-12-10 19:51:10 +0000105 print t
106 unload(t)
107 try:
108 __import__(t, globals(), locals())
109 except ImportError, msg:
110 if warn:
111 sys.stderr.write(msg+': Un-installed'
112 ' optional module?\n')
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000113 try:
114 fake_stdout.close()
115 except TestFailed:
116 pass
117 fake_stdout = None
Guido van Rossum85f18201992-11-27 22:53:50 +0000118 finally:
119 sys.stdout = real_stdout
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000120 if fake_stdout:
121 fake_stdout.close()
Barry Warsaw272c00b1996-12-10 19:51:10 +0000122
123
124
125def main():
126 global generate
127 global warn
128 try:
129 opts, args = getopt.getopt(
130 sys.argv[1:], 'ghw',
131 ['generate', 'help', 'warn'])
132 except getopt.error, msg:
133 print msg
134 usage(1)
135 for opt, val in opts:
136 if opt in ['-h', '--help']:
137 usage(0)
138 elif opt in ['-g', '--generate']:
139 generate = 1
140 elif opt in ['-w', '--warn']:
141 warn = 1
142
143 # find the output directory
Barry Warsawe4a252e1996-12-10 23:19:14 +0000144 outdir = findfile('output')
Barry Warsaw272c00b1996-12-10 19:51:10 +0000145 if args:
146 tests = args
147 else:
148 import testall
149 tests = testall.tests
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000150 failed = 0
Barry Warsaw272c00b1996-12-10 19:51:10 +0000151 for test in tests:
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000152 print 'testing:', test
Barry Warsaw272c00b1996-12-10 19:51:10 +0000153 try:
154 do_one_test(test, outdir)
155 except TestFailed, msg:
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000156 print 'test', test, 'failed'
157 failed = failed + 1
158 if not failed:
Barry Warsawcb17a461996-12-12 22:34:26 +0000159 print 'All tests OK.'
Barry Warsawaf82a7e1996-12-18 16:39:31 +0000160 else:
161 print failed, 'tests failed'
Barry Warsawcb17a461996-12-12 22:34:26 +0000162
Guido van Rossum85f18201992-11-27 22:53:50 +0000163main()