Skip to content

Commit

Permalink
Overwrite instance check for PathLibPathModule (#685)
Browse files Browse the repository at this point in the history
- fixes #666 without the need to skip _pytest.pathlib patching
- add module and session scoped fs fixtures
- fixes #684
  • Loading branch information
mrbean-bremen authored Jul 11, 2022
1 parent bbba0c6 commit 6bc3162
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ The released versions correspond to PyPi releases.
### New Features
* added some support for the upcoming Python version 3.11
(see [#677](../../issues/677))
* added convenience fixtures for module- and session based `fs` fixtures
(`fs_module` and `fs_session`)

### Fixes
* fixed an incompatibility of `tmpdir` (and probably other fixtures) with the
module-scoped version of `fs`; had been introduced in
pyfakefs 4.5.5 by the fix for [#666](../../issues/666)
(see [#684](../../issues/684))

## [Version 4.5.6](https://pypi.python.org/pypi/pyfakefs/4.5.6) (2022-03-17)
Fixes a regression which broke tests with older pytest versions (< 3.9).
Expand Down
4 changes: 4 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ tests:
"""
yield fs
For convenience, module- and session-scoped fixtures with the same
functionality are provided, named ``fs_module`` and ``fs_session``,
respectively.


Patch using fake_filesystem_unittest.Patcher
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
5 changes: 5 additions & 0 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,11 @@ def __call__(self, *args, **kwargs):
def __getattr__(self, name):
return getattr(self.fake_pathlib.Path, name)

@classmethod
def __instancecheck__(cls, instance):
# fake the inheritance to pass isinstance checks - see #666
return isinstance(instance, PurePath)


class RealPath(pathlib.Path):
"""Replacement for `pathlib.Path` if it shall not be faked.
Expand Down
27 changes: 24 additions & 3 deletions pyfakefs/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ 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)
if hasattr(_pytest, "pathlib"):
Patcher.SKIPMODULES.add(_pytest.pathlib)


@pytest.fixture
Expand All @@ -31,3 +28,27 @@ def fs(request):
patcher.setUp()
yield patcher.fs
patcher.tearDown()


@pytest.fixture(scope="module")
def fs_module(request):
""" Module-scoped fake filesystem fixture. """
if hasattr(request, 'param'):
patcher = Patcher(*request.param)
else:
patcher = Patcher()
patcher.setUp()
yield patcher.fs
patcher.tearDown()


@pytest.fixture(scope="session")
def fs_session(request):
""" Session-scoped fake filesystem fixture. """
if hasattr(request, 'param'):
patcher = Patcher(*request.param)
else:
patcher = Patcher()
patcher.setUp()
yield patcher.fs
patcher.tearDown()
12 changes: 12 additions & 0 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from pathlib import Path
from unittest import TestCase, mock

import pytest

import pyfakefs.tests.import_as_example
import pyfakefs.tests.logsio
from pyfakefs import fake_filesystem_unittest, fake_filesystem
Expand Down Expand Up @@ -879,5 +881,15 @@ def test_is_absolute(self, fs):
self.assertTrue(pathlib.Path(".").absolute().is_absolute())


class TestModuleScopedFsWithTmpdir:
@pytest.fixture(autouse=True)
def test_internal(self, tmpdir):
yield

def test_fail(self, fs_module):
# Regression test for #684
assert True


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

0 comments on commit 6bc3162

Please sign in to comment.