diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b1a10940..d7650df6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,8 @@ Unreleased ---------- * Allow configuring sibling layer independence. +* Fix bug where a warning would be logged if a non-Python file with multiple dots + in the filename was encountered. 3.1 (2023-10-13) ---------------- diff --git a/src/grimp/adaptors/modulefinder.py b/src/grimp/adaptors/modulefinder.py index 7acaf25a..36d51a59 100644 --- a/src/grimp/adaptors/modulefinder.py +++ b/src/grimp/adaptors/modulefinder.py @@ -74,6 +74,9 @@ def _is_python_file(self, filename: str, dirpath: str) -> bool: if filename.startswith("."): return False + if not filename.endswith(".py"): + return False + # Ignore files like some.module.py. if filename.count(".") > 1: logger.warning( @@ -82,7 +85,7 @@ def _is_python_file(self, filename: str, dirpath: str) -> bool: ) return False - return filename.endswith(".py") + return True def _module_name_from_filename( self, package_name: str, filename_and_path: str, package_directory: str diff --git a/tests/unit/adaptors/test_modulefinder.py b/tests/unit/adaptors/test_modulefinder.py index bf6cdb83..9d7a04c1 100644 --- a/tests/unit/adaptors/test_modulefinder.py +++ b/tests/unit/adaptors/test_modulefinder.py @@ -2,6 +2,7 @@ from grimp.application.ports.modulefinder import FoundPackage, ModuleFile from grimp.domain.valueobjects import Module from tests.adaptors.filesystem import DEFAULT_MTIME, FakeFileSystem +import pytest def test_happy_path(): @@ -123,18 +124,25 @@ def test_ignores_orphaned_python_files(): ) -def test_ignores_dotted_python_files(caplog): +@pytest.mark.parametrize( + "extension, should_warn", + ( + ("py", True), + ("txt", False), # Anything other than .py. + ), +) +def test_ignores_dotted_python_files(extension, should_warn, caplog): # Python files containing dots (other than the one before .py) should not be discovered. module_finder = ModuleFinder() file_system = FakeFileSystem( - contents=""" + contents=f""" /path/to/mypackage/ __init__.py foo.py bar/ __init__.py - baz.dotted.py + baz.dotted.{extension} """ ) @@ -155,12 +163,15 @@ def test_ignores_dotted_python_files(caplog): } ), ) - assert caplog.messages == [ - ( - "Warning: skipping module with too many dots in the name: " - "/path/to/mypackage/bar/baz.dotted.py" - ) - ] + if should_warn: + assert caplog.messages == [ + ( + "Warning: skipping module with too many dots in the name: " + f"/path/to/mypackage/bar/baz.dotted.{extension}" + ) + ] + else: + assert caplog.messages == [] def test_ignores_hidden_directories():