Skip to content

Commit

Permalink
Use RealPathlibModule for all skipped modules
Browse files Browse the repository at this point in the history
- avoids the use of FakePathLibPath in pathlib
- add _pytest.pathlib to skipped modules,
  fake isinstance check for RealPathlibPathModule
- prevents issue with _pytest.pathlib in pytest 7.0.0,
  which would cause all tests with fs fixture to fail
- see #666
  • Loading branch information
mrbean-bremen committed Feb 13, 2022
1 parent a96bdbb commit 2ee83a3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The released versions correspond to PyPi releases.
(see [#661](../../issues/661))
* disallow `encoding` argument on binary `open()`
(see [#664](../../issues/664))
* fixed compatibility issue with pytest 7.0.0
(see [#666](../../issues/666))

## [Version 4.5.4](https://pypi.python.org/pypi/pyfakefs/4.5.4) (2022-01-12)
Minor bugfix release.
Expand Down
36 changes: 18 additions & 18 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,6 @@ def _find_modules(self) -> None:
for name, module in list(sys.modules.items()):
try:
if (self.use_cache and module in self.CACHED_MODULES or
module in self.SKIPMODULES or
not inspect.ismodule(module)):
continue
except Exception:
Expand All @@ -692,7 +691,8 @@ def _find_modules(self) -> None:
if self.use_cache:
self.__class__.CACHED_MODULES.add(module)
continue
skipped = (any([sn.startswith(module.__name__)
skipped = (module in self.SKIPMODULES or
any([sn.startswith(module.__name__)
for sn in self._skip_names]))
module_items = module.__dict__.copy().items()

Expand All @@ -703,22 +703,22 @@ def _find_modules(self) -> None:
for name, mod in modules.items():
self.__class__.SKIPPED_FS_MODULES.setdefault(
name, set()).add((module, mod.__name__))
continue

for name, mod in modules.items():
self.__class__.FS_MODULES.setdefault(name, set()).add(
(module, mod.__name__))
functions = {name: fct for name, fct in
module_items
if self._is_fs_function(fct)}

for name, fct in functions.items():
self.__class__.FS_FUNCTIONS.setdefault(
(name, fct.__name__, fct.__module__), set()).add(module)

# find default arguments that are file system functions
if self.patch_default_args:
self._find_def_values(module_items)
else:
for name, mod in modules.items():
self.__class__.FS_MODULES.setdefault(name, set()).add(
(module, mod.__name__))
functions = {name: fct for name, fct in
module_items
if self._is_fs_function(fct)}

for name, fct in functions.items():
self.__class__.FS_FUNCTIONS.setdefault(
(name, fct.__name__, fct.__module__),
set()).add(module)

# find default arguments that are file system functions
if self.patch_default_args:
self._find_def_values(module_items)

if self.use_cache:
self.__class__.CACHED_MODULES.add(module)
Expand Down
6 changes: 6 additions & 0 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,12 @@ class RealPathlibPathModule:
"""Patches `pathlib.Path` by passing all calls to RealPathlibModule."""
real_pathlib = None

@classmethod
def __instancecheck__(cls, instance):
# as we cannot derive from pathlib.Path, we fake
# the inheritance to pass isinstance checks - see #666
return isinstance(instance, PurePath)

def __init__(self):
if self.real_pathlib is None:
self.__class__.real_pathlib = RealPathlibModule()
Expand Down
3 changes: 2 additions & 1 deletion pyfakefs/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ def my_fakefs_test(fs):
fs.create_file('/var/data/xx1.txt')
assert os.path.exists('/var/data/xx1.txt')
"""

import _pytest
import py
import pytest

from pyfakefs.fake_filesystem_unittest import Patcher

Patcher.SKIPMODULES.add(py)
Patcher.SKIPMODULES.add(pytest)
Patcher.SKIPMODULES.add(_pytest.pathlib)


@pytest.fixture
Expand Down
6 changes: 6 additions & 0 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,8 @@ def test_disk_full_after_reopened(self):
f.write('b' * 110)
with self.raises_os_error(errno.ENOSPC):
f.flush()
with self.open('bar.txt') as f:
self.assertEqual('', f.read())

def test_disk_full_append(self):
file_path = 'bar.txt'
Expand All @@ -1826,6 +1828,8 @@ def test_disk_full_append(self):
f.write('b' * 41)
with self.raises_os_error(errno.ENOSPC):
f.flush()
with self.open('bar.txt') as f:
self.assertEqual(f.read(), 'a' * 60)

def test_disk_full_after_reopened_rplus_seek(self):
with self.open('bar.txt', 'w') as f:
Expand All @@ -1838,6 +1842,8 @@ def test_disk_full_after_reopened_rplus_seek(self):
f.write('b' * 60)
with self.raises_os_error(errno.ENOSPC):
f.flush()
with self.open('bar.txt') as f:
self.assertEqual(f.read(), 'a' * 60)


class MountPointTest(TestCase):
Expand Down

0 comments on commit 2ee83a3

Please sign in to comment.