Skip to content

Commit

Permalink
Fix loading of fake modules for relative imports
Browse files Browse the repository at this point in the history
 - was missing in previous commit
  • Loading branch information
mrbean-bremen committed Oct 20, 2024
1 parent 1411d76 commit f3de3b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,10 @@ def fake_module_path(self, name: str) -> str:
module_path = fs.joinpaths(path, base_path)
py_module_path = module_path + ".py"
if fs.exists(py_module_path):
return py_module_path
return fs.absnormpath(py_module_path)
init_path = fs.joinpaths(module_path, "__init__.py")
if fs.exists(init_path):
return init_path
return fs.absnormpath(init_path)
return ""

def find_spec(
Expand Down
9 changes: 9 additions & 0 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,15 @@ def test_fake_import_dotted_module(self):
assert module.__name__ == "fakepkg.fake_module"
assert module.number == 42

def test_fake_relative_import(self):
fake_module_path = Path("site-packages") / "fake_module.py"
self.fs.create_file(fake_module_path, contents="number = 42")
sys.path.insert(0, str(fake_module_path.parent))
module = importlib.import_module("fake_module")
del sys.path[0]
assert module.__name__ == "fake_module"
assert module.number == 42


if __name__ == "__main__":
unittest.main()

0 comments on commit f3de3b9

Please sign in to comment.