This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
fixture.py
260 lines (227 loc) · 10.5 KB
/
fixture.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os
import sys
from pathlib import Path
import fnmatch
import astroid
import pylint
from pylint.checkers.variables import VariablesChecker
from pylint.interfaces import IAstroidChecker
import pytest
from ..utils import (
_can_use_fixture,
_is_pytest_mark,
_is_pytest_mark_usefixtures,
_is_pytest_fixture,
_is_same_module,
)
from . import BasePytestChecker
# TODO: support pytest python_files configuration
FILE_NAME_PATTERNS = ('test_*.py', '*_test.py')
class FixtureCollector:
fixtures = {}
errors = set()
def pytest_sessionfinish(self, session):
# pylint: disable=protected-access
self.fixtures = session._fixturemanager._arg2fixturedefs
def pytest_collectreport(self, report):
if report.failed:
self.errors.add(report)
class FixtureChecker(BasePytestChecker):
__implements__ = IAstroidChecker
msgs = {
'W6401': (
'Using a deprecated @pytest.yield_fixture decorator',
'deprecated-pytest-yield-fixture',
'Used when using a deprecated pytest decorator that has been deprecated in pytest-3.0'
),
'W6402': (
'Using useless `@pytest.mark.*` decorator for fixtures',
'useless-pytest-mark-decorator',
(
'@pytest.mark.* decorators can\'t by applied to fixtures. '
'Take a look at: https://docs.pytest.org/en/stable/reference.html#marks'
),
),
'W6403': (
'Using a deprecated positional arguments for fixture',
'deprecated-positional-argument-for-pytest-fixture',
(
'Pass scope as a kwarg, not positional arg, which is deprecated in future pytest. '
'Take a look at: https://docs.pytest.org/en/stable/deprecations.html#pytest-fixture-arguments-are-keyword-only'
),
),
'F6401': (
(
'pylint-pytest plugin cannot enumerate and collect pytest fixtures. '
'Please run `pytest --fixtures --collect-only path/to/current/module.py` and resolve any potential syntax error or package dependency issues'
),
'cannot-enumerate-pytest-fixtures',
'Used when pylint-pytest has been unable to enumerate and collect pytest fixtures.',
),
}
_pytest_fixtures = {}
_invoked_with_func_args = set()
_invoked_with_usefixtures = set()
_original_add_message = callable
def open(self):
# patch VariablesChecker.add_message
FixtureChecker._original_add_message = VariablesChecker.add_message
VariablesChecker.add_message = FixtureChecker.patch_add_message
def close(self):
'''restore & reset class attr for testing'''
# restore add_message
VariablesChecker.add_message = FixtureChecker._original_add_message
FixtureChecker._original_add_message = callable
# reset fixture info storage
FixtureChecker._pytest_fixtures = {}
FixtureChecker._invoked_with_func_args = set()
FixtureChecker._invoked_with_usefixtures = set()
def visit_module(self, node):
'''
- only run once per module
- invoke pytest session to collect available fixtures
- create containers for the module to store args and fixtures
'''
# storing all fixtures discovered by pytest session
FixtureChecker._pytest_fixtures = {} # Dict[List[_pytest.fixtures.FixtureDef]]
# storing all used function arguments
FixtureChecker._invoked_with_func_args = set() # Set[str]
# storing all invoked fixtures through @pytest.mark.usefixture(...)
FixtureChecker._invoked_with_usefixtures = set() # Set[str]
is_test_module = False
for pattern in FILE_NAME_PATTERNS:
if fnmatch.fnmatch(Path(node.file).name, pattern):
is_test_module = True
break
try:
with open(os.devnull, 'w') as devnull:
# suppress any future output from pytest
stdout, stderr = sys.stdout, sys.stderr
sys.stderr = sys.stdout = devnull
# run pytest session with customized plugin to collect fixtures
fixture_collector = FixtureCollector()
# save and restore sys.path to prevent pytest.main from altering it
sys_path = sys.path.copy()
ret = pytest.main(
[
node.file, '--fixtures', '--collect-only',
'--pythonwarnings=ignore:Module already imported:pytest.PytestWarning',
],
plugins=[fixture_collector],
)
# restore sys.path
sys.path = sys_path
FixtureChecker._pytest_fixtures = fixture_collector.fixtures
if (ret != pytest.ExitCode.OK or fixture_collector.errors) and is_test_module:
self.add_message('cannot-enumerate-pytest-fixtures', node=node)
finally:
# restore output devices
sys.stdout, sys.stderr = stdout, stderr
def visit_decorators(self, node):
"""
Walk through all decorators on functions.
Tries to find cases:
When uses `@pytest.fixture` with `scope` as positional argument (deprecated)
https://docs.pytest.org/en/stable/deprecations.html#pytest-fixture-arguments-are-keyword-only
>>> @pytest.fixture("module")
>>> def awesome_fixture(): ...
Instead
>>> @pytest.fixture(scope="module")
>>> def awesome_fixture(): ...
When uses `@pytest.mark.usefixtures` for fixture (useless because didn't work)
https://docs.pytest.org/en/stable/reference.html#marks
>>> @pytest.mark.usefixtures("another_fixture")
>>> @pytest.fixture
>>> def awesome_fixture(): ...
Parameters
----------
node : astroid.scoped_nodes.Decorators
"""
uses_fixture_deco, uses_mark_deco = False, False
for decorator in node.nodes:
try:
if _is_pytest_fixture(decorator) and isinstance(decorator, astroid.Call) and decorator.args:
self.add_message(
'deprecated-positional-argument-for-pytest-fixture', node=decorator
)
uses_fixture_deco |= _is_pytest_fixture(decorator)
uses_mark_deco |= _is_pytest_mark(decorator)
except AttributeError:
# ignore any parse exceptions
pass
if uses_mark_deco and uses_fixture_deco:
self.add_message("useless-pytest-mark-decorator", node=node)
def visit_functiondef(self, node):
'''
- save invoked fixtures for later use
- save used function arguments for later use
'''
if _can_use_fixture(node):
if node.decorators:
# check all decorators
for decorator in node.decorators.nodes:
if _is_pytest_mark_usefixtures(decorator):
# save all visited fixtures
for arg in decorator.args:
self._invoked_with_usefixtures.add(arg.value)
if int(pytest.__version__.split('.')[0]) >= 3 and \
_is_pytest_fixture(decorator, fixture=False):
# raise deprecated warning for @pytest.yield_fixture
self.add_message('deprecated-pytest-yield-fixture', node=node)
for arg in node.args.args:
self._invoked_with_func_args.add(arg.name)
# pylint: disable=protected-access,bad-staticmethod-argument
@staticmethod
def patch_add_message(self, msgid, line=None, node=None, args=None,
confidence=None, col_offset=None):
'''
- intercept and discard unwanted warning messages
'''
# check W0611 unused-import
if msgid == 'unused-import':
# actual attribute name is not passed as arg so...dirty hack
# message is usually in the form of '%s imported from %s (as %)'
message_tokens = args.split()
fixture_name = message_tokens[0]
# ignoring 'import %s' message
if message_tokens[0] == 'import' and len(message_tokens) == 2:
pass
# fixture is defined in other modules and being imported to
# conftest for pytest magic
elif isinstance(node.parent, astroid.Module) \
and node.parent.name.split('.')[-1] == 'conftest' \
and fixture_name in FixtureChecker._pytest_fixtures:
return
# imported fixture is referenced in test/fixture func
elif fixture_name in FixtureChecker._invoked_with_func_args \
and fixture_name in FixtureChecker._pytest_fixtures:
if _is_same_module(fixtures=FixtureChecker._pytest_fixtures,
import_node=node,
fixture_name=fixture_name):
return
# fixture is referenced in @pytest.mark.usefixtures
elif fixture_name in FixtureChecker._invoked_with_usefixtures \
and fixture_name in FixtureChecker._pytest_fixtures:
if _is_same_module(fixtures=FixtureChecker._pytest_fixtures,
import_node=node,
fixture_name=fixture_name):
return
# check W0613 unused-argument
if msgid == 'unused-argument' and \
_can_use_fixture(node.parent.parent) and \
isinstance(node.parent, astroid.Arguments) and \
node.name in FixtureChecker._pytest_fixtures:
return
# check W0621 redefined-outer-name
if msgid == 'redefined-outer-name' and \
_can_use_fixture(node.parent.parent) and \
isinstance(node.parent, astroid.Arguments) and \
node.name in FixtureChecker._pytest_fixtures:
return
if int(pylint.__version__.split('.')[0]) >= 2:
FixtureChecker._original_add_message(
self, msgid, line, node, args, confidence, col_offset)
else:
# python2 + pylint1.9 backward compatibility
FixtureChecker._original_add_message(
self, msgid, line, node, args, confidence)