diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2023-03-24 22:24:37 +0100 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2023-03-28 21:48:17 +0200 |
commit | 178c7c63fa0625e70119983daca95b809e97289a (patch) | |
tree | 959014665e981062e3b8aa82c6b099f8b73ffa30 | |
parent | bb069b8b2841f190124248f19b11a4258dab4c4f (diff) | |
download | pylint-git-178c7c63fa0625e70119983daca95b809e97289a.tar.gz |
[testutil] Do not count files with leading underscores
-rw-r--r-- | pylint/testutils/functional/find_functional_tests.py | 24 | ||||
-rw-r--r-- | tests/testutils/test_functional_testutils.py | 9 | ||||
-rw-r--r-- | tests/testutils/test_lint_module_output_update.py | 5 |
3 files changed, 22 insertions, 16 deletions
diff --git a/pylint/testutils/functional/find_functional_tests.py b/pylint/testutils/functional/find_functional_tests.py index 7473537a8..d2fed0319 100644 --- a/pylint/testutils/functional/find_functional_tests.py +++ b/pylint/testutils/functional/find_functional_tests.py @@ -64,20 +64,27 @@ def _check_functional_tests_structure( files: set[Path] = set() dirs: set[Path] = set() + def _get_files_from_dir( + path: Path, violations: list[tuple[Path, int]] + ) -> list[Path]: + """Return directories and files from a directory and handles violations.""" + files_without_leading_underscore = list( + p for p in path.iterdir() if not p.stem.startswith("_") + ) + if len(files_without_leading_underscore) > max_file_per_directory: + violations.append((path, len(files_without_leading_underscore))) + return files_without_leading_underscore + def walk(path: Path) -> Iterator[Path]: violations: list[tuple[Path, int]] = [] 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))) + parent_dir_files = _get_files_from_dir(path, violations) 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) > max_file_per_directory: - violations.append((_file_or_dir, len(_files))) + _files = _get_files_from_dir(_file_or_dir, violations) yield _file_or_dir.resolve() try: yield from walk(_file_or_dir) @@ -85,7 +92,7 @@ def _check_functional_tests_structure( violations_msgs.add(str(e).replace(error_msg, "")) else: yield _file_or_dir.resolve() - if violations: + if violations or violations_msgs: _msg = error_msg for offending_file, number in violations: _msg += f"- {offending_file}: {number} when the max is {max_file_per_directory}\n" @@ -95,8 +102,6 @@ def _check_functional_tests_structure( # Collect all sub-directories and files in directory for file_or_dir in walk(directory): - if file_or_dir.stem.startswith("_"): - continue if file_or_dir.is_dir(): dirs.add(file_or_dir) elif file_or_dir.suffix == ".py": @@ -116,6 +121,7 @@ def _check_functional_tests_structure( ): if not file.stem.startswith(file.parent.stem): misplaced_file.append(file) + if directory_does_not_exists or misplaced_file: msg = "The following functional tests are disorganized:\n" for file, possible_dir in directory_does_not_exists: diff --git a/tests/testutils/test_functional_testutils.py b/tests/testutils/test_functional_testutils.py index 16732df16..d4a7a0ce1 100644 --- a/tests/testutils/test_functional_testutils.py +++ b/tests/testutils/test_functional_testutils.py @@ -48,8 +48,7 @@ def test_get_functional_test_files_from_directory() -> None: "using_dir.py should go in a directory that starts with the " "first letters of 'using_dir'" ) - with pytest.raises(AssertionError): - exc_info.match("incredibly_bold_mischief.py") + assert "incredibly_bold_mischief.py" not in str(exc_info.value) # Leading underscore mean that this should not fail the assertion get_functional_test_files_from_directory(DATA_DIRECTORY / "u/_no_issue_here") @@ -64,10 +63,10 @@ def test_get_functional_test_files_from_crowded_directory() -> None: 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 + DATA_DIRECTORY / "m", max_file_per_directory=3 ) - assert exc_info.match("m: 4 when the max is 2") - assert exc_info.match("max_overflow: 3 when the max is 2") + assert exc_info.match("m: 4 when the max is 3") + assert "max_overflow" not in str(exc_info.value) def test_minimal_messages_config_enabled(pytest_config: MagicMock) -> None: diff --git a/tests/testutils/test_lint_module_output_update.py b/tests/testutils/test_lint_module_output_update.py index 4874c9713..04c51d884 100644 --- a/tests/testutils/test_lint_module_output_update.py +++ b/tests/testutils/test_lint_module_output_update.py @@ -18,7 +18,6 @@ from pylint.testutils.functional import LintModuleOutputUpdate FIXTURE_DIRECTORY = Path(__file__).parent / "data/functional" DIRECTORIES = list(FIXTURE_DIRECTORY.iterdir()) -DIRECTORIES_NAMES = [d.name for d in DIRECTORIES] @pytest.fixture() @@ -88,7 +87,9 @@ def test_lint_module_output_update_remove_useless_txt( not PY38_PLUS or (IS_PYPY and not PY39_PLUS), reason="Requires accurate 'end_col' value to update output", ) -@pytest.mark.parametrize("directory_path", DIRECTORIES, ids=DIRECTORIES_NAMES) +@pytest.mark.parametrize( + "directory_path", DIRECTORIES, ids=[str(p) for p in DIRECTORIES] +) def test_update_of_functional_output(directory_path: Path, tmp_path: Path) -> None: """Functional test for the functional tests' helper.""" |