File: test_flake8.py

package info (click to toggle)
python-pytest-flake8 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 140 kB
  • sloc: python: 370; sh: 5; makefile: 4
file content (187 lines) | stat: -rw-r--r-- 5,220 bytes parent folder | download
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
# coding=utf8
"""Unit tests for flake8 pytest plugin."""
from __future__ import print_function

import py
import pytest

pytest_plugins = "pytester",


def test_version():
    """Verify we can get version."""
    import pytest_flake8
    assert pytest_flake8.__version__


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

    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 = testdir.runpytest("--flake8", "-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 = testdir.runpytest("--flake8", "-s")
        result.assert_outcomes(passed=1)
        result.stdout.fnmatch_lines([
            "*collected 1*",
            "*xy.py .*",
            "*1 passed*",
        ])

    def test_w293w292(self, testdir, example):
        result = testdir.runpytest("--flake8", )
        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 = testdir.runpytest("--flake8", )
        result.stdout.fnmatch_lines([
            # "*plugins*flake8*",
            "*W293*",
            "*W292*",
        ])
        result.assert_outcomes(passed=1, failed=1)
        result = testdir.runpytest("--flake8", )
        result.stdout.fnmatch_lines([
            "*W293*",
            "*W292*",
        ])
        result.assert_outcomes(skipped=1, failed=1)
        testdir.makeini("""
            [pytest]
            flake8-ignore = *.py W293 W292 W391
        """)
        result = testdir.runpytest("--flake8", )
        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 = testdir.runpytest("--flake8")
    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 = testdir.runpytest("--flake8", "--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 = testdir.runpytest("--flake8", "-mflake8")
    result.stdout.fnmatch_lines([
        "*E201*",
        "*1 failed*",
    ])
    result.assert_outcomes(failed=1)


@pytest.mark.xfail("sys.platform == 'win32'")
def test_unicode_error(testdir):
    x = testdir.tmpdir.join("x.py")
    import codecs
    f = codecs.open(str(x), "w", encoding="utf8")
    f.write(py.builtin._totext("""
# coding=utf8

accent_map = {
    u'\\xc0': 'a',  # À -> a  non-ascii comment crashes it
}
""", "utf8"))
    f.close()
    # result = testdir.runpytest("--flake8", 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("--strict", "-mflake8")
    result.assert_outcomes(passed=1)


def test_junit_classname(testdir):
    testdir.makepyfile("")
    result = testdir.runpytest("--flake8", "--junit-xml=TEST.xml")
    junit = testdir.tmpdir.join("TEST.xml")
    with open(str(junit)) as j_file:
        j_text = j_file.read()
    result.assert_outcomes(passed=1)
    assert 'classname=""' not in j_text