monkeypatch.syspath_prepend: call fixup_namespace_packages
Without the patch the test fails as follows: # Prepending should call fixup_namespace_packages. monkeypatch.syspath_prepend("world") > import ns_pkg.world E ModuleNotFoundError: No module named 'ns_pkg.world'
This commit is contained in:
parent
15d608867d
commit
475119988c
|
@ -0,0 +1 @@
|
|||
``monkeypatch.syspath_prepend`` calls ``pkg_resources.fixup_namespace_packages`` to handle namespace packages better.
|
|
@ -262,10 +262,15 @@ class MonkeyPatch(object):
|
|||
|
||||
def syspath_prepend(self, path):
|
||||
""" Prepend ``path`` to ``sys.path`` list of import locations. """
|
||||
from pkg_resources import fixup_namespace_packages
|
||||
|
||||
if self._savesyspath is None:
|
||||
self._savesyspath = sys.path[:]
|
||||
sys.path.insert(0, str(path))
|
||||
|
||||
# https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
|
||||
fixup_namespace_packages(str(path))
|
||||
|
||||
def chdir(self, path):
|
||||
""" Change the current working directory to the specified path.
|
||||
Path can be a string or a py.path.local object.
|
||||
|
|
|
@ -437,3 +437,28 @@ def test_context():
|
|||
m.setattr(functools, "partial", 3)
|
||||
assert not inspect.isclass(functools.partial)
|
||||
assert inspect.isclass(functools.partial)
|
||||
|
||||
|
||||
def test_syspath_prepend_with_namespace_packages(testdir, monkeypatch):
|
||||
for dirname in "hello", "world":
|
||||
d = testdir.mkdir(dirname)
|
||||
ns = d.mkdir("ns_pkg")
|
||||
ns.join("__init__.py").write(
|
||||
"__import__('pkg_resources').declare_namespace(__name__)"
|
||||
)
|
||||
lib = ns.mkdir(dirname)
|
||||
lib.join("__init__.py").write("def check(): return %r" % dirname)
|
||||
|
||||
monkeypatch.syspath_prepend("hello")
|
||||
import ns_pkg.hello
|
||||
|
||||
assert ns_pkg.hello.check() == "hello"
|
||||
|
||||
with pytest.raises(ImportError):
|
||||
import ns_pkg.world
|
||||
|
||||
# Prepending should call fixup_namespace_packages.
|
||||
monkeypatch.syspath_prepend("world")
|
||||
import ns_pkg.world
|
||||
|
||||
assert ns_pkg.world.check() == "world"
|
||||
|
|
Loading…
Reference in New Issue