File: test_file_checker.py

package info (click to toggle)
python-flake8 7.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,212 kB
  • sloc: python: 6,592; sh: 21; makefile: 19
file content (65 lines) | stat: -rw-r--r-- 1,883 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
"""Unit tests for the FileChecker class."""
from __future__ import annotations

import argparse
import importlib.metadata
from unittest import mock

import pytest

import flake8
from flake8 import checker
from flake8.plugins import finder


@mock.patch("flake8.checker.FileChecker._make_processor", return_value=None)
def test_repr(*args):
    """Verify we generate a correct repr."""
    file_checker = checker.FileChecker(
        filename="example.py",
        plugins=finder.Checkers([], [], []),
        options=argparse.Namespace(),
    )
    assert repr(file_checker) == "FileChecker for example.py"


def test_nonexistent_file():
    """Verify that checking non-existent file results in an error."""
    c = checker.FileChecker(
        filename="example.py",
        plugins=finder.Checkers([], [], []),
        options=argparse.Namespace(),
    )

    assert c.processor is None
    assert not c.should_process
    assert len(c.results) == 1
    error = c.results[0]
    assert error[0] == "E902"


def test_raises_exception_on_failed_plugin(tmp_path, default_options):
    """Checks that a failing plugin results in PluginExecutionFailed."""
    fname = tmp_path.joinpath("t.py")
    fname.touch()
    plugin = finder.LoadedPlugin(
        finder.Plugin(
            "plugin-name",
            "1.2.3",
            importlib.metadata.EntryPoint("X", "dne:dne", "flake8.extension"),
        ),
        mock.Mock(side_effect=ValueError),
        {},
    )
    fchecker = checker.FileChecker(
        filename=str(fname),
        plugins=finder.Checkers([], [], []),
        options=default_options,
    )
    with pytest.raises(flake8.exceptions.PluginExecutionFailed) as excinfo:
        fchecker.run_check(plugin)
    expected = (
        f'{fname}: "plugin-name[X]" failed during execution '
        f"due to ValueError()"
    )
    assert str(excinfo.value) == expected