Skip to content

Commit

Permalink
[testutil] Display recursive call for crowded functional test dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Mar 23, 2023
1 parent 9b4a883 commit 31c8ff0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
29 changes: 22 additions & 7 deletions pylint/testutils/functional/find_functional_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@

def get_functional_test_files_from_directory(
input_dir: Path | str,
max_file_per_directory: int = REASONABLY_DISPLAYABLE_VERTICALLY,
) -> list[FunctionalTestFile]:
"""Get all functional tests in the input_dir."""
suite = []

_check_functional_tests_structure(Path(input_dir))
_check_functional_tests_structure(Path(input_dir), max_file_per_directory)

for dirpath, dirnames, filenames in os.walk(input_dir):
if dirpath.endswith("__pycache__"):
Expand All @@ -50,7 +51,9 @@ def get_functional_test_files_from_directory(
return suite


def _check_functional_tests_structure(directory: Path) -> None:
def _check_functional_tests_structure(
directory: Path, max_file_per_directory: int
) -> None:
"""Check if test directories follow correct file/folder structure.
Ignore underscored directories or files.
Expand All @@ -63,19 +66,31 @@ def _check_functional_tests_structure(directory: Path) -> None:

def walk(path: Path) -> Iterator[Path]:
violations: list[tuple[Path, int]] = []
for _file_or_dir in path.iterdir():
violations_msgs: set[str] = set()
parent_dir_files = list(path.iterdir())
if len(parent_dir_files) > max_file_per_directory:
violations.append((path, len(parent_dir_files)))
error_msg = (
"The following directory contains too many functional tests files:\n"
)
for _file_or_dir in parent_dir_files:
if _file_or_dir.is_dir():
_files = list(_file_or_dir.iterdir())
if len(_files) > REASONABLY_DISPLAYABLE_VERTICALLY:
if len(_files) > max_file_per_directory:
violations.append((_file_or_dir, len(_files)))
yield _file_or_dir
yield from walk(_file_or_dir)
try:
yield from walk(_file_or_dir)
except AssertionError as e:
violations_msgs.add(str(e).replace(error_msg, ""))
else:
yield _file_or_dir.resolve()
if violations:
_msg = "The following directory contains too many functional tests files:\n"
_msg = error_msg
for offending_file, number in violations:
_msg += f"- {offending_file}: ({number}) > {REASONABLY_DISPLAYABLE_VERTICALLY}\n"
_msg += f"- {offending_file}: {number} when the max is {max_file_per_directory}\n"
for error_msg in violations_msgs:
_msg += error_msg
raise AssertionError(_msg)

# Collect all sub-directories and files in directory
Expand Down
Empty file.
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions tests/testutils/test_functional_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def test_get_functional_test_files_from_directory() -> None:
get_functional_test_files_from_directory(DATA_DIRECTORY / "u/_no_issue_here")


def test_get_functional_test_files_from_crowded_directory() -> None:
"""Test that we correctly check the functional test directory structures."""
with pytest.raises(AssertionError) as exc_info:
get_functional_test_files_from_directory(
DATA_DIRECTORY / "m", max_file_per_directory=1
)
assert exc_info.match("m: 4 when the max is 1")
assert exc_info.match("max_overflow: 3 when the max is 1")
with pytest.raises(AssertionError) as exc_info:
get_functional_test_files_from_directory(
DATA_DIRECTORY / "m", max_file_per_directory=2
)
assert exc_info.match("m: 4 when the max is 2")
assert exc_info.match("max_overflow: 3 when the max is 2")


def test_minimal_messages_config_enabled(pytest_config: MagicMock) -> None:
"""Test that all messages not targeted in the functional test are disabled
when running with --minimal-messages-config.
Expand Down

0 comments on commit 31c8ff0

Please sign in to comment.