File: alltests.py

package info (click to toggle)
python-docutils 0.22%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 11,448 kB
  • sloc: python: 53,302; lisp: 14,475; xml: 1,807; javascript: 1,032; makefile: 102; sh: 96
file content (109 lines) | stat: -rwxr-xr-x 3,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
''''exec python3 -u "$0" "$@" #'''

# $Id: alltests.py 10046 2025-03-09 01:45:28Z aa-turner $
# Author: David Goodger <goodger@python.org>,
#         Garth Kidd <garth@deadlybloodyserious.com>
# Copyright: This module has been placed in the public domain.

from __future__ import annotations

__doc__ = """\
All modules named 'test_*.py' in the current directory, and recursively in
subdirectories (packages) called 'test_*', are loaded and test suites within
are run.
"""  # NoQA: A001 (shadowing builtin `__doc__`)

import atexit
import platform
import time
import sys
from pathlib import Path

# Prepend the "docutils root" to the Python library path
# so we import the local `docutils` package.
DOCUTILS_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(DOCUTILS_ROOT))

import docutils  # NoQA: E402

TYPE_CHECKING = False
if TYPE_CHECKING:
    import types
    from typing import TextIO
    from unittest.case import TestCase

    from docutils.utils._typing import TypeAlias

    ErrorTriple: TypeAlias = tuple[
        type[BaseException],
        BaseException,
        types.TracebackType,
        ]

STDOUT = sys.__stdout__


class Tee:
    """Write to a file and stdout simultaneously."""

    def __init__(self, filename: str) -> None:
        self.file: TextIO | None = open(filename, mode='w',
                                        encoding='utf-8',
                                        errors='backslashreplace')
        self.encoding: str = 'utf-8'
        atexit.register(self.close)

    def close(self) -> None:
        if self.file is not None:
            self.file.close()
            self.file = None

    def write(self, string: str) -> None:
        try:
            STDOUT.write(string)
        except UnicodeEncodeError:
            bstring = string.encode(STDOUT.encoding, errors='backslashreplace')
            STDOUT.buffer.write(bstring)
        if self.file is not None:
            self.file.write(string)

    def flush(self) -> None:
        STDOUT.flush()
        if self.file is not None:
            self.file.flush()


import unittest  # NoQA: E402


class NumbersTestResult(unittest.TextTestResult):
    """Result class that counts subTests."""
    def addSubTest(self,
                   test: TestCase,
                   subtest: TestCase,
                   error: ErrorTriple | None,
                   ) -> None:
        super().addSubTest(test, subtest, error)
        self.testsRun += 1
        if self.dots:
            self.stream.write('.' if error is None else 'E')
            self.stream.flush()


if __name__ == '__main__':
    # Start point for elapsed time, except suite setup.
    start = time.time()
    suite = unittest.defaultTestLoader.discover(str(DOCUTILS_ROOT / 'test'))
    print(f'Testing Docutils {docutils.__version__} '
          f'with Python {sys.version.split()[0]} '
          f'on {time.strftime("%Y-%m-%d at %H:%M:%S")}')
    print(f'OS: {platform.system()} {platform.release()} {platform.version()} '
          f'({sys.platform}, {platform.platform()})')
    print(f'Working directory: {Path.cwd()}')
    print(f'Docutils package: {Path(docutils.__file__).parent}')
    sys.stdout.flush()
    result = unittest.TextTestRunner(resultclass=NumbersTestResult, verbosity=2).run(suite)
    finish = time.time()
    print(f'Elapsed time: {finish - start:.3f} seconds')
    sys.exit(not result.wasSuccessful())