|
9 | 9 | from typing import Any
|
10 | 10 | from typing import Generator
|
11 | 11 | from typing import Iterator
|
| 12 | +from typing import Tuple |
12 | 13 | import unittest.mock
|
13 | 14 |
|
14 | 15 | from _pytest.monkeypatch import MonkeyPatch
|
15 | 16 | from _pytest.pathlib import bestrelpath
|
16 | 17 | from _pytest.pathlib import commonpath
|
| 18 | +from _pytest.pathlib import CouldNotResolvePathError |
17 | 19 | from _pytest.pathlib import ensure_deletable
|
18 | 20 | from _pytest.pathlib import fnmatch_ex
|
19 | 21 | from _pytest.pathlib import get_extended_length_path_str
|
|
25 | 27 | from _pytest.pathlib import maybe_delete_a_numbered_dir
|
26 | 28 | from _pytest.pathlib import module_name_from_path
|
27 | 29 | from _pytest.pathlib import resolve_package_path
|
| 30 | +from _pytest.pathlib import resolve_pkg_root_and_module_name |
28 | 31 | from _pytest.pathlib import safe_exists
|
29 | 32 | from _pytest.pathlib import symlink_or_skip
|
30 | 33 | from _pytest.pathlib import visit
|
|
33 | 36 | import pytest
|
34 | 37 |
|
35 | 38 |
|
| 39 | +@pytest.fixture(autouse=True) |
| 40 | +def autouse_pytester(pytester: Pytester) -> None: |
| 41 | + """ |
| 42 | + Fixture to make pytester() being autouse for all tests in this module. |
| 43 | +
|
| 44 | + pytester makes sure to restore sys.path to its previous state, and many tests in this module |
| 45 | + import modules and change sys.path because of that, so common module names such as "test" or "test.conftest" |
| 46 | + end up leaking to tests in other modules. |
| 47 | +
|
| 48 | + Note: we might consider extracting the sys.path restoration aspect into its own fixture, and apply it |
| 49 | + to the entire test suite always. |
| 50 | + """ |
| 51 | + |
| 52 | + |
36 | 53 | class TestFNMatcherPort:
|
37 | 54 | """Test our port of py.common.FNMatcher (fnmatch_ex)."""
|
38 | 55 |
|
@@ -596,6 +613,33 @@ def test_module_name_from_path(self, tmp_path: Path) -> None:
|
596 | 613 | )
|
597 | 614 | assert result == "_env_310.tests.test_foo"
|
598 | 615 |
|
| 616 | + def test_resolve_pkg_root_and_module_name( |
| 617 | + self, tmp_path: Path, monkeypatch: MonkeyPatch |
| 618 | + ) -> None: |
| 619 | + # Create a directory structure first without __init__.py files. |
| 620 | + (tmp_path / "src/app/core").mkdir(parents=True) |
| 621 | + models_py = tmp_path / "src/app/core/models.py" |
| 622 | + models_py.touch() |
| 623 | + with pytest.raises(CouldNotResolvePathError): |
| 624 | + _ = resolve_pkg_root_and_module_name(models_py) |
| 625 | + |
| 626 | + # Create the __init__.py files, it should now resolve to a proper module name. |
| 627 | + (tmp_path / "src/app/__init__.py").touch() |
| 628 | + (tmp_path / "src/app/core/__init__.py").touch() |
| 629 | + assert resolve_pkg_root_and_module_name(models_py) == ( |
| 630 | + tmp_path / "src", |
| 631 | + "app.core.models", |
| 632 | + ) |
| 633 | + |
| 634 | + # If we add tmp_path to sys.path, src becomes a namespace package. |
| 635 | + monkeypatch.syspath_prepend(tmp_path) |
| 636 | + assert resolve_pkg_root_and_module_name( |
| 637 | + models_py, consider_ns_packages=True |
| 638 | + ) == ( |
| 639 | + tmp_path, |
| 640 | + "src.app.core.models", |
| 641 | + ) |
| 642 | + |
599 | 643 | def test_insert_missing_modules(
|
600 | 644 | self, monkeypatch: MonkeyPatch, tmp_path: Path
|
601 | 645 | ) -> None:
|
@@ -741,3 +785,102 @@ def test_safe_exists(tmp_path: Path) -> None:
|
741 | 785 | side_effect=ValueError("name too long"),
|
742 | 786 | ):
|
743 | 787 | assert safe_exists(p) is False
|
| 788 | + |
| 789 | + |
| 790 | +class TestNamespacePackages: |
| 791 | + """Test import_path support when importing from properly namespace packages.""" |
| 792 | + |
| 793 | + def setup_directories( |
| 794 | + self, tmp_path: Path, monkeypatch: MonkeyPatch, pytester: Pytester |
| 795 | + ) -> Tuple[Path, Path]: |
| 796 | + # Set up a namespace package "com.company", containing |
| 797 | + # two subpackages, "app" and "calc". |
| 798 | + (tmp_path / "src/dist1/com/company/app/core").mkdir(parents=True) |
| 799 | + (tmp_path / "src/dist1/com/company/app/__init__.py").touch() |
| 800 | + (tmp_path / "src/dist1/com/company/app/core/__init__.py").touch() |
| 801 | + models_py = tmp_path / "src/dist1/com/company/app/core/models.py" |
| 802 | + models_py.touch() |
| 803 | + |
| 804 | + (tmp_path / "src/dist2/com/company/calc/algo").mkdir(parents=True) |
| 805 | + (tmp_path / "src/dist2/com/company/calc/__init__.py").touch() |
| 806 | + (tmp_path / "src/dist2/com/company/calc/algo/__init__.py").touch() |
| 807 | + algorithms_py = tmp_path / "src/dist2/com/company/calc/algo/algorithms.py" |
| 808 | + algorithms_py.touch() |
| 809 | + |
| 810 | + # Validate the namespace package by importing it in a Python subprocess. |
| 811 | + r = pytester.runpython_c( |
| 812 | + dedent( |
| 813 | + f""" |
| 814 | + import sys |
| 815 | + sys.path.append(r{str(tmp_path / "src/dist1")!r}) |
| 816 | + sys.path.append(r{str(tmp_path / "src/dist2")!r}) |
| 817 | + import com.company.app.core.models |
| 818 | + import com.company.calc.algo.algorithms |
| 819 | + """ |
| 820 | + ) |
| 821 | + ) |
| 822 | + assert r.ret == 0 |
| 823 | + |
| 824 | + monkeypatch.syspath_prepend(tmp_path / "src/dist1") |
| 825 | + monkeypatch.syspath_prepend(tmp_path / "src/dist2") |
| 826 | + return models_py, algorithms_py |
| 827 | + |
| 828 | + @pytest.mark.parametrize("import_mode", ["prepend", "append"]) |
| 829 | + def test_resolve_pkg_root_and_module_name_ns_multiple_levels( |
| 830 | + self, |
| 831 | + tmp_path: Path, |
| 832 | + monkeypatch: MonkeyPatch, |
| 833 | + pytester: Pytester, |
| 834 | + import_mode: str, |
| 835 | + ) -> None: |
| 836 | + models_py, algorithms_py = self.setup_directories( |
| 837 | + tmp_path, monkeypatch, pytester |
| 838 | + ) |
| 839 | + |
| 840 | + pkg_root, module_name = resolve_pkg_root_and_module_name( |
| 841 | + models_py, consider_ns_packages=True |
| 842 | + ) |
| 843 | + assert (pkg_root, module_name) == ( |
| 844 | + tmp_path / "src/dist1", |
| 845 | + "com.company.app.core.models", |
| 846 | + ) |
| 847 | + |
| 848 | + mod = import_path(models_py, mode=import_mode, root=tmp_path) |
| 849 | + assert mod.__name__ == "com.company.app.core.models" |
| 850 | + assert mod.__file__ == str(models_py) |
| 851 | + |
| 852 | + pkg_root, module_name = resolve_pkg_root_and_module_name( |
| 853 | + algorithms_py, consider_ns_packages=True |
| 854 | + ) |
| 855 | + assert (pkg_root, module_name) == ( |
| 856 | + tmp_path / "src/dist2", |
| 857 | + "com.company.calc.algo.algorithms", |
| 858 | + ) |
| 859 | + |
| 860 | + mod = import_path(algorithms_py, mode=import_mode, root=tmp_path) |
| 861 | + assert mod.__name__ == "com.company.calc.algo.algorithms" |
| 862 | + assert mod.__file__ == str(algorithms_py) |
| 863 | + |
| 864 | + @pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"]) |
| 865 | + def test_incorrect_namespace_package( |
| 866 | + self, |
| 867 | + tmp_path: Path, |
| 868 | + monkeypatch: MonkeyPatch, |
| 869 | + pytester: Pytester, |
| 870 | + import_mode: str, |
| 871 | + ) -> None: |
| 872 | + models_py, algorithms_py = self.setup_directories( |
| 873 | + tmp_path, monkeypatch, pytester |
| 874 | + ) |
| 875 | + # Namespace packages must not have an __init__.py at any of its |
| 876 | + # directories; if it does, we then fall back to importing just the |
| 877 | + # part of the package containing the __init__.py files. |
| 878 | + (tmp_path / "src/dist1/com/__init__.py").touch() |
| 879 | + |
| 880 | + pkg_root, module_name = resolve_pkg_root_and_module_name( |
| 881 | + models_py, consider_ns_packages=True |
| 882 | + ) |
| 883 | + assert (pkg_root, module_name) == ( |
| 884 | + tmp_path / "src/dist1/com/company", |
| 885 | + "app.core.models", |
| 886 | + ) |
0 commit comments