Repository navigation
Expand file tree
/
Copy pathpytest_flake8.py
More file actions
255 lines (223 loc) · 8.23 KB
/
Copy pathpytest_flake8.py
File metadata and controls
255 lines (223 loc) · 8.23 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
243
244
245
246
247
248
249
250
251
252
253
254
255
"""py.test plugin to test with flake8."""
import fnmatch
import os
import re
from contextlib import redirect_stderr, redirect_stdout
from io import BytesIO, TextIOWrapper
import pytest
from flake8.main import application
HISTKEY = "flake8/mtimes"
def pytest_addoption(parser):
"""Hook up additional options."""
group = parser.getgroup("general")
group.addoption(
'--flake8',
action='store_true',
help="perform some flake8 sanity checks on .py files",
)
parser.addini(
"flake8-ignore",
type="linelist",
help="each line specifies a glob pattern and whitespace "
"separated FLAKE8 errors or warnings which will be ignored, "
"example: *.py W293",
)
parser.addini("flake8-max-line-length", help="maximum line length")
parser.addini("flake8-max-doc-length", help="maximum doc line length")
parser.addini("flake8-max-complexity", help="McCabe complexity threshold")
parser.addini(
"flake8-show-source",
type="bool",
help="show the source generate each error or warning",
)
parser.addini("flake8-statistics", type="bool", help="count errors and warnings")
parser.addini(
"flake8-extensions",
type="args",
default=[".py"],
help="a list of file extensions, for example: .py .pyx",
)
def pytest_configure(config):
"""Start a new session."""
if config.option.flake8:
config._flake8ignore = Ignorer(config.getini("flake8-ignore"))
config._flake8maxlen = config.getini("flake8-max-line-length")
config._flake8maxdoclen = config.getini("flake8-max-doc-length")
config._flake8maxcomplexity = config.getini("flake8-max-complexity")
config._flake8showsource = config.getini("flake8-show-source")
config._flake8statistics = config.getini("flake8-statistics")
config._flake8exts = config.getini("flake8-extensions")
config.addinivalue_line('markers', "flake8: Tests which run flake8.")
if hasattr(config, 'cache'):
config._flake8mtimes = config.cache.get(HISTKEY, {})
def pytest_collect_file(file_path, parent):
"""Filter files down to which ones should be checked."""
config = parent.config
if config.option.flake8 and file_path.suffix in config._flake8exts:
flake8ignore = config._flake8ignore(file_path)
if flake8ignore is not None:
item = Flake8File.from_parent(
parent,
path=file_path,
flake8ignore=flake8ignore,
maxlength=config._flake8maxlen,
maxdoclength=config._flake8maxdoclen,
maxcomplexity=config._flake8maxcomplexity,
showsource=config._flake8showsource,
statistics=config._flake8statistics,
)
return item
def pytest_unconfigure(config):
"""Flush cache at end of run."""
if hasattr(config, "_flake8mtimes"):
config.cache.set(HISTKEY, config._flake8mtimes)
class Flake8Error(Exception):
"""indicates an error during flake8 checks."""
class Flake8File(pytest.File):
def __init__(
self,
*k,
flake8ignore=None,
maxlength=None,
maxdoclength=None,
maxcomplexity=None,
showsource=None,
statistics=None,
**kw,
):
super().__init__(*k, **kw)
self.flake8ignore = flake8ignore
self.maxlength = maxlength
self.maxdoclength = maxdoclength
self.maxcomplexity = maxcomplexity
self.showsource = showsource
self.statistics = statistics
def collect(self):
return [Flake8Item.from_parent(self, name="flake-8")]
class Flake8Item(pytest.Item):
def __init__(self, *k, **kwargs):
super().__init__(*k, **kwargs)
self._nodeid += "::FLAKE8"
self.add_marker("flake8")
self.flake8ignore = self.parent.flake8ignore
self.maxlength = self.parent.maxlength
self.maxdoclength = self.parent.maxdoclength
self.maxcomplexity = self.parent.maxcomplexity
self.showsource = self.parent.showsource
self.statistics = self.parent.statistics
def setup(self):
if hasattr(self.config, "_flake8mtimes"):
flake8mtimes = self.config._flake8mtimes
else:
flake8mtimes = {}
self._flake8mtime = self.fspath.mtime()
old = flake8mtimes.get(str(self.fspath), (0, []))
if old == [self._flake8mtime, self.flake8ignore]:
pytest.skip("file(s) previously passed FLAKE8 checks")
def runtest(self):
with (
BytesIO() as bo,
TextIOWrapper(bo, encoding='utf-8') as to,
BytesIO() as be,
TextIOWrapper(be, encoding='utf-8') as te,
redirect_stdout(to),
redirect_stderr(te),
):
found_errors = check_file(
self.fspath,
self.flake8ignore,
self.maxlength,
self.maxdoclength,
self.maxcomplexity,
self.showsource,
self.statistics,
)
to.flush()
te.flush()
out = bo.getvalue().decode('utf-8')
err = be.getvalue().decode('utf-8')
if found_errors:
raise Flake8Error(out, err)
# update mtime only if test passed
# otherwise failures would not be re-run next time
if hasattr(self.config, "_flake8mtimes"):
self.config._flake8mtimes[str(self.fspath)] = (
self._flake8mtime,
self.flake8ignore,
)
def repr_failure(self, excinfo):
if excinfo.errisinstance(Flake8Error):
return excinfo.value.args[0]
return super().repr_failure(excinfo)
def reportinfo(self):
if self.flake8ignore:
ignores = "(ignoring {})".format(" ".join(self.flake8ignore))
else:
ignores = ""
return (self.fspath, -1, f"FLAKE8-check{ignores}")
class Ignorer:
def __init__(self, ignorelines, coderex=re.compile(r"[EW]\d\d\d")):
self.ignores = ignores = []
for line in ignorelines:
i = line.find("#")
if i != -1:
line = line[:i]
try:
glob, ign = line.split(None, 1)
except ValueError:
glob, ign = None, line
if glob and coderex.match(glob):
glob, ign = None, line
ign = ign.split()
if "ALL" in ign:
ign = None
if glob and "/" != os.sep and "/" in glob:
glob = glob.replace("/", os.sep)
ignores.append((glob, ign))
def __call__(self, path):
l = []
for glob, ignlist in self.ignores:
if not glob or _fnmatch(path, glob):
if ignlist is None:
return None
l.extend(ignlist)
return l
def _fnmatch(path, pattern):
"""
Match path against pattern with the semantics of ``py.path.local.fnmatch``.
A pattern without a path separator matches the basename. A relative
pattern with a separator matches the end of the path.
>>> _fnmatch('/a/b/x.py', '*.py')
True
>>> _fnmatch(os.path.join(os.sep, 'a', 'b', 'x.py'), os.path.join('b', '?.py'))
True
>>> _fnmatch(os.path.join(os.sep, 'a', 'x.py'), os.path.join('b', '?.py'))
False
"""
name = os.fspath(path)
if os.sep not in pattern:
name = os.path.basename(name)
elif not os.path.isabs(pattern):
pattern = '*' + os.sep + pattern
return fnmatch.fnmatch(name, pattern)
def check_file(
path, flake8ignore, maxlength, maxdoclenght, maxcomplexity, showsource, statistics
):
"""Run flake8 over a single file, and return the number of failures."""
args = []
if maxlength:
args += ['--max-line-length', maxlength]
if maxdoclenght:
args += ['--max-doc-length', maxdoclenght]
if maxcomplexity:
args += ['--max-complexity', maxcomplexity]
if showsource:
args += ['--show-source']
if statistics:
args += ['--statistics']
app = application.Application()
if flake8ignore:
args += ["--ignore", flake8ignore]
args += [str(path)]
app.run(args)
return app.result_count