summaryrefslogtreecommitdiffstats
path: root/Lib/test/autotest.py
blob: 57e3674865bbad905d7e7eb56ed437ea485ff010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Automatic Python regression test.
#
# Some essential parts of the Python interpreter are tested by the module
# 'testall'.  (Despite its name, it doesn't test everything -- that would
# be a truly Herculean task!)  When a test fails, 'testall' raises an
# exception.  When all tests succeed, it produces quite a lot of output.
#
# For a normal regression test, this output is never looked at unless
# something goes wrong.  Thus, it would be wise to suppress the output
# normally.  This module does that, but it doesn't just throw the output
# from 'testall' away -- it compares it with 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 in a file 'testall.out'
# somewhere on the 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 'testall' manually
# and direct its output to 'testall.out'.
#
# The comparison uses (and demonstrates!) a rather new Python feature:
# program output that normally goes to stdout (by 'print' statements
# or by writing directly to sys.stdout) can be redirected to an
# arbitrary class instance as long as it has a 'write' method.

import os
import sys

# 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

# Exception raised when the test failed (not the same as in test_support)
TestFailed = 'autotest.TestFailed'

# Class substituted for sys.stdout, to compare it with the given file
class Compare:
	def init(self, filename):
		self.fp = open(filename, 'r')
		return self
	def write(self, data):
		expected = self.fp.read(len(data))
		if data <> expected:
			raise TestFailed, \
				'Writing: '+`data`+', expected: '+`expected`
	def close(self):
		self.fp.close()

# The main program
def main():
	import sys
	filename = findfile('testall.out')
	real_stdout = sys.stdout
	try:
		sys.stdout = Compare().init(filename)
		import testall
	finally:
		sys.stdout = real_stdout
	print 'All tests OK.'

main()