-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathconftest.py
More file actions
126 lines (99 loc) · 3.37 KB
/
conftest.py
File metadata and controls
126 lines (99 loc) · 3.37 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
#
# (C) Pywikibot team, 2026
#
# Distributed under the terms of the MIT license.
#
"""Configuration file for pytest.
.. version-added:: 10.3
"""
from __future__ import annotations
import re
import time
from pathlib import Path
from typing import Literal
from pywikibot.tools import SPHINX_RUNNING
try:
import pytest
except ModuleNotFoundError:
if SPHINX_RUNNING:
class _DummyPytest:
def fixture(self, *args, **kwargs):
def wrapper(func):
return func
return wrapper
pytest = _DummyPytest()
else:
pytest = None
EXCLUDE_PATTERN = re.compile(
r'(?:'
r'(__metadata__|backports|config|cosmetic_changes|daemonize|diff|echo|'
r'exceptions|fixes|logging|plural|time|titletranslate)|'
r'(comms|data|families|specialbots)/__init__|'
r'comms/eventstreams|'
r'data/(api/(__init__|_optionset)|citoid|memento|wikistats)|'
r'families/[a-z][a-z\d]+_family|'
r'page/(__init__|_decorators|_page|_revision|_user)|'
r'pagegenerators/(__init__|_filters)|'
r'scripts/(i18n/)?__init__|'
r'site/(__init__|_basesite|_decorators|_interwikimap|'
r'_tokenwallet|_upload)|'
r'tools/(_deprecate|_logging|_unidata|chars|formatter|itertools)|'
r'userinterfaces/(__init__|_interface_base|buffer_interface|'
r'terminal_interface|transliteration)'
r')\.py'
)
def pytest_ignore_collect(collection_path: Path,
config) -> Literal[True] | None:
"""Ignore files matching EXCLUDE_PATTERN when pytest-mypy is loaded."""
# Check if any plugin name includes 'mypy'
plugin_names = {p.__class__.__name__.lower()
for p in config.pluginmanager.get_plugins()}
if not any('mypy' in name for name in plugin_names):
return None
# no cover: start
project_root = Path(__file__).parent / 'pywikibot'
try:
rel_path = collection_path.relative_to(project_root)
except ValueError:
# Ignore files outside project root
return None
norm_path = rel_path.as_posix()
if EXCLUDE_PATTERN.fullmatch(norm_path):
print(f'Ignoring file in mypy: {norm_path}') # noqa: T201
return True
return None
# no cover: stop
def pytest_addoption(parser) -> None:
"""Add CLI option --doctest-wait to pause between doctests.
If the option is given without parameter, the default value is 0.5
seconds.
.. version-added:: 11.0
:param parser: The pytest parser object used to add CLI options.
:type parser: _pytest.config.argparsing.Parser
"""
parser.addoption(
'--doctest-wait',
action='store',
nargs='?',
type=float,
const=0.5,
default=0.0,
help='Pause (seconds) between doctests only. Default is 0.5 s.',
)
if pytest:
@pytest.fixture(autouse=True)
def pause_between_doctests(request) -> None:
"""Insert a pause after each doctest if enabled.
.. version-added:: 11.0
:param request: The pytest FixtureRequest object providing test
context.
:type request: _pytest.fixtures.FixtureRequest
"""
# Handle Doctests only
if type(request.node).__name__ != 'DoctestItem':
yield
return
yield
wait_time = request.config.getoption('--doctest-wait')
if wait_time > 0:
time.sleep(wait_time)