Skip to content

Commit 3bc56d2

Browse files
committed
tests/modulefinder: tests for FindModuleCache.find_module()
Those tests go hand in hand with the directory structure that was created in test-data/packages/modulefinder.
1 parent fc31dfe commit 3bc56d2

File tree

13 files changed

+131
-0
lines changed

13 files changed

+131
-0
lines changed

mypy/test/testmodulefinder.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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, "pkg4"),
23+
os.path.join(data_path, "pkg5"),
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_pkg4(self) -> None:
51+
"""
52+
Find find pkg4/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, "pkg4", "a.py")
56+
assert_equal(expected, found_module)
57+
58+
def test__no_namespace_packages__find_b_in_pkg5(self) -> None:
59+
found_module = self.fmc_ns.find_module("b")
60+
expected = os.path.join(data_path, "pkg5", "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+
found_module = self.fmc_ns.find_module("nsx.a")
74+
expected = os.path.join(data_path, "nsx-pkg1", "nsx", "a", "__init__.py")
75+
assert_equal(expected, found_module)
76+
77+
def test__find_nsx_b_init_in_pkg2(self) -> None:
78+
found_module = self.fmc_ns.find_module("nsx.b")
79+
expected = os.path.join(data_path, "nsx-pkg2", "nsx", "b", "__init__.py")
80+
assert_equal(expected, found_module)
81+
82+
def test__find_nsx_c_c_in_pkg3(self) -> None:
83+
found_module = self.fmc_ns.find_module("nsx.c.c")
84+
expected = os.path.join(data_path, "nsx-pkg3", "nsx", "c", "c.py")
85+
assert_equal(expected, found_module)
86+
87+
def test__find_nsy_a__init_pyi(self) -> None:
88+
"""
89+
Prefer a __init__.pyi file over a __init__.py
90+
"""
91+
found_module = self.fmc_ns.find_module("nsy.a")
92+
expected = os.path.join(data_path, "nsy-pkg1", "nsy", "a", "__init__.pyi")
93+
assert_equal(expected, found_module)
94+
95+
def test__find_nsy_b__init_py(self) -> None:
96+
"""
97+
There is a nsy-pkg2/nsy/b.pyi, but also a nsy-pkg2/nsy/b/__init__.py.
98+
We expect to find the latter when looking up "nsy.b" as
99+
a package is preferred over a module.
100+
"""
101+
found_module = self.fmc_ns.find_module("nsy.b")
102+
expected = os.path.join(data_path, "nsy-pkg2", "nsy", "b", "__init__.py")
103+
assert_equal(expected, found_module)
104+
105+
def test__find_nsy_c_pyi(self) -> None:
106+
"""
107+
There is a nsy-pkg2/nsy/c.pyi and nsy-pkg2/nsy/c.py
108+
We expect to find the former when looking up "nsy.b" as
109+
.pyi is preferred over .py.
110+
"""
111+
found_module = self.fmc_ns.find_module("nsy.c")
112+
expected = os.path.join(data_path, "nsy-pkg2", "nsy", "c.pyi")
113+
assert_equal(expected, found_module)
114+
115+
def test__find_a_in_pkg4(self) -> None:
116+
found_module = self.fmc_ns.find_module("a")
117+
expected = os.path.join(data_path, "pkg4", "a.py")
118+
assert_equal(expected, found_module)
119+
120+
def test__find_b_init_in_pkg5(self) -> None:
121+
found_module = self.fmc_ns.find_module("b")
122+
expected = os.path.join(data_path, "pkg5", "b", "__init__.py")
123+
assert_equal(expected, found_module)
124+
125+
def test__find_d_nowhere(self) -> None:
126+
found_module = self.fmc_ns.find_module("d")
127+
self.assertIsNone(found_module)

test-data/packages/modulefinder/nsx-pkg1/nsx/a/__init__.py

Whitespace-only changes.

test-data/packages/modulefinder/nsx-pkg2/nsx/b/__init__.py

Whitespace-only changes.

test-data/packages/modulefinder/nsx-pkg3/nsx/c/c.py

Whitespace-only changes.

test-data/packages/modulefinder/nsy-pkg1/nsy/a/__init__.py

Whitespace-only changes.

test-data/packages/modulefinder/nsy-pkg1/nsy/a/__init__.pyi

Whitespace-only changes.

test-data/packages/modulefinder/nsy-pkg2/nsy/b.pyi

Whitespace-only changes.

test-data/packages/modulefinder/nsy-pkg2/nsy/b/__init__.py

Whitespace-only changes.

test-data/packages/modulefinder/nsy-pkg2/nsy/c.py

Whitespace-only changes.

test-data/packages/modulefinder/nsy-pkg2/nsy/c.pyi

Whitespace-only changes.

test-data/packages/modulefinder/pkg4/a.py

Whitespace-only changes.

test-data/packages/modulefinder/pkg5/b/__init__.py

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Samples for testing modulefinder.FindModuleCache.
2+
3+
Contains three packages for the `nsx` namespace, and two packages
4+
providing `a` and `b`.

0 commit comments

Comments
 (0)