File: test_pytest_flake8_path.py

package info (click to toggle)
pytest-flake8-path 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: python: 151; makefile: 3
file content (139 lines) | stat: -rw-r--r-- 3,396 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
from __future__ import annotations

import os
from textwrap import dedent

import flake8

from pytest_flake8_path import Flake8Result

# Prevent Flake8 from running in dev mode because it returns some problems
os.environ.pop("PYTHONDEVMODE", None)


def test_windows_path_normalization():
    result = Flake8Result(
        out=(
            ".\\example.py:1:2: E221 multiple spaces before operator\n"
            + "Flake8 is here!\n"
        ),
        err="",
        exit_code=0,
    )
    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator",
        "Flake8 is here!",
    ]


def test_simple_run(flake8_path):
    (flake8_path / "example.py").write_text("x  = 1\n")

    result = flake8_path.run_flake8()

    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator"
    ]
    assert result.err_lines == []
    assert result.exit_code == 1


def test_multi_line(flake8_path):
    (flake8_path / "example.py").write_text(
        dedent(
            """\
            x  = 1
            y  = 2
            """
        )
    )

    result = flake8_path.run_flake8()

    assert result.out_lines == [
        "./example.py:1:2: E221 multiple spaces before operator",
        "./example.py:2:2: E221 multiple spaces before operator",
    ]
    assert result.err_lines == []
    assert result.exit_code == 1


def test_with_setup_cfg(flake8_path):
    (flake8_path / "setup.cfg").write_text(
        dedent(
            """\
            [flake8]
            ignore = E221
            """
        )
    )
    (flake8_path / "example.py").write_text("x  = 1\n")

    result = flake8_path.run_flake8()

    assert result.out_lines == []
    assert result.err_lines == []
    assert result.exit_code == 0


def test_error_output(flake8_path):
    (flake8_path / "setup.cfg").write_text(
        dedent(
            """
            [flake8]
            extend-select = MC1

            [flake8:local-plugins]
            extension =
                MC1 = example:MyChecker1
            paths = .
            """
        )
    )
    (flake8_path / "example.py").write_text(
        dedent(
            """
            import warnings

            warnings.warn("This is a warning!", UserWarning)


            class MyChecker1:
                name = "MyChecker1"
                version = "1"

                def __init__(self, tree):
                    self.tree = tree

                def run(self):
                    if False:
                        yield
            """
        )
    )

    result = flake8_path.run_flake8()

    assert "This is a warning!" in result.err
    assert result.err_lines[0].endswith("UserWarning: This is a warning!")
    assert result.err_lines[1] == '  warnings.warn("This is a warning!", UserWarning)'


def test_extra_args(flake8_path):
    (flake8_path / "example.py").write_text("x  = 1\n")

    result = flake8_path.run_flake8(["--ignore", "E221"])

    assert result.out_lines == []
    assert result.err_lines == []
    assert result.exit_code == 0


def test_extra_args_version(flake8_path):
    result = flake8_path.run_flake8(extra_args=["--version"])
    assert result.out.startswith(flake8.__version__ + " ")


def test_separate_tmp_path(flake8_path, tmp_path):
    (flake8_path / "example.py").write_text("x  = 1\n")
    assert not (tmp_path / "example.py").exists()