Fix [ 3525847 ] UnicodeEncodeError with locale == C
[docutils/kirr.git] / docutils / test / alltests.py
blob79d0059f458ba62edf81d0d1018046bfbf798b06
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 DocutilsTestSupport # must be imported before docutils
23 import docutils
26 class Tee:
28 """Write to a file and a stream (default: stdout) simultaneously."""
30 def __init__(self, filename, stream=sys.__stdout__):
31 self.file = open(filename, 'w')
32 self.stream = stream
33 self.encoding = getattr(stream, 'encoding', None)
35 def write(self, string):
36 try:
37 self.stream.write(string)
38 self.file.write(string)
39 except UnicodeEncodeError: # Py3k writing to "ascii" stream/file
40 string = string.encode('raw_unicode_escape').decode('ascii')
41 self.stream.write(string)
42 self.file.write(string)
44 def flush(self):
45 self.stream.flush()
46 self.file.flush()
49 def pformat(suite):
50 step = 4
51 suitestr = repr(suite).replace('=[<', '=[\n<').replace(', ', ',\n')
52 indent = 0
53 output = []
54 for line in suitestr.splitlines():
55 output.append(' ' * indent + line)
56 if line[-1:] == '[':
57 indent += step
58 else:
59 if line [-5:] == ']>]>,':
60 indent -= step * 2
61 elif line[-3:] == ']>,':
62 indent -= step
63 return '\n'.join(output)
65 def suite():
66 path, script = os.path.split(sys.argv[0])
67 suite = package_unittest.loadTestModules(DocutilsTestSupport.testroot,
68 'test_', packages=1)
69 sys.stdout.flush()
70 return suite
72 # must redirect stderr *before* first import of unittest
73 sys.stdout = sys.stderr = Tee('alltests.out')
75 import package_unittest
78 if __name__ == '__main__':
79 suite = suite()
80 print ('Testing Docutils %s [%s] with Python %s on %s at %s'
81 % (docutils.__version__, docutils.__version_details__,
82 sys.version.split()[0],
83 time.strftime('%Y-%m-%d'), time.strftime('%H:%M:%S')))
84 print 'Working directory: %s' % os.getcwd()
85 print 'Docutils package: %s' % os.path.dirname(docutils.__file__)
86 sys.stdout.flush()
87 result = package_unittest.main(suite)
88 #if package_unittest.verbosity > 1:
89 # print >>sys.stderr, pformat(suite) # check the test suite
90 finish = time.time()
91 print 'Elapsed time: %.3f seconds' % (finish - start)
92 sys.exit(not result.wasSuccessful())