forked from coherent-oss/pytest-flake8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_flake8.py
202 lines (170 loc) · 5.42 KB
/
test_flake8.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
"""Unit tests for flake8 pytest plugin."""
import pathlib
import textwrap
from packaging import version
import pytest
from flake8 import __version__ as flake8_version
pytest_plugins = ("pytester",)
def run_pytest(testdir, *args):
"""
Run pytest with flake8 enabled (and the enabler disabled).
"""
return testdir.runpytest("-p", "no:enabler", "--flake8", *args)
class TestIgnores:
"""Test ignores."""
@pytest.fixture
def example(self, request):
"""Create a test file."""
testdir = request.getfixturevalue("testdir")
import sys
print(testdir, file=sys.stderr)
p = testdir.makepyfile("")
p.write("class AClass:\n pass\n \n\n# too many spaces")
return p
def test_ignores(self, tmpdir):
"""Verify parsing of ignore statements."""
from pytest_flake8 import Ignorer
ignores = ["E203", "b/?.py E204 W205", "z.py ALL", "*.py E300"]
ign = Ignorer(ignores)
assert ign(tmpdir.join("a/b/x.py")) == "E203 E204 W205 E300".split()
assert ign(tmpdir.join("a/y.py")) == "E203 E300".split()
assert ign(tmpdir.join("a/z.py")) is None
@pytest.mark.xfail(
version.parse(flake8_version) >= version.parse("6.0.0"),
reason="Requires Flake8 version earlier than 6.0.0.",
)
def test_default_flake8_ignores(self, testdir):
testdir.makeini("""
[pytest]
markers = flake8
[flake8]
ignore = E203
*.py E300
tests/*.py ALL E203 # something
""")
testdir.tmpdir.ensure("xy.py")
testdir.tmpdir.ensure("tests/hello.py")
result = run_pytest(testdir, "-s")
result.assert_outcomes(passed=2)
result.stdout.fnmatch_lines([
"*collected 2*",
"*xy.py .*",
"*2 passed*",
])
def test_ignores_all(self, testdir):
"""Verify success when all errors are ignored."""
testdir.makeini("""
[pytest]
markers = flake8
flake8-ignore = E203
*.py E300
tests/*.py ALL E203 # something
""")
testdir.tmpdir.ensure("xy.py")
testdir.tmpdir.ensure("tests/hello.py")
result = run_pytest(testdir, "-s")
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines([
"*collected 1*",
"*xy.py .*",
"*1 passed*",
])
def test_w293w292(self, testdir, example):
result = run_pytest(testdir)
result.stdout.fnmatch_lines([
# "*plugins*flake8*",
"*W293*",
"*W292*",
])
result.assert_outcomes(failed=1)
def test_mtime_caching(self, testdir, example):
testdir.tmpdir.ensure("hello.py")
result = run_pytest(testdir)
result.stdout.fnmatch_lines([
# "*plugins*flake8*",
"*W293*",
"*W292*",
])
result.assert_outcomes(passed=1, failed=1)
result = run_pytest(testdir)
result.stdout.fnmatch_lines([
"*W293*",
"*W292*",
])
result.assert_outcomes(skipped=1, failed=1)
testdir.makeini("""
[pytest]
flake8-ignore = *.py W293 W292 W391
""")
result = run_pytest(testdir)
result.assert_outcomes(passed=2)
def test_extensions(testdir):
testdir.makeini("""
[pytest]
markers = flake8
flake8-extensions = .py .pyx
""")
testdir.makefile(
".pyx",
"""
@cfunc
def f():
pass
""",
)
result = run_pytest(testdir)
result.stdout.fnmatch_lines([
"*collected 1*",
])
result.assert_outcomes(failed=1)
def test_ok_verbose(testdir):
p = testdir.makepyfile("""
class AClass:
pass
""")
p = p.write(p.read() + "\n")
result = run_pytest(testdir, "--verbose")
result.stdout.fnmatch_lines([
"*test_ok_verbose*",
])
result.assert_outcomes(passed=1)
def test_keyword_match(testdir):
testdir.makepyfile("""
def test_hello():
a=[ 1,123]
#
""")
result = run_pytest(testdir, "-mflake8")
result.stdout.fnmatch_lines([
"*E201*",
"*1 failed*",
])
result.assert_outcomes(failed=1)
def test_run_on_init_file(testdir):
d = testdir.mkpydir("tests")
result = run_pytest(testdir, d / "__init__.py")
result.assert_outcomes(passed=1)
@pytest.mark.xfail("sys.platform == 'win32'")
def test_unicode_error(testdir):
x = testdir.tmpdir.join("x.py")
content = textwrap.dedent("""
# coding=utf8
accent_map = {
u'\\xc0': 'a', # À -> a non-ascii comment crashes it
}
""").lstrip()
x.write_text(content, encoding='utf-8')
# result = run_pytest(testdir, x, "-s")
# result.stdout.fnmatch_lines("*non-ascii comment*")
@pytest.mark.xfail(reason="flake8 is not properly registered as a marker")
def test_strict(testdir):
testdir.makepyfile("")
result = testdir.runpytest("-p", "no:enabler", "--strict", "-mflake8")
result.assert_outcomes(passed=1)
def test_junit_classname(testdir):
testdir.makepyfile("")
result = run_pytest(testdir, "--junit-xml=TEST.xml")
junit = testdir.tmpdir.join("TEST.xml")
j_text = pathlib.Path(junit).read_text(encoding='utf-8')
result.assert_outcomes(passed=1)
assert 'classname=""' not in j_text