Skip to content

Commit 178c7c6

Browse files
[testutil] Do not count files with leading underscores
1 parent bb069b8 commit 178c7c6

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

pylint/testutils/functional/find_functional_tests.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,35 @@ def _check_functional_tests_structure(
6464
files: set[Path] = set()
6565
dirs: set[Path] = set()
6666

67+
def _get_files_from_dir(
68+
path: Path, violations: list[tuple[Path, int]]
69+
) -> list[Path]:
70+
"""Return directories and files from a directory and handles violations."""
71+
files_without_leading_underscore = list(
72+
p for p in path.iterdir() if not p.stem.startswith("_")
73+
)
74+
if len(files_without_leading_underscore) > max_file_per_directory:
75+
violations.append((path, len(files_without_leading_underscore)))
76+
return files_without_leading_underscore
77+
6778
def walk(path: Path) -> Iterator[Path]:
6879
violations: list[tuple[Path, int]] = []
6980
violations_msgs: set[str] = set()
70-
parent_dir_files = list(path.iterdir())
71-
if len(parent_dir_files) > max_file_per_directory:
72-
violations.append((path, len(parent_dir_files)))
81+
parent_dir_files = _get_files_from_dir(path, violations)
7382
error_msg = (
7483
"The following directory contains too many functional tests files:\n"
7584
)
7685
for _file_or_dir in parent_dir_files:
7786
if _file_or_dir.is_dir():
78-
_files = list(_file_or_dir.iterdir())
79-
if len(_files) > max_file_per_directory:
80-
violations.append((_file_or_dir, len(_files)))
87+
_files = _get_files_from_dir(_file_or_dir, violations)
8188
yield _file_or_dir.resolve()
8289
try:
8390
yield from walk(_file_or_dir)
8491
except AssertionError as e:
8592
violations_msgs.add(str(e).replace(error_msg, ""))
8693
else:
8794
yield _file_or_dir.resolve()
88-
if violations:
95+
if violations or violations_msgs:
8996
_msg = error_msg
9097
for offending_file, number in violations:
9198
_msg += f"- {offending_file}: {number} when the max is {max_file_per_directory}\n"
@@ -95,8 +102,6 @@ def walk(path: Path) -> Iterator[Path]:
95102

96103
# Collect all sub-directories and files in directory
97104
for file_or_dir in walk(directory):
98-
if file_or_dir.stem.startswith("_"):
99-
continue
100105
if file_or_dir.is_dir():
101106
dirs.add(file_or_dir)
102107
elif file_or_dir.suffix == ".py":
@@ -116,6 +121,7 @@ def walk(path: Path) -> Iterator[Path]:
116121
):
117122
if not file.stem.startswith(file.parent.stem):
118123
misplaced_file.append(file)
124+
119125
if directory_does_not_exists or misplaced_file:
120126
msg = "The following functional tests are disorganized:\n"
121127
for file, possible_dir in directory_does_not_exists:

tests/testutils/test_functional_testutils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def test_get_functional_test_files_from_directory() -> None:
4848
"using_dir.py should go in a directory that starts with the "
4949
"first letters of 'using_dir'"
5050
)
51-
with pytest.raises(AssertionError):
52-
exc_info.match("incredibly_bold_mischief.py")
51+
assert "incredibly_bold_mischief.py" not in str(exc_info.value)
5352
# Leading underscore mean that this should not fail the assertion
5453
get_functional_test_files_from_directory(DATA_DIRECTORY / "u/_no_issue_here")
5554

@@ -64,10 +63,10 @@ def test_get_functional_test_files_from_crowded_directory() -> None:
6463
assert exc_info.match("max_overflow: 3 when the max is 1")
6564
with pytest.raises(AssertionError) as exc_info:
6665
get_functional_test_files_from_directory(
67-
DATA_DIRECTORY / "m", max_file_per_directory=2
66+
DATA_DIRECTORY / "m", max_file_per_directory=3
6867
)
69-
assert exc_info.match("m: 4 when the max is 2")
70-
assert exc_info.match("max_overflow: 3 when the max is 2")
68+
assert exc_info.match("m: 4 when the max is 3")
69+
assert "max_overflow" not in str(exc_info.value)
7170

7271

7372
def test_minimal_messages_config_enabled(pytest_config: MagicMock) -> None:

tests/testutils/test_lint_module_output_update.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
FIXTURE_DIRECTORY = Path(__file__).parent / "data/functional"
2020
DIRECTORIES = list(FIXTURE_DIRECTORY.iterdir())
21-
DIRECTORIES_NAMES = [d.name for d in DIRECTORIES]
2221

2322

2423
@pytest.fixture()
@@ -88,7 +87,9 @@ def test_lint_module_output_update_remove_useless_txt(
8887
not PY38_PLUS or (IS_PYPY and not PY39_PLUS),
8988
reason="Requires accurate 'end_col' value to update output",
9089
)
91-
@pytest.mark.parametrize("directory_path", DIRECTORIES, ids=DIRECTORIES_NAMES)
90+
@pytest.mark.parametrize(
91+
"directory_path", DIRECTORIES, ids=[str(p) for p in DIRECTORIES]
92+
)
9293
def test_update_of_functional_output(directory_path: Path, tmp_path: Path) -> None:
9394
"""Functional test for the functional tests' helper."""
9495

0 commit comments

Comments
 (0)