Skip to content

Commit 6bc3162

Browse files
Overwrite instance check for PathLibPathModule (#685)
- fixes #666 without the need to skip _pytest.pathlib patching - add module and session scoped fs fixtures - fixes #684
1 parent bbba0c6 commit 6bc3162

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

Diff for: CHANGES.md

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ The released versions correspond to PyPi releases.
1818
### New Features
1919
* added some support for the upcoming Python version 3.11
2020
(see [#677](../../issues/677))
21+
* added convenience fixtures for module- and session based `fs` fixtures
22+
(`fs_module` and `fs_session`)
23+
24+
### Fixes
25+
* fixed an incompatibility of `tmpdir` (and probably other fixtures) with the
26+
module-scoped version of `fs`; had been introduced in
27+
pyfakefs 4.5.5 by the fix for [#666](../../issues/666)
28+
(see [#684](../../issues/684))
2129

2230
## [Version 4.5.6](https://pypi.python.org/pypi/pyfakefs/4.5.6) (2022-03-17)
2331
Fixes a regression which broke tests with older pytest versions (< 3.9).

Diff for: docs/usage.rst

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ tests:
6464
"""
6565
yield fs
6666
67+
For convenience, module- and session-scoped fixtures with the same
68+
functionality are provided, named ``fs_module`` and ``fs_session``,
69+
respectively.
70+
6771

6872
Patch using fake_filesystem_unittest.Patcher
6973
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Diff for: pyfakefs/fake_pathlib.py

+5
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,11 @@ def __call__(self, *args, **kwargs):
792792
def __getattr__(self, name):
793793
return getattr(self.fake_pathlib.Path, name)
794794

795+
@classmethod
796+
def __instancecheck__(cls, instance):
797+
# fake the inheritance to pass isinstance checks - see #666
798+
return isinstance(instance, PurePath)
799+
795800

796801
class RealPath(pathlib.Path):
797802
"""Replacement for `pathlib.Path` if it shall not be faked.

Diff for: pyfakefs/pytest_plugin.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ def my_fakefs_test(fs):
88
fs.create_file('/var/data/xx1.txt')
99
assert os.path.exists('/var/data/xx1.txt')
1010
"""
11-
import _pytest
1211
import py
1312
import pytest
1413

1514
from pyfakefs.fake_filesystem_unittest import Patcher
1615

1716
Patcher.SKIPMODULES.add(py)
1817
Patcher.SKIPMODULES.add(pytest)
19-
if hasattr(_pytest, "pathlib"):
20-
Patcher.SKIPMODULES.add(_pytest.pathlib)
2118

2219

2320
@pytest.fixture
@@ -31,3 +28,27 @@ def fs(request):
3128
patcher.setUp()
3229
yield patcher.fs
3330
patcher.tearDown()
31+
32+
33+
@pytest.fixture(scope="module")
34+
def fs_module(request):
35+
""" Module-scoped fake filesystem fixture. """
36+
if hasattr(request, 'param'):
37+
patcher = Patcher(*request.param)
38+
else:
39+
patcher = Patcher()
40+
patcher.setUp()
41+
yield patcher.fs
42+
patcher.tearDown()
43+
44+
45+
@pytest.fixture(scope="session")
46+
def fs_session(request):
47+
""" Session-scoped fake filesystem fixture. """
48+
if hasattr(request, 'param'):
49+
patcher = Patcher(*request.param)
50+
else:
51+
patcher = Patcher()
52+
patcher.setUp()
53+
yield patcher.fs
54+
patcher.tearDown()

Diff for: pyfakefs/tests/fake_filesystem_unittest_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
from pathlib import Path
3333
from unittest import TestCase, mock
3434

35+
import pytest
36+
3537
import pyfakefs.tests.import_as_example
3638
import pyfakefs.tests.logsio
3739
from pyfakefs import fake_filesystem_unittest, fake_filesystem
@@ -879,5 +881,15 @@ def test_is_absolute(self, fs):
879881
self.assertTrue(pathlib.Path(".").absolute().is_absolute())
880882

881883

884+
class TestModuleScopedFsWithTmpdir:
885+
@pytest.fixture(autouse=True)
886+
def test_internal(self, tmpdir):
887+
yield
888+
889+
def test_fail(self, fs_module):
890+
# Regression test for #684
891+
assert True
892+
893+
882894
if __name__ == "__main__":
883895
unittest.main()

0 commit comments

Comments
 (0)