Skip to content

Commit

Permalink
monkeypatch.syspath_prepend: invalidate import cache
Browse files Browse the repository at this point in the history
This was done with testdir only, and uses the fixed monkeypatch method
there now.
  • Loading branch information
blueyed committed Apr 12, 2019
1 parent a9e850f commit 3dcfdcc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/_pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ def syspath_prepend(self, path):
# https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
fixup_namespace_packages(str(path))

# A call to syspathinsert() usually means that the caller wants to
# import some dynamically created files, thus with python3 we
# invalidate its import caches.
try:
from importlib import invalidate_caches
except ImportError:
return
invalidate_caches()

def chdir(self, path):
""" Change the current working directory to the specified path.
Path can be a string or a py.path.local object.
Expand Down
19 changes: 1 addition & 18 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,27 +616,10 @@ def syspathinsert(self, path=None):
This is undone automatically when this object dies at the end of each
test.
"""
from pkg_resources import fixup_namespace_packages

if path is None:
path = self.tmpdir

dirname = str(path)
sys.path.insert(0, dirname)
fixup_namespace_packages(dirname)

# a call to syspathinsert() usually means that the caller wants to
# import some dynamically created files, thus with python3 we
# invalidate its import caches
self._possibly_invalidate_import_caches()

def _possibly_invalidate_import_caches(self):
# invalidate caches if we can (py33 and above)
try:
from importlib import invalidate_caches
except ImportError:
return
invalidate_caches()
self.monkeypatch.syspath_prepend(str(path))

def mkdir(self, name):
"""Create a new (sub)directory."""
Expand Down

0 comments on commit 3dcfdcc

Please sign in to comment.