|
3 | 3 | import os.path
|
4 | 4 | from pathlib import Path
|
5 | 5 | import pickle
|
| 6 | +import shutil |
6 | 7 | import sys
|
7 | 8 | from textwrap import dedent
|
8 | 9 | from types import ModuleType
|
@@ -720,6 +721,146 @@ def test_my_test():
|
720 | 721 | result = pytester.runpytest("--import-mode=importlib")
|
721 | 722 | result.stdout.fnmatch_lines("* 1 passed *")
|
722 | 723 |
|
| 724 | + def create_installed_doctests_and_tests_dir( |
| 725 | + self, path: Path, monkeypatch: MonkeyPatch |
| 726 | + ) -> Tuple[Path, Path, Path]: |
| 727 | + """ |
| 728 | + Create a directory structure where the application code is installed in a virtual environment, |
| 729 | + and the tests are in an outside ".tests" directory. |
| 730 | +
|
| 731 | + Return the paths to the core module (installed in the virtualenv), and the test modules. |
| 732 | + """ |
| 733 | + app = path / "src/app" |
| 734 | + app.mkdir(parents=True) |
| 735 | + (app / "__init__.py").touch() |
| 736 | + core_py = app / "core.py" |
| 737 | + core_py.write_text( |
| 738 | + dedent( |
| 739 | + """ |
| 740 | + def foo(): |
| 741 | + ''' |
| 742 | + >>> 1 + 1 |
| 743 | + 2 |
| 744 | + ''' |
| 745 | + """ |
| 746 | + ), |
| 747 | + encoding="ascii", |
| 748 | + ) |
| 749 | + |
| 750 | + # Install it into a site-packages directory, and add it to sys.path, mimicking what |
| 751 | + # happens when installing into a virtualenv. |
| 752 | + site_packages = path / ".env/lib/site-packages" |
| 753 | + site_packages.mkdir(parents=True) |
| 754 | + shutil.copytree(app, site_packages / "app") |
| 755 | + assert (site_packages / "app/core.py").is_file() |
| 756 | + |
| 757 | + monkeypatch.syspath_prepend(site_packages) |
| 758 | + |
| 759 | + # Create the tests files, outside 'src' and the virtualenv. |
| 760 | + # We use the same test name on purpose, but in different directories, to ensure |
| 761 | + # this works as advertised. |
| 762 | + conftest_path1 = path / ".tests/a/conftest.py" |
| 763 | + conftest_path1.parent.mkdir(parents=True) |
| 764 | + conftest_path1.write_text( |
| 765 | + dedent( |
| 766 | + """ |
| 767 | + import pytest |
| 768 | + @pytest.fixture |
| 769 | + def a_fix(): return "a" |
| 770 | + """ |
| 771 | + ), |
| 772 | + encoding="ascii", |
| 773 | + ) |
| 774 | + test_path1 = path / ".tests/a/test_core.py" |
| 775 | + test_path1.write_text( |
| 776 | + dedent( |
| 777 | + """ |
| 778 | + import app.core |
| 779 | + def test(a_fix): |
| 780 | + assert a_fix == "a" |
| 781 | + """, |
| 782 | + ), |
| 783 | + encoding="ascii", |
| 784 | + ) |
| 785 | + |
| 786 | + conftest_path2 = path / ".tests/b/conftest.py" |
| 787 | + conftest_path2.parent.mkdir(parents=True) |
| 788 | + conftest_path2.write_text( |
| 789 | + dedent( |
| 790 | + """ |
| 791 | + import pytest |
| 792 | + @pytest.fixture |
| 793 | + def b_fix(): return "b" |
| 794 | + """ |
| 795 | + ), |
| 796 | + encoding="ascii", |
| 797 | + ) |
| 798 | + |
| 799 | + test_path2 = path / ".tests/b/test_core.py" |
| 800 | + test_path2.write_text( |
| 801 | + dedent( |
| 802 | + """ |
| 803 | + import app.core |
| 804 | + def test(b_fix): |
| 805 | + assert b_fix == "b" |
| 806 | + """, |
| 807 | + ), |
| 808 | + encoding="ascii", |
| 809 | + ) |
| 810 | + return (site_packages / "app/core.py"), test_path1, test_path2 |
| 811 | + |
| 812 | + def test_import_using_normal_mechanism_first( |
| 813 | + self, monkeypatch: MonkeyPatch, pytester: Pytester |
| 814 | + ) -> None: |
| 815 | + """ |
| 816 | + Test import_path imports from the canonical location when possible first, only |
| 817 | + falling back to its normal flow when the module being imported is not reachable via sys.path (#11475). |
| 818 | + """ |
| 819 | + core_py, test_path1, test_path2 = self.create_installed_doctests_and_tests_dir( |
| 820 | + pytester.path, monkeypatch |
| 821 | + ) |
| 822 | + |
| 823 | + # core_py is reached from sys.path, so should be imported normally. |
| 824 | + mod = import_path(core_py, mode="importlib", root=pytester.path) |
| 825 | + assert mod.__name__ == "app.core" |
| 826 | + assert mod.__file__ and Path(mod.__file__) == core_py |
| 827 | + |
| 828 | + # tests are not reachable from sys.path, so they are imported as a standalone modules. |
| 829 | + # Instead of '.tests.a.test_core', we import as "_tests.a.test_core" because |
| 830 | + # importlib considers module names starting with '.' to be local imports. |
| 831 | + mod = import_path(test_path1, mode="importlib", root=pytester.path) |
| 832 | + assert mod.__name__ == "_tests.a.test_core" |
| 833 | + mod = import_path(test_path2, mode="importlib", root=pytester.path) |
| 834 | + assert mod.__name__ == "_tests.b.test_core" |
| 835 | + |
| 836 | + def test_import_using_normal_mechanism_first_integration( |
| 837 | + self, monkeypatch: MonkeyPatch, pytester: Pytester |
| 838 | + ) -> None: |
| 839 | + """ |
| 840 | + Same test as above, but verify the behavior calling pytest. |
| 841 | +
|
| 842 | + We should not make this call in the same test as above, as the modules have already |
| 843 | + been imported by separate import_path() calls. |
| 844 | + """ |
| 845 | + core_py, test_path1, test_path2 = self.create_installed_doctests_and_tests_dir( |
| 846 | + pytester.path, monkeypatch |
| 847 | + ) |
| 848 | + result = pytester.runpytest( |
| 849 | + "--import-mode=importlib", |
| 850 | + "--doctest-modules", |
| 851 | + "--pyargs", |
| 852 | + "app", |
| 853 | + "./.tests", |
| 854 | + ) |
| 855 | + result.stdout.fnmatch_lines( |
| 856 | + [ |
| 857 | + f"{core_py.relative_to(pytester.path)} . *", |
| 858 | + f"{test_path1.relative_to(pytester.path)} . *", |
| 859 | + f"{test_path2.relative_to(pytester.path)} . *", |
| 860 | + "* 3 passed*", |
| 861 | + ] |
| 862 | + ) |
| 863 | + |
723 | 864 | def test_import_path_imports_correct_file(self, pytester: Pytester) -> None:
|
724 | 865 | """
|
725 | 866 | Import the module by the given path, even if other module with the same name
|
@@ -818,7 +959,7 @@ def setup_directories(
|
818 | 959 | monkeypatch.syspath_prepend(tmp_path / "src/dist2")
|
819 | 960 | return models_py, algorithms_py
|
820 | 961 |
|
821 |
| - @pytest.mark.parametrize("import_mode", ["prepend", "append"]) |
| 962 | + @pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"]) |
822 | 963 | def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
|
823 | 964 | self,
|
824 | 965 | tmp_path: Path,
|
|
0 commit comments