Update policies on branches and versioning.
[docutils.git] / docutils / test / alltests.py
blobd7ad4be207f6adaa7bb2ae99f8e0e6c9a48ee832
1 #!/bin/sh
2 ''''exec python -u "$0" "$@" #'''
4 # $Id$
5 # Author: David Goodger <goodger@python.org>
6 # Copyright: This module has been placed in the public domain.
8 __doc__ = \
9 """
10 All modules named 'test_*.py' in the current directory, and recursively in
11 subdirectories (packages) called 'test_*', are loaded and test suites within
12 are run.
13 """
15 import time
16 # Start point for actual elapsed time, including imports
17 # and setup outside of unittest.
18 start = time.time()
20 import sys
21 import os
22 import platform
23 import DocutilsTestSupport # must be imported before docutils
24 import docutils
27 class Tee:
29 """Write to a file and a stream (default: stdout) simultaneously."""
31 def __init__(self, filename, stream=sys.__stdout__):
32 self.file = open(filename, 'w')
33 self.stream = stream
34 self.encoding = getattr(stream, 'encoding', None)
36 def write(self, string):
37 try:
38 self.stream.write(string)
39 self.file.write(string)
40 except UnicodeEncodeError: # Py3k writing to "ascii" stream/file
41 string = string.encode('raw_unicode_escape').decode('ascii')
42 self.stream.write(string)
43 self.file.write(string)
45 def flush(self):
46 self.stream.flush()
47 self.file.flush()
50 def pformat(suite):
51 step = 4
52 suitestr = repr(suite).replace('=[<', '=[\n<').replace(', ', ',\n')
53 indent = 0
54 output = []
55 for line in suitestr.splitlines():
56 output.append(' ' * indent + line)
57 if line[-1:] == '[':
58 indent += step
59 else:
60 if line [-5:] == ']>]>,':
61 indent -= step * 2
62 elif line[-3:] == ']>,':
63 indent -= step
64 return '\n'.join(output)
66 def suite():
67 path, script = os.path.split(sys.argv[0])
68 suite = package_unittest.loadTestModules(DocutilsTestSupport.testroot,
69 'test_', packages=1)
70 sys.stdout.flush()
71 return suite
73 # must redirect stderr *before* first import of unittest
74 sys.stdout = sys.stderr = Tee('alltests.out')
76 import package_unittest
79 if __name__ == '__main__':
80 suite = suite()
81 print ('Testing Docutils %s [%s] with Python %s on %s at %s'
82 % (docutils.__version__, docutils.__version_details__,
83 sys.version.split()[0],
84 time.strftime('%Y-%m-%d'), time.strftime('%H:%M:%S')))
85 print ('OS: %s %s %s (%s, %s)'
86 % (platform.system(), platform.release(), platform.version(),
87 sys.platform, platform.platform()))
88 print 'Working directory: %s' % os.getcwd()
89 print 'Docutils package: %s' % os.path.dirname(docutils.__file__)
90 sys.stdout.flush()
91 result = package_unittest.main(suite)
92 #if package_unittest.verbosity > 1:
93 # print >>sys.stderr, pformat(suite) # check the test suite
94 finish = time.time()
95 print 'Elapsed time: %.3f seconds' % (finish - start)
96 sys.exit(not result.wasSuccessful())