|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import inspect |
| 4 | + |
| 5 | +import astroid |
| 6 | +from pylint.checkers.variables import VariablesChecker |
| 7 | +import pylint |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +def _is_pytest_mark_usefixtures(decorator): |
| 12 | + # expecting @pytest.mark.usefixture(...) |
| 13 | + try: |
| 14 | + if isinstance(decorator, astroid.Call): |
| 15 | + if decorator.func.attrname == 'usefixtures' and \ |
| 16 | + decorator.func.expr.attrname == 'mark' and \ |
| 17 | + decorator.func.expr.expr.name == 'pytest': |
| 18 | + return True |
| 19 | + except AttributeError: |
| 20 | + pass |
| 21 | + return False |
| 22 | + |
| 23 | + |
| 24 | +def _is_pytest_fixture(decorator): |
| 25 | + attr = None |
| 26 | + |
| 27 | + try: |
| 28 | + if isinstance(decorator, astroid.Attribute): |
| 29 | + # expecting @pytest.fixture |
| 30 | + attr = decorator |
| 31 | + |
| 32 | + if isinstance(decorator, astroid.Call): |
| 33 | + # expecting @pytest.fixture(scope=...) |
| 34 | + attr = decorator.func |
| 35 | + |
| 36 | + if attr and attr.attrname == 'fixture' and attr.expr.name == 'pytest': |
| 37 | + return True |
| 38 | + except AttributeError: |
| 39 | + pass |
| 40 | + |
| 41 | + return False |
| 42 | + |
| 43 | + |
| 44 | +def _can_use_fixture(function): |
| 45 | + if isinstance(function, astroid.FunctionDef): |
| 46 | + |
| 47 | + # test_*, *_test |
| 48 | + if function.name.startswith('test_') or function.name.endswith('_test'): |
| 49 | + return True |
| 50 | + |
| 51 | + if function.decorators: |
| 52 | + for decorator in function.decorators.nodes: |
| 53 | + # usefixture |
| 54 | + if _is_pytest_mark_usefixtures(decorator): |
| 55 | + return True |
| 56 | + |
| 57 | + # fixture |
| 58 | + if _is_pytest_fixture(decorator): |
| 59 | + return True |
| 60 | + |
| 61 | + return False |
| 62 | + |
| 63 | + |
| 64 | +def _is_same_module(fixtures, import_node, fixture_name): |
| 65 | + '''Comparing pytest fixture node with astroid.ImportFrom''' |
| 66 | + for fixture in fixtures[fixture_name]: |
| 67 | + for import_from in import_node.root().globals[fixture_name]: |
| 68 | + if inspect.getmodule(fixture.func).__file__ == \ |
| 69 | + import_from.parent.import_module(import_from.modname).file: |
| 70 | + return True |
| 71 | + |
| 72 | + return False |
| 73 | + |
| 74 | + |
| 75 | +# pylint: disable=protected-access |
| 76 | +class FixtureCollector: |
| 77 | + fixtures = {} |
| 78 | + |
| 79 | + def pytest_sessionfinish(self, session): |
| 80 | + self.fixtures = session._fixturemanager._arg2fixturedefs |
| 81 | + |
| 82 | + |
| 83 | +ORIGINAL = {} |
| 84 | + |
| 85 | + |
| 86 | +def unregister(): |
| 87 | + VariablesChecker.add_message = ORIGINAL['add_message'] |
| 88 | + del ORIGINAL['add_message'] |
| 89 | + VariablesChecker.visit_functiondef = ORIGINAL['visit_functiondef'] |
| 90 | + del ORIGINAL['visit_functiondef'] |
| 91 | + VariablesChecker.visit_module = ORIGINAL['visit_module'] |
| 92 | + del ORIGINAL['visit_module'] |
| 93 | + |
| 94 | + |
| 95 | +# pylint: disable=protected-access |
| 96 | +def register(_): |
| 97 | + '''Patch VariablesChecker to add additional checks for pytest fixtures |
| 98 | + ''' |
| 99 | + # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 100 | + def patched_visit_module(self, node): |
| 101 | + ''' |
| 102 | + - only run once per module |
| 103 | + - invoke pytest session to collect available fixtures |
| 104 | + - create containers for the module to store args and fixtures |
| 105 | + ''' |
| 106 | + # storing all fixtures discovered by pytest session |
| 107 | + self._pytest_fixtures = {} # Dict[List[_pytest.fixtures.FixtureDef]] |
| 108 | + |
| 109 | + # storing all used function arguments |
| 110 | + self._invoked_with_func_args = set() # Set[str] |
| 111 | + |
| 112 | + # storing all invoked fixtures through @pytest.mark.usefixture(...) |
| 113 | + self._invoked_with_usefixtures = set() # Set[str] |
| 114 | + |
| 115 | + try: |
| 116 | + with open(os.devnull, 'w') as devnull: |
| 117 | + # suppress any future output from pytest |
| 118 | + stdout, stderr = sys.stdout, sys.stderr |
| 119 | + sys.stderr = sys.stdout = devnull |
| 120 | + |
| 121 | + # run pytest session with customized plugin to collect fixtures |
| 122 | + fixture_collector = FixtureCollector() |
| 123 | + pytest.main( |
| 124 | + [node.file, '--fixtures'], |
| 125 | + plugins=[fixture_collector], |
| 126 | + ) |
| 127 | + self._pytest_fixtures = fixture_collector.fixtures |
| 128 | + finally: |
| 129 | + # restore output devices |
| 130 | + sys.stdout, sys.stderr = stdout, stderr |
| 131 | + |
| 132 | + ORIGINAL['visit_module'](self, node) |
| 133 | + ORIGINAL['visit_module'] = VariablesChecker.visit_module |
| 134 | + VariablesChecker.visit_module = patched_visit_module |
| 135 | + # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 136 | + |
| 137 | + # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 138 | + def patched_visit_functiondef(self, node): |
| 139 | + ''' |
| 140 | + - save invoked fixtures for later use |
| 141 | + - save used function arguments for later use |
| 142 | + ''' |
| 143 | + if _can_use_fixture(node): |
| 144 | + if node.decorators: |
| 145 | + # check all decorators |
| 146 | + for decorator in node.decorators.nodes: |
| 147 | + if _is_pytest_mark_usefixtures(decorator): |
| 148 | + # save all visited fixtures |
| 149 | + for arg in decorator.args: |
| 150 | + self._invoked_with_usefixtures.add(arg.value) |
| 151 | + for arg in node.args.args: |
| 152 | + self._invoked_with_func_args.add(arg.name) |
| 153 | + |
| 154 | + ORIGINAL['visit_functiondef'](self, node) |
| 155 | + ORIGINAL['visit_functiondef'] = VariablesChecker.visit_functiondef |
| 156 | + VariablesChecker.visit_functiondef = patched_visit_functiondef |
| 157 | + # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 158 | + |
| 159 | + # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 160 | + def patched_add_message(self, msgid, line=None, node=None, args=None, |
| 161 | + confidence=None, col_offset=None): |
| 162 | + ''' |
| 163 | + - intercept and discard unwanted warning messages |
| 164 | + ''' |
| 165 | + # check W0611 unused-import |
| 166 | + if msgid == 'unused-import': |
| 167 | + # actual attribute name is not passed as arg so...dirty hack |
| 168 | + # message is usually in the form of '%s imported from %s (as %)' |
| 169 | + message_tokens = args.split() |
| 170 | + fixture_name = message_tokens[0] |
| 171 | + |
| 172 | + # ignoring 'import %s' message |
| 173 | + if message_tokens[0] == 'import' and len(message_tokens) == 2: |
| 174 | + pass |
| 175 | + |
| 176 | + # imported fixture is referenced in test/fixture func |
| 177 | + elif fixture_name in self._invoked_with_func_args \ |
| 178 | + and fixture_name in self._pytest_fixtures: |
| 179 | + if _is_same_module(fixtures=self._pytest_fixtures, |
| 180 | + import_node=node, |
| 181 | + fixture_name=fixture_name): |
| 182 | + return |
| 183 | + |
| 184 | + # fixture is referenced in @pytest.mark.usefixtures |
| 185 | + elif fixture_name in self._invoked_with_usefixtures \ |
| 186 | + and fixture_name in self._pytest_fixtures: |
| 187 | + if _is_same_module(fixtures=self._pytest_fixtures, |
| 188 | + import_node=node, |
| 189 | + fixture_name=fixture_name): |
| 190 | + return |
| 191 | + |
| 192 | + # check W0613 unused-argument |
| 193 | + if msgid == 'unused-argument' and \ |
| 194 | + node.name in self._pytest_fixtures and \ |
| 195 | + _can_use_fixture(node.parent.parent): |
| 196 | + return |
| 197 | + |
| 198 | + # check W0621 redefined-outer-name |
| 199 | + if msgid == 'redefined-outer-name' and \ |
| 200 | + node.name in self._pytest_fixtures and \ |
| 201 | + _can_use_fixture(node.parent.parent): |
| 202 | + return |
| 203 | + |
| 204 | + if int(pylint.__version__.split('.')[0]) >= 2: |
| 205 | + ORIGINAL['add_message']( |
| 206 | + self, msgid, line, node, args, confidence, col_offset) |
| 207 | + else: |
| 208 | + # python2 + pylint1.9 backward compatibility |
| 209 | + ORIGINAL['add_message']( |
| 210 | + self, msgid, line, node, args, confidence) |
| 211 | + ORIGINAL['add_message'] = VariablesChecker.add_message |
| 212 | + VariablesChecker.add_message = patched_add_message |
| 213 | + # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
0 commit comments