2 ''''exec python3
-u "$0" "$@" #'''
5 # Author: David Goodger <goodger@python.org>,
6 # Garth Kidd <garth@deadlybloodyserious.com>
7 # Copyright: This module has been placed in the public domain.
10 All modules named 'test_*.py' in the current directory, and recursively in
11 subdirectories (packages) called 'test_*', are loaded and test suites within
16 # Start point for actual elapsed time, including imports
17 # and setup outside of unittest.
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
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
)
45 self.encoding
= getattr
(stream
, 'encoding', None
)
51 def
write(self
, string
):
53 self.stream.
write(string
)
54 except UnicodeEncodeError
:
55 bstring
= string.encode
(self.encoding
, errors
='backslashreplace')
56 self.stream.
write(bstring.decode
())
58 self.
file.
write(string
)
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
)
78 self.stream.
write('.' if error is None
else 'E')
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__)}')
92 result
= unittest.TextTestRunner
(resultclass
=NumbersTestResult
).run
(suite
)
94 print
(f
'Elapsed time: {finish - start:.3f} seconds')
95 sys.
exit(not result.wasSuccessful
())