File: test_discover_files.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 (166 lines) | stat: -rw-r--r-- 4,846 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
from __future__ import annotations

import os.path

import pytest

from flake8 import utils
from flake8.discover_files import _filenames_from
from flake8.discover_files import expand_paths


@pytest.fixture
def files_dir(tmpdir):
    """Create test dir for testing filenames_from."""
    with tmpdir.as_cwd():
        tmpdir.join("a/b/c.py").ensure()
        tmpdir.join("a/b/d.py").ensure()
        tmpdir.join("a/b/e/f.py").ensure()
        yield tmpdir


def _noop(path):
    return False


def _normpath(s):
    return s.replace("/", os.sep)


def _normpaths(pths):
    return {_normpath(pth) for pth in pths}


@pytest.mark.usefixtures("files_dir")
def test_filenames_from_a_directory():
    """Verify that filenames_from walks a directory."""
    filenames = set(_filenames_from(_normpath("a/b/"), predicate=_noop))
    # should include all files
    expected = _normpaths(("a/b/c.py", "a/b/d.py", "a/b/e/f.py"))
    assert filenames == expected


@pytest.mark.usefixtures("files_dir")
def test_filenames_from_a_directory_with_a_predicate():
    """Verify that predicates filter filenames_from."""
    filenames = set(
        _filenames_from(
            arg=_normpath("a/b/"),
            predicate=lambda path: path.endswith(_normpath("b/c.py")),
        )
    )
    # should not include c.py
    expected = _normpaths(("a/b/d.py", "a/b/e/f.py"))
    assert filenames == expected


@pytest.mark.usefixtures("files_dir")
def test_filenames_from_a_directory_with_a_predicate_from_the_current_dir():
    """Verify that predicates filter filenames_from."""
    filenames = set(
        _filenames_from(
            arg=_normpath("./a/b"),
            predicate=lambda path: path == "c.py",
        )
    )
    # none should have matched the predicate so all returned
    expected = _normpaths(("./a/b/c.py", "./a/b/d.py", "./a/b/e/f.py"))
    assert filenames == expected


@pytest.mark.usefixtures("files_dir")
def test_filenames_from_a_single_file():
    """Verify that we simply yield that filename."""
    filenames = set(_filenames_from(_normpath("a/b/c.py"), predicate=_noop))
    assert filenames == {_normpath("a/b/c.py")}


def test_filenames_from_a_single_file_does_not_exist():
    """Verify that a passed filename which does not exist is returned back."""
    filenames = set(_filenames_from(_normpath("d/n/e.py"), predicate=_noop))
    assert filenames == {_normpath("d/n/e.py")}


def test_filenames_from_exclude_doesnt_exclude_directory_names(tmpdir):
    """Verify that we don't greedily exclude subdirs."""
    tmpdir.join("1/dont_return_me.py").ensure()
    tmpdir.join("2/1/return_me.py").ensure()
    exclude = [tmpdir.join("1").strpath]

    def predicate(pth):
        return utils.fnmatch(os.path.abspath(pth), exclude)

    with tmpdir.as_cwd():
        filenames = list(_filenames_from(".", predicate=predicate))
    assert filenames == [os.path.join(".", "2", "1", "return_me.py")]


def test_filenames_from_predicate_applies_to_initial_arg(tmp_path):
    """Test that the predicate is also applied to the passed argument."""
    fname = str(tmp_path.joinpath("f.py"))
    ret = tuple(_filenames_from(fname, predicate=lambda _: True))
    assert ret == ()


def test_filenames_from_predicate_applies_to_dirname(tmp_path):
    """Test that the predicate can filter whole directories."""
    a_dir = tmp_path.joinpath("a")
    a_dir.mkdir()
    a_dir.joinpath("b.py").touch()

    b_py = tmp_path.joinpath("b.py")
    b_py.touch()

    def predicate(p):
        # filter out the /a directory
        return p.endswith("a")

    ret = tuple(_filenames_from(str(tmp_path), predicate=predicate))
    assert ret == (str(b_py),)


def _expand_paths(
    *,
    paths=(".",),
    stdin_display_name="stdin",
    filename_patterns=("*.py",),
    exclude=(),
):
    return set(
        expand_paths(
            paths=paths,
            stdin_display_name=stdin_display_name,
            filename_patterns=filename_patterns,
            exclude=exclude,
        )
    )


@pytest.mark.usefixtures("files_dir")
def test_expand_paths_honors_exclude():
    expected = _normpaths(("./a/b/c.py", "./a/b/e/f.py"))
    assert _expand_paths(exclude=["d.py"]) == expected


@pytest.mark.usefixtures("files_dir")
def test_expand_paths_defaults_to_dot():
    expected = _normpaths(("./a/b/c.py", "./a/b/d.py", "./a/b/e/f.py"))
    assert _expand_paths(paths=()) == expected


def test_default_stdin_name_is_not_filtered():
    assert _expand_paths(paths=("-",)) == {"-"}


def test_alternate_stdin_name_is_filtered():
    ret = _expand_paths(
        paths=("-",),
        stdin_display_name="wat",
        exclude=("wat",),
    )
    assert ret == set()


def test_filename_included_even_if_not_matching_include(tmp_path):
    some_file = str(tmp_path.joinpath("some/file"))
    assert _expand_paths(paths=(some_file,)) == {some_file}