Skip to content

Commit d3b0b93

Browse files
committed
Fix loading of fake modules for relative imports
- was missing in previous commit
1 parent de8e415 commit d3b0b93

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

pyfakefs/fake_filesystem_unittest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,10 @@ def fake_module_path(self, name: str) -> str:
12441244
module_path = fs.joinpaths(path, base_path)
12451245
py_module_path = module_path + ".py"
12461246
if fs.exists(py_module_path):
1247-
return py_module_path
1247+
return fs.absnormpath(py_module_path)
12481248
init_path = fs.joinpaths(module_path, "__init__.py")
12491249
if fs.exists(init_path):
1250-
return init_path
1250+
return fs.absnormpath(init_path)
12511251
return ""
12521252

12531253
def find_spec(

pyfakefs/tests/fake_filesystem_unittest_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,15 @@ def test_fake_import_dotted_module(self):
978978
assert module.__name__ == "fakepkg.fake_module"
979979
assert module.number == 42
980980

981+
def test_fake_relative_import(self):
982+
fake_module_path = Path("site-packages") / "fake_module.py"
983+
self.fs.create_file(fake_module_path, contents="number = 42")
984+
sys.path.insert(0, str(fake_module_path.parent))
985+
module = importlib.import_module("fake_module")
986+
del sys.path[0]
987+
assert module.__name__ == "fake_module"
988+
assert module.number == 42
989+
981990

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

0 commit comments

Comments
 (0)