From 34980949f39d72d7337cc1554de5adc301d299c0 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 6 Apr 2024 12:24:58 -0300 Subject: [PATCH] Change issue to improvement --- ...12112.bugfix.rst => 12112.improvement.rst} | 0 testing/test_pathlib.py | 34 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) rename changelog/{12112.bugfix.rst => 12112.improvement.rst} (100%) diff --git a/changelog/12112.bugfix.rst b/changelog/12112.improvement.rst similarity index 100% rename from changelog/12112.bugfix.rst rename to changelog/12112.improvement.rst diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index 487d47254..d83153d56 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -1129,22 +1129,46 @@ def test_safe_exists(tmp_path: Path) -> None: class TestNamespacePackages: """Test import_path support when importing from properly namespace packages.""" + @pytest.fixture(autouse=True) + def setup_imports_tracking(self, monkeypatch: MonkeyPatch) -> None: + monkeypatch.setattr(sys, "pytest_namespace_packages_test", [], raising=False) + def setup_directories( self, tmp_path: Path, monkeypatch: Optional[MonkeyPatch], pytester: Pytester ) -> Tuple[Path, Path]: + # Use a code to guard against modules being imported more than once. + # This is a safeguard in case future changes break this invariant. + code = dedent( + """ + import sys + imported = getattr(sys, "pytest_namespace_packages_test", []) + assert __name__ not in imported, f"{__name__} already imported" + imported.append(__name__) + sys.pytest_namespace_packages_test = imported + """ + ) + # Set up a namespace package "com.company", containing # two subpackages, "app" and "calc". (tmp_path / "src/dist1/com/company/app/core").mkdir(parents=True) - (tmp_path / "src/dist1/com/company/app/__init__.py").touch() - (tmp_path / "src/dist1/com/company/app/core/__init__.py").touch() + (tmp_path / "src/dist1/com/company/app/__init__.py").write_text( + code, encoding="UTF-8" + ) + (tmp_path / "src/dist1/com/company/app/core/__init__.py").write_text( + code, encoding="UTF-8" + ) models_py = tmp_path / "src/dist1/com/company/app/core/models.py" models_py.touch() (tmp_path / "src/dist2/com/company/calc/algo").mkdir(parents=True) - (tmp_path / "src/dist2/com/company/calc/__init__.py").touch() - (tmp_path / "src/dist2/com/company/calc/algo/__init__.py").touch() + (tmp_path / "src/dist2/com/company/calc/__init__.py").write_text( + code, encoding="UTF-8" + ) + (tmp_path / "src/dist2/com/company/calc/algo/__init__.py").write_text( + code, encoding="UTF-8" + ) algorithms_py = tmp_path / "src/dist2/com/company/calc/algo/algorithms.py" - algorithms_py.touch() + algorithms_py.write_text(code, encoding="UTF-8") # Validate the namespace package by importing it in a Python subprocess. r = validate_namespace_package(