-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest_doctest_docutils.py
More file actions
242 lines (198 loc) · 7.72 KB
/
pytest_doctest_docutils.py
File metadata and controls
242 lines (198 loc) · 7.72 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""pytest_doctest_docutils
pytest plugin for doctest w/ reStructuredText and markdown
.. seealso::
- http://www.sphinx-doc.org/en/stable/ext/doctest.html
- https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/doctest.py
This is a derivative of my PR https://github.com/thisch/pytest-sphinx/pull/38 to
pytest-sphinx (BSD 3-clause), 2022-09-03.
"""
import bdb
import doctest
import logging
import sys
import types
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterable, Optional, Tuple, Type
import pytest
import _pytest
from _pytest import outcomes
from _pytest.outcomes import OutcomeException
from doctest_docutils import DocutilsDocTestFinder, setup
if TYPE_CHECKING:
from doctest import _Out
from _pytest.config.argparsing import Parser
from _pytest.doctest import DoctestItem
logger = logging.getLogger(__name__)
# Lazy definition of runner class
RUNNER_CLASS = None
def pytest_addoption(parser: "Parser") -> None:
group = parser.getgroup("collect")
group.addoption(
"--doctest-docutils-modules",
action="store_true",
default=False,
help="run doctest-doctests in .py modules (pass-through to pytest-doctest)",
dest="doctestmodules",
)
group.addoption(
"--no-doctest-docutils-modules",
action="store_false",
help="disable doctest-doctests in .py modules (pass-through to pytest-doctest)",
dest="doctestmodules",
)
def pytest_configure(config: pytest.Config) -> None:
"""Disable pytest.doctest to prevent running tests twice.
Todo: Find a way to make these plugins cooperate without collecting twice.
"""
if config.pluginmanager.has_plugin("doctest"):
config.pluginmanager.set_blocked("doctest")
def pytest_unconfigure() -> None:
global RUNNER_CLASS
RUNNER_CLASS = None
def pytest_collect_file(
file_path: Path, parent: pytest.Collector
) -> Optional[Tuple["DocTestDocutilsFile", "_pytest.doctest.DoctestModule"]]:
config = parent.config
if file_path.suffix == ".py":
if config.option.doctestmodules and not any(
# if not any(
(
_pytest.doctest._is_setup_py(file_path),
_pytest.doctest._is_main_py(file_path),
)
):
mod: Tuple[
"DocTestDocutilsFile", "_pytest.doctest.DoctestModule"
] = _pytest.doctest.DoctestModule.from_parent(parent, path=file_path)
return mod
elif _is_doctest(config, file_path, parent):
return DocTestDocutilsFile.from_parent(parent, path=file_path) # type: ignore
return None
def _is_doctest(config: pytest.Config, path: Path, parent: pytest.Collector) -> bool:
if path.suffix in (".rst", ".md") and parent.session.isinitpath(path):
return True
globs = config.getoption("doctestglob") or ["*.rst", "*.md"]
for glob in globs:
if path.match(path_pattern=glob):
return True
return False
def _init_runner_class() -> Type["doctest.DocTestRunner"]:
import doctest
class PytestDoctestRunner(doctest.DebugRunner):
"""Runner to collect failures.
Note that the out variable in this case is a list instead of a
stdout-like object.
"""
def __init__(
self,
checker: Optional["doctest.OutputChecker"] = None,
verbose: Optional[bool] = None,
optionflags: int = 0,
continue_on_failure: bool = True,
) -> None:
super().__init__(checker=checker, verbose=verbose, optionflags=optionflags)
self.continue_on_failure = continue_on_failure
def report_failure(
self,
out: "_Out",
test: "doctest.DocTest",
example: "doctest.Example",
got: str,
) -> None:
failure = doctest.DocTestFailure(test, example, got)
if self.continue_on_failure:
assert isinstance(out, list)
out.append(failure)
else:
raise failure
def report_unexpected_exception(
self,
out: "_Out",
test: "doctest.DocTest",
example: "doctest.Example",
exc_info: Tuple[Type[BaseException], BaseException, types.TracebackType],
) -> None:
if isinstance(exc_info[1], OutcomeException):
raise exc_info[1]
if isinstance(exc_info[1], bdb.BdbQuit):
outcomes.exit("Quitting debugger")
failure = doctest.UnexpectedException(test, example, exc_info)
if self.continue_on_failure:
assert isinstance(out, list)
out.append(failure)
else:
raise failure
return PytestDoctestRunner
def _get_runner(
checker: Optional["doctest.OutputChecker"] = None,
verbose: Optional[bool] = None,
optionflags: int = 0,
continue_on_failure: bool = True,
) -> "doctest.DocTestRunner":
# We need this in order to do a lazy import on doctest
global RUNNER_CLASS
if RUNNER_CLASS is None:
RUNNER_CLASS = _init_runner_class()
# Type ignored because the continue_on_failure argument is only defined on
# PytestDoctestRunner, which is lazily defined so can't be used as a type.
return RUNNER_CLASS( # type: ignore
checker=checker,
verbose=verbose,
optionflags=optionflags,
continue_on_failure=continue_on_failure,
)
class DocutilsDocTestRunner(doctest.DocTestRunner):
def summarize( # type: ignore
self, out: "_Out", verbose: Optional[bool] = None
) -> Tuple[int, int]:
string_io = StringIO()
old_stdout = sys.stdout
sys.stdout = string_io
try:
res = super().summarize(verbose)
finally:
sys.stdout = old_stdout
out(string_io.getvalue())
return res
def _DocTestRunner__patched_linecache_getlines(
self, filename: str, module_globals: Any = None
) -> Any:
# this is overridden from DocTestRunner adding the try-except below
m = self._DocTestRunner__LINECACHE_FILENAME_RE.match(filename) # type: ignore
if m and m.group("name") == self.test.name:
try:
example = self.test.examples[int(m.group("examplenum"))]
# because we compile multiple doctest blocks with the same name
# (viz. the group name) this might, for outer stack frames in a
# traceback, get the wrong test which might not have enough examples
except IndexError:
pass
else:
return example.source.splitlines(True)
return self.save_linecache_getlines(filename, module_globals) # type: ignore
class DocTestDocutilsFile(pytest.Module):
def collect(self) -> Iterable["DoctestItem"]:
setup()
encoding = self.config.getini("doctest_encoding")
text = self.fspath.read_text(encoding)
# Uses internal doctest module parsing mechanism.
finder = DocutilsDocTestFinder()
optionflags = _pytest.doctest.get_optionflags(self) # type: ignore
runner = _get_runner(
verbose=False,
optionflags=optionflags,
checker=_pytest.doctest._get_checker(),
continue_on_failure=_pytest.doctest._get_continue_on_failure( # type:ignore
self.config
),
)
from _pytest.doctest import DoctestItem
for test in finder.find(
text,
str(self.fspath),
):
if test.examples: # skip empty doctests
yield DoctestItem.from_parent(
self, name=test.name, runner=runner, dtest=test # type: ignore
)