1
1
import fnmatch
2
- import os
2
+ import io
3
3
import sys
4
4
from pathlib import Path
5
5
from typing import Set , Tuple
@@ -68,8 +68,8 @@ class FixtureChecker(BasePytestChecker):
68
68
"F6401" : (
69
69
(
70
70
"pylint-pytest plugin cannot enumerate and collect pytest fixtures. "
71
- "Please run `pytest --fixtures --collect-only path/to/current/module.py` "
72
- " and resolve any potential syntax error or package dependency issues"
71
+ "Please run `pytest --fixtures --collect-only %s` and resolve "
72
+ "any potential syntax error or package dependency issues. stdout: %s. stderr: %s. "
73
73
),
74
74
"cannot-enumerate-pytest-fixtures" ,
75
75
"Used when pylint-pytest has been unable to enumerate and collect pytest fixtures." ,
@@ -118,9 +118,10 @@ def visit_module(self, node):
118
118
119
119
stdout , stderr = sys .stdout , sys .stderr
120
120
try :
121
- with open ( os . devnull , "w" ) as devnull :
121
+ with io . StringIO () as captured_stdout , io . StringIO ( ) as captured_stderr :
122
122
# suppress any future output from pytest
123
- sys .stderr = sys .stdout = devnull
123
+ sys .stderr = captured_stderr
124
+ sys .stdout = captured_stdout
124
125
125
126
# run pytest session with customized plugin to collect fixtures
126
127
fixture_collector = FixtureCollector ()
@@ -143,8 +144,32 @@ def visit_module(self, node):
143
144
144
145
FixtureChecker ._pytest_fixtures = fixture_collector .fixtures
145
146
146
- if (ret != pytest .ExitCode .OK or fixture_collector .errors ) and is_test_module :
147
- self .add_message ("cannot-enumerate-pytest-fixtures" , node = node )
147
+ legitimate_failure_paths = set (
148
+ collection_report .nodeid
149
+ for collection_report in fixture_collector .errors
150
+ if any (
151
+ fnmatch .fnmatch (
152
+ Path (collection_report .nodeid ).name ,
153
+ pattern ,
154
+ )
155
+ for pattern in FILE_NAME_PATTERNS
156
+ )
157
+ )
158
+ if (ret != pytest .ExitCode .OK or legitimate_failure_paths ) and is_test_module :
159
+ files_to_report = {
160
+ str (Path (x ).absolute ().relative_to (Path .cwd ()))
161
+ for x in legitimate_failure_paths | {node .file }
162
+ }
163
+
164
+ self .add_message (
165
+ "cannot-enumerate-pytest-fixtures" ,
166
+ args = (
167
+ " " .join (files_to_report ),
168
+ captured_stdout .getvalue (),
169
+ captured_stderr .getvalue (),
170
+ ),
171
+ node = node ,
172
+ )
148
173
finally :
149
174
# restore output devices
150
175
sys .stdout , sys .stderr = stdout , stderr
0 commit comments