Skip to content

Commit

Permalink
fix tests; avoid skipped file exploration
Browse files Browse the repository at this point in the history
  • Loading branch information
purajit committed Dec 15, 2024
1 parent b29b3b6 commit 54aeecd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
27 changes: 8 additions & 19 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,39 +615,31 @@ def initialize(self) -> None:
if not msg.may_be_emitted(self.config.py_version):
self._msgs_state[msg.msgid] = False

def _discover_files(
self, files_or_modules: Sequence[str], all_files: bool = False
) -> Iterator[str]:
def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]:
"""Discover python modules and packages in sub-directory.
:param Sequence[str] files_or_modules: list of directories to explore
:param str all_files: whether to return _all_ files, entering modules
and not considering ignored paths
Returns iterator of paths to discovered modules and packages.
"""
for something in files_or_modules:
if os.path.isdir(something):
if not all_files and not os.path.isfile(
os.path.join(something, "__init__.py")
):
continue
if os.path.isdir(something) and not os.path.isfile(
os.path.join(something, "__init__.py")
):
skip_subtrees: list[str] = []
for root, _, files in os.walk(something):
if any(root.startswith(s) for s in skip_subtrees):
# Skip subtree of already discovered package.
continue

if not all_files and _is_ignored_file(
if _is_ignored_file(
root,
self.config.ignore,
self.config.ignore_patterns,
self.config.ignore_paths,
):
self.skipped_paths.add(root)
skip_subtrees.append(root)
continue

if not all_files and "__init__.py" in files:
if "__init__.py" in files:
skip_subtrees.append(root)
yield root
else:
Expand Down Expand Up @@ -1156,11 +1148,8 @@ def _report_evaluation(self, verbose: bool = False) -> int | None:

if verbose:
checked_files_count = self.stats.node_count["module"]
skipped_files = list(
self._discover_files(list(self.skipped_paths), all_files=True)
)
skipped_files_count = len(skipped_files)
msg += f"\nChecked {checked_files_count} files, skipped {skipped_files_count} files"
skipped_paths_count = len(self.skipped_paths)
msg += f"\nChecked {checked_files_count} files, skipped {skipped_paths_count} files/modules"

if self.config.score:
sect = report_nodes.EvaluationSection(msg)
Expand Down
14 changes: 13 additions & 1 deletion tests/lint/unittest_expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": True,
"name": "lint.unittest_expand_modules",
"path": EXPAND_MODULES,
"isignored": False,
}

this_file_relative_to_parent = {
Expand All @@ -48,6 +49,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": True,
"name": "lint.unittest_expand_modules",
"path": EXPAND_MODULES_BASE,
"isignored": False,
}

this_file_from_init = {
Expand All @@ -56,6 +58,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"name": "lint.unittest_expand_modules",
"path": EXPAND_MODULES,
"isignored": False,
}

this_file_from_init_deduplicated = {
Expand All @@ -64,6 +67,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": True,
"name": "lint.unittest_expand_modules",
"path": EXPAND_MODULES,
"isignored": False,
}

unittest_lint = {
Expand All @@ -72,6 +76,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"name": "lint.unittest_lint",
"path": str(TEST_DIRECTORY / "lint/unittest_lint.py"),
"isignored": False,
}

test_utils = {
Expand All @@ -80,6 +85,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"name": "lint.test_utils",
"path": str(TEST_DIRECTORY / "lint/test_utils.py"),
"isignored": False,
}

test_run_pylint = {
Expand All @@ -88,6 +94,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"name": "lint.test_run_pylint",
"path": str(TEST_DIRECTORY / "lint/test_run_pylint.py"),
"isignored": False,
}

test_pylinter = {
Expand All @@ -96,6 +103,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"name": "lint.test_pylinter",
"path": str(TEST_DIRECTORY / "lint/test_pylinter.py"),
"isignored": False,
}

test_caching = {
Expand All @@ -104,6 +112,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"name": "lint.test_caching",
"path": str(TEST_DIRECTORY / "lint/test_caching.py"),
"isignored": False,
}

init_of_package = {
Expand All @@ -112,6 +121,7 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": True,
"name": "lint",
"path": INIT_PATH,
"isignored": False,
}

# A directory that is not a python package.
Expand All @@ -123,13 +133,15 @@ def test__is_in_ignore_list_re_match() -> None:
"isarg": False,
"basepath": str(REPORTERS_PATH / "__init__.py"),
"basename": "reporters",
"isignored": False,
},
str(REPORTERS_PATH / "unittest_reporting.py"): {
"path": str(REPORTERS_PATH / "unittest_reporting.py"),
"name": "reporters.unittest_reporting",
"isarg": False,
"basepath": str(REPORTERS_PATH / "__init__.py"),
"basename": "reporters",
"isignored": False,
},
}

Expand Down Expand Up @@ -304,5 +316,5 @@ def test_expand_modules_with_ignore(
ignore_list_re,
self.linter.config.ignore_paths,
)
assert modules == expected
assert {k: v for k, v in modules.items() if not v["isignored"]} == expected
assert not errors
2 changes: 1 addition & 1 deletion tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def test_output_with_verbose(self) -> None:
out=out,
code=4,
)
assert "Checked 1 files, skipped 1 files" in out.getvalue().strip()
assert "Checked 1 files, skipped 1 files/modules" in out.getvalue().strip()

def test_no_out_encoding(self) -> None:
"""Test redirection of stdout with non ascii characters."""
Expand Down

0 comments on commit 54aeecd

Please sign in to comment.