diff options
author | mrbean-bremen <mrbean-bremen@users.noreply.github.com> | 2022-07-11 18:31:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-11 18:31:30 +0200 |
commit | 6bc3162256acc9364a096ba1d58883c9cb599ce5 (patch) | |
tree | bcce2b29b8954fb3251766101472e629f07a1438 /pyfakefs | |
parent | bbba0c626746f8cb93caf42f11fc85fa387d675b (diff) | |
download | pyfakefs-6bc3162256acc9364a096ba1d58883c9cb599ce5.tar.gz |
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
Diffstat (limited to 'pyfakefs')
-rw-r--r-- | pyfakefs/fake_pathlib.py | 5 | ||||
-rw-r--r-- | pyfakefs/pytest_plugin.py | 27 | ||||
-rw-r--r-- | pyfakefs/tests/fake_filesystem_unittest_test.py | 12 |
3 files changed, 41 insertions, 3 deletions
diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py index e1179a2..efa0706 100644 --- a/pyfakefs/fake_pathlib.py +++ b/pyfakefs/fake_pathlib.py @@ -792,6 +792,11 @@ class FakePathlibPathModule: 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. diff --git a/pyfakefs/pytest_plugin.py b/pyfakefs/pytest_plugin.py index 3d3306c..7a3103f 100644 --- a/pyfakefs/pytest_plugin.py +++ b/pyfakefs/pytest_plugin.py @@ -8,7 +8,6 @@ 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 @@ -16,8 +15,6 @@ 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 @@ -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() diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 97b5f7b..4627b09 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -32,6 +32,8 @@ from distutils.dir_util import copy_tree, remove_tree 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 @@ -879,5 +881,15 @@ class TestAbsolutePathOnWindows(fake_filesystem_unittest.TestCase): 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() |