Mask trailing whitespace in test sample.
[docutils.git] / docutils / test / alltests.py
blob4aadc770b245e113ed9e7cb1ebca7d4e0739f82d
1 #!/bin/sh
2 ''''exec python3 -u "$0" "$@" #'''
4 # $Id$
5 # Author: David Goodger <goodger@python.org>,
6 # Garth Kidd <garth@deadlybloodyserious.com>
7 # Copyright: This module has been placed in the public domain.
9 __doc__ = """\
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 atexit # noqa: E402
21 import os # noqa: E402
22 from pathlib import Path # noqa: E402
23 import platform # noqa: E402
24 import sys # noqa: E402
26 # Prepend the "docutils root" to the Python library path
27 # so we import the local `docutils` package.
28 # For Python < 3.9, we need `resolve()` to ensure an absolute path.
29 # https://docs.python.org/3/whatsnew/3.9.html#other-language-changes
30 DOCUTILS_ROOT = Path(__file__).resolve().parents[1]
31 sys.path.insert(0, str(DOCUTILS_ROOT))
33 import docutils # noqa: E402
36 class Tee:
38 """Write to a file and a stream (default: stdout) simultaneously."""
40 def __init__(self, filename, stream=sys.__stdout__):
41 self.file = open(filename, 'w', encoding='utf-8',
42 errors='backslashreplace')
43 atexit.register(self.close)
44 self.stream = stream
45 self.encoding = getattr(stream, 'encoding', None)
47 def close(self):
48 self.file.close()
49 self.file = None
51 def write(self, string):
52 try:
53 self.stream.write(string)
54 except UnicodeEncodeError:
55 bstring = string.encode(self.encoding, errors='backslashreplace')
56 self.stream.write(bstring.decode())
57 if self.file:
58 self.file.write(string)
60 def flush(self):
61 self.stream.flush()
62 if self.file:
63 self.file.flush()
66 # must redirect stderr *before* first import of unittest
67 sys.stdout = sys.stderr = Tee('alltests.out')
69 import unittest # NoQA: E402
72 class NumbersTestResult(unittest.TextTestResult):
73 """Result class that counts subTests."""
74 def addSubTest(self, test, subtest, error):
75 super().addSubTest(test, subtest, error)
76 self.testsRun += 1
77 if self.dots:
78 self.stream.write('.' if error is None else 'E')
79 self.stream.flush()
82 if __name__ == '__main__':
83 suite = unittest.defaultTestLoader.discover(str(DOCUTILS_ROOT / 'test'))
84 print(f'Testing Docutils {docutils.__version__} '
85 f'with Python {sys.version.split()[0]} '
86 f'on {time.strftime("%Y-%m-%d at %H:%M:%S")}')
87 print(f'OS: {platform.system()} {platform.release()} {platform.version()} '
88 f'({sys.platform}, {platform.platform()})')
89 print(f'Working directory: {os.getcwd()}')
90 print(f'Docutils package: {os.path.dirname(docutils.__file__)}')
91 sys.stdout.flush()
92 result = unittest.TextTestRunner(resultclass=NumbersTestResult).run(suite)
93 finish = time.time()
94 print(f'Elapsed time: {finish - start:.3f} seconds')
95 sys.exit(not result.wasSuccessful())