|
| 1 | +import os |
| 2 | + |
| 3 | +from mypy.options import Options |
| 4 | +from mypy.modulefinder import FindModuleCache, SearchPaths |
| 5 | + |
| 6 | +from mypy.test.helpers import Suite, assert_equal |
| 7 | +from mypy.test.config import package_path |
| 8 | +data_path = os.path.relpath(os.path.join(package_path, "modulefinder")) |
| 9 | + |
| 10 | + |
| 11 | +class ModuleFinderSuite(Suite): |
| 12 | + |
| 13 | + def setUp(self) -> None: |
| 14 | + self.search_paths = SearchPaths( |
| 15 | + python_path=(), |
| 16 | + mypy_path=( |
| 17 | + os.path.join(data_path, "nsx-pkg1"), |
| 18 | + os.path.join(data_path, "nsx-pkg2"), |
| 19 | + os.path.join(data_path, "nsx-pkg3"), |
| 20 | + os.path.join(data_path, "nsy-pkg1"), |
| 21 | + os.path.join(data_path, "nsy-pkg2"), |
| 22 | + os.path.join(data_path, "pkg1"), |
| 23 | + os.path.join(data_path, "pkg2"), |
| 24 | + ), |
| 25 | + package_path=(), |
| 26 | + typeshed_path=(), |
| 27 | + ) |
| 28 | + options = Options() |
| 29 | + options.namespace_packages = True |
| 30 | + self.fmc_ns = FindModuleCache(self.search_paths, options=options) |
| 31 | + |
| 32 | + options = Options() |
| 33 | + options.namespace_packages = False |
| 34 | + self.fmc_nons = FindModuleCache(self.search_paths, options=options) |
| 35 | + |
| 36 | + def test__no_namespace_packages__nsx(self) -> None: |
| 37 | + """ |
| 38 | + If namespace_packages is False, we shouldn't find nsx |
| 39 | + """ |
| 40 | + found_module = self.fmc_nons.find_module("nsx") |
| 41 | + self.assertIsNone(found_module) |
| 42 | + |
| 43 | + def test__no_namespace_packages__nsx_a(self) -> None: |
| 44 | + """ |
| 45 | + If namespace_packages is False, we shouldn't find nsx.a. |
| 46 | + """ |
| 47 | + found_module = self.fmc_nons.find_module("nsx.a") |
| 48 | + self.assertIsNone(found_module) |
| 49 | + |
| 50 | + def test__no_namespace_packages__find_a_in_pkg1(self) -> None: |
| 51 | + """ |
| 52 | + Find find pkg1/a.py for "a" with namespace_packages False. |
| 53 | + """ |
| 54 | + found_module = self.fmc_nons.find_module("a") |
| 55 | + expected = os.path.join(data_path, "pkg1", "a.py") |
| 56 | + assert_equal(expected, found_module) |
| 57 | + |
| 58 | + def test__no_namespace_packages__find_b_in_pkg2(self) -> None: |
| 59 | + found_module = self.fmc_ns.find_module("b") |
| 60 | + expected = os.path.join(data_path, "pkg2", "b", "__init__.py") |
| 61 | + assert_equal(expected, found_module) |
| 62 | + |
| 63 | + def test__find_nsx_as_namespace_pkg_in_pkg1(self) -> None: |
| 64 | + """ |
| 65 | + There's no __init__.py in any of the nsx dirs, return |
| 66 | + the path to the first one found in mypypath. |
| 67 | + """ |
| 68 | + found_module = self.fmc_ns.find_module("nsx") |
| 69 | + expected = os.path.join(data_path, "nsx-pkg1", "nsx") |
| 70 | + assert_equal(expected, found_module) |
| 71 | + |
| 72 | + def test__find_nsx_a_init_in_pkg1(self) -> None: |
| 73 | + """ |
| 74 | + Find nsx-pkg1/nsx/a/__init__.py for "nsx.a" in namespace mode. |
| 75 | + """ |
| 76 | + found_module = self.fmc_ns.find_module("nsx.a") |
| 77 | + expected = os.path.join(data_path, "nsx-pkg1", "nsx", "a", "__init__.py") |
| 78 | + assert_equal(expected, found_module) |
| 79 | + |
| 80 | + def test__find_nsx_b_init_in_pkg2(self) -> None: |
| 81 | + """ |
| 82 | + Find nsx-pkg2/nsx/b/__init__.py for "nsx.b" in namespace mode. |
| 83 | + """ |
| 84 | + found_module = self.fmc_ns.find_module("nsx.b") |
| 85 | + expected = os.path.join(data_path, "nsx-pkg2", "nsx", "b", "__init__.py") |
| 86 | + assert_equal(expected, found_module) |
| 87 | + |
| 88 | + def test__find_nsx_c_c_in_pkg3(self) -> None: |
| 89 | + """ |
| 90 | + Find nsx-pkg3/nsx/c/c.py for "nsx.c.c" in namespace mode. |
| 91 | + """ |
| 92 | + found_module = self.fmc_ns.find_module("nsx.c.c") |
| 93 | + expected = os.path.join(data_path, "nsx-pkg3", "nsx", "c", "c.py") |
| 94 | + assert_equal(expected, found_module) |
| 95 | + |
| 96 | + def test__find_nsy_a__init_pyi(self) -> None: |
| 97 | + """ |
| 98 | + Prefer nsy-pkg1/a/__init__.pyi file over __init__.py. |
| 99 | + """ |
| 100 | + found_module = self.fmc_ns.find_module("nsy.a") |
| 101 | + expected = os.path.join(data_path, "nsy-pkg1", "nsy", "a", "__init__.pyi") |
| 102 | + assert_equal(expected, found_module) |
| 103 | + |
| 104 | + def test__find_nsy_b__init_py(self) -> None: |
| 105 | + """ |
| 106 | + There is a nsy-pkg2/nsy/b.pyi, but also a nsy-pkg2/nsy/b/__init__.py. |
| 107 | + We expect to find the latter when looking up "nsy.b" as |
| 108 | + a package is preferred over a module. |
| 109 | + """ |
| 110 | + found_module = self.fmc_ns.find_module("nsy.b") |
| 111 | + expected = os.path.join(data_path, "nsy-pkg2", "nsy", "b", "__init__.py") |
| 112 | + assert_equal(expected, found_module) |
| 113 | + |
| 114 | + def test__find_nsy_c_pyi(self) -> None: |
| 115 | + """ |
| 116 | + There is a nsy-pkg2/nsy/c.pyi and nsy-pkg2/nsy/c.py |
| 117 | + We expect to find the former when looking up "nsy.b" as |
| 118 | + .pyi is preferred over .py. |
| 119 | + """ |
| 120 | + found_module = self.fmc_ns.find_module("nsy.c") |
| 121 | + expected = os.path.join(data_path, "nsy-pkg2", "nsy", "c.pyi") |
| 122 | + assert_equal(expected, found_module) |
| 123 | + |
| 124 | + def test__find_a_in_pkg1(self) -> None: |
| 125 | + found_module = self.fmc_ns.find_module("a") |
| 126 | + expected = os.path.join(data_path, "pkg1", "a.py") |
| 127 | + assert_equal(expected, found_module) |
| 128 | + |
| 129 | + def test__find_b_init_in_pkg2(self) -> None: |
| 130 | + found_module = self.fmc_ns.find_module("b") |
| 131 | + expected = os.path.join(data_path, "pkg2", "b", "__init__.py") |
| 132 | + assert_equal(expected, found_module) |
| 133 | + |
| 134 | + def test__find_d_nowhere(self) -> None: |
| 135 | + found_module = self.fmc_ns.find_module("d") |
| 136 | + self.assertIsNone(found_module) |
0 commit comments