Skip to content

Commit

Permalink
🧪 Add a test for implicit ns src root lookup
Browse files Browse the repository at this point in the history
This patch modifies the `test_discover_package_path_source_root_as_*`
tests to also run against directory layouts with no exlicitly existing
`__init__.py` file.

They were added in #10036 and seem to be insufficient, not covering
PEP 420 implicit namespaces.
  • Loading branch information
webknjaz committed Jan 8, 2025
1 parent 6456374 commit d0fb075
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions tests/pyreverse/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ def test_project_root_in_sys_path() -> None:
assert sys.path == [PROJECT_ROOT_DIR]


def test_discover_package_path_source_root_as_parent(tmp_path: Any) -> None:
@pytest.mark.parametrize(
"py_mod_base_name",
("__init__", "impl"),
ids=("explicit-namespace", "implicit-namespace"),
)
def test_discover_package_path_source_root_as_parent(
py_mod_base_name: str,
tmp_path: Any,
) -> None:
"""Test discover_package_path when source root is a parent of the module."""
# Create this temporary structure:
# /tmp_path/
Expand All @@ -75,14 +83,22 @@ def test_discover_package_path_source_root_as_parent(tmp_path: Any) -> None:
project_dir = tmp_path / "project"
package_dir = project_dir / "mypackage"
package_dir.mkdir(parents=True)
(package_dir / "__init__.py").touch()
(package_dir / f"{py_mod_base_name}.py").touch()

# Test with project_dir as source root (parent of package)
result = discover_package_path(str(package_dir), [str(project_dir)])
assert result == str(project_dir)


def test_discover_package_path_source_root_as_child(tmp_path: Any) -> None:
@pytest.mark.parametrize(
"py_mod_base_name",
("__init__", "impl"),
ids=("explicit-namespace", "implicit-namespace"),
)
def test_discover_package_path_source_root_as_child(
py_mod_base_name: str,
tmp_path: Any,
) -> None:
"""Test discover_package_path when source root is a child of the module."""
# Create this temporary structure:
# /tmp_path/
Expand All @@ -94,7 +110,7 @@ def test_discover_package_path_source_root_as_child(tmp_path: Any) -> None:
src_dir = project_dir / "src"
package_dir = src_dir / "mypackage"
package_dir.mkdir(parents=True)
(package_dir / "__init__.py").touch()
(package_dir / f"{py_mod_base_name}.py").touch()

# Test with src_dir as source root (child of project)
result = discover_package_path(str(project_dir), [str(src_dir)])
Expand Down

0 comments on commit d0fb075

Please sign in to comment.