| 1 |
#!/bin/sh |
| 2 |
''''exec python3 -u "$0" "$@" #''' |
| 3 |
|
| 4 |
# $Id: alltests.py 10046 2025-03-09 01:45:28Z aa-turner $ |
| 5 |
# Author: David Goodger <[email protected]>, |
| 6 |
# Garth Kidd <[email protected]> |
| 7 |
# Copyright: This module has been placed in the public domain. |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
__doc__ = """\ |
| 12 |
All modules named 'test_*.py' in the current directory, and recursively in |
| 13 |
subdirectories (packages) called 'test_*', are loaded and test suites within |
| 14 |
are run. |
| 15 |
""" # NoQA: A001 (shadowing builtin `__doc__`) |
| 16 |
|
| 17 |
import atexit |
| 18 |
import platform |
| 19 |
import time |
| 20 |
import sys |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
# Prepend the "docutils root" to the Python library path |
| 24 |
# so we import the local `docutils` package. |
| 25 |
DOCUTILS_ROOT = Path(__file__).resolve().parents[1] |
| 26 |
sys.path.insert(0, str(DOCUTILS_ROOT)) |
| 27 |
|
| 28 |
import docutils # NoQA: E402 |
| 29 |
|
| 30 |
TYPE_CHECKING = False |
| 31 |
if TYPE_CHECKING: |
| 32 |
import types |
| 33 |
from typing import TextIO |
| 34 |
from unittest.case import TestCase |
| 35 |
|
| 36 |
from docutils.utils._typing import TypeAlias |
| 37 |
|
| 38 |
ErrorTriple: TypeAlias = tuple[ |
| 39 |
type[BaseException], |
| 40 |
BaseException, |
| 41 |
types.TracebackType, |
| 42 |
] |
| 43 |
|
| 44 |
STDOUT = sys.__stdout__ |
| 45 |
|
| 46 |
|
| 47 |
class Tee: |
| 48 |
"""Write to a file and stdout simultaneously.""" |
| 49 |
|
| 50 |
def __init__(self, filename: str) -> None: |
| 51 |
self.file: TextIO | None = open(filename, mode='w', |
| 52 |
encoding='utf-8', |
| 53 |
errors='backslashreplace') |
| 54 |
self.encoding: str = 'utf-8' |
| 55 |
atexit.register(self.close) |
| 56 |
|
| 57 |
def close(self) -> None: |
| 58 |
if self.file is not None: |
| 59 |
self.file.close() |
| 60 |
self.file = None |
| 61 |
|
| 62 |
def write(self, string: str) -> None: |
| 63 |
try: |
| 64 |
STDOUT.write(string) |
| 65 |
except UnicodeEncodeError: |
| 66 |
bstring = string.encode(STDOUT.encoding, errors='backslashreplace') |
| 67 |
STDOUT.buffer.write(bstring) |
| 68 |
if self.file is not None: |
| 69 |
self.file.write(string) |
| 70 |
|
| 71 |
def flush(self) -> None: |
| 72 |
STDOUT.flush() |
| 73 |
if self.file is not None: |
| 74 |
self.file.flush() |
| 75 |
|
| 76 |
|
| 77 |
# must redirect stderr *before* first import of unittest |
| 78 |
sys.stdout = sys.stderr = Tee('alltests.out') |
| 79 |
|
| 80 |
import unittest # NoQA: E402 |
| 81 |
|
| 82 |
|
| 83 |
class NumbersTestResult(unittest.TextTestResult): |
| 84 |
"""Result class that counts subTests.""" |
| 85 |
def addSubTest(self, |
| 86 |
test: TestCase, |
| 87 |
subtest: TestCase, |
| 88 |
error: ErrorTriple | None, |
| 89 |
) -> None: |
| 90 |
super().addSubTest(test, subtest, error) |
| 91 |
self.testsRun += 1 |
| 92 |
if self.dots: |
| 93 |
self.stream.write('.' if error is None else 'E') |
| 94 |
self.stream.flush() |
| 95 |
|
| 96 |
|
| 97 |
if __name__ == '__main__': |
| 98 |
# Start point for elapsed time, except suite setup. |
| 99 |
start = time.time() |
| 100 |
suite = unittest.defaultTestLoader.discover(str(DOCUTILS_ROOT / 'test')) |
| 101 |
print(f'Testing Docutils {docutils.__version__} ' |
| 102 |
f'with Python {sys.version.split()[0]} ' |
| 103 |
f'on {time.strftime("%Y-%m-%d at %H:%M:%S")}') |
| 104 |
print(f'OS: {platform.system()} {platform.release()} {platform.version()} ' |
| 105 |
f'({sys.platform}, {platform.platform()})') |
| 106 |
print(f'Working directory: {Path.cwd()}') |
| 107 |
print(f'Docutils package: {Path(docutils.__file__).parent}') |
| 108 |
sys.stdout.flush() |
| 109 |
result = unittest.TextTestRunner(resultclass=NumbersTestResult).run(suite) |
| 110 |
finish = time.time() |
| 111 |
print(f'Elapsed time: {finish - start:.3f} seconds') |
| 112 |
sys.exit(not result.wasSuccessful()) |