From f277169a88565ccf4865ef4a82b027549d42962c Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Mon, 15 Oct 2018 20:54:40 +0200 Subject: [PATCH] Patch 'Path' if imported from pathlib or pathlib2 - see #440 --- CHANGES.md | 2 ++ pyfakefs/fake_filesystem_unittest.py | 8 +++++-- pyfakefs/fake_pathlib.py | 15 +++++++++++++ .../tests/fake_filesystem_unittest_test.py | 22 +------------------ pyfakefs/tests/import_as_example.py | 10 ++++----- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a7fa419c..9613665d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ This version of pyfakefs does not support Python 3.3. Python 3.3 users shall keep using pyfakefs 3.4.3, or upgrade to a newer Python version. #### New Features + * automatically patch `Path` if imported like `from pathlib import Path` + ([#440](../../issues/440)) * added side_effect option to fake files ([#433](../../pull/433)) * parameter `patch_path` has been removed from `UnitTest` and `Patcher`, the correct patching of `path` imports is now done automatically diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index 19c153d6..264d28d4 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -64,7 +64,7 @@ from pyfakefs import fake_filesystem from pyfakefs import fake_filesystem_shutil from pyfakefs import mox3_stubout -from pyfakefs.extra_packages import pathlib, use_scandir +from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir if pathlib: from pyfakefs import fake_pathlib @@ -313,14 +313,18 @@ def __init__(self, additional_skip_names=None, 'shutil': fake_filesystem_shutil.FakeShutilModule, 'io': fake_filesystem.FakeIoModule, } + self._class_modules = {} if pathlib: self._fake_module_classes[ 'pathlib'] = fake_pathlib.FakePathlibModule + self._fake_module_classes[ + 'Path'] = fake_pathlib.FakePathlibPathModule + mod_name = 'pathlib2' if pathlib2 is not None else 'pathlib' + self._class_modules['Path'] = mod_name if use_scandir: self._fake_module_classes[ 'scandir'] = fake_scandir.FakeScanDirModule - self._class_modules = {} if modules_to_patch is not None: for name, fake_module in modules_to_patch.items(): if '.' in name: diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py index d3631e30..bd514fd6 100644 --- a/pyfakefs/fake_pathlib.py +++ b/pyfakefs/fake_pathlib.py @@ -670,3 +670,18 @@ class PosixPath(FakePath, PurePosixPath): def __getattr__(self, name): """Forwards any unfaked calls to the standard pathlib module.""" return getattr(self._pathlib_module, name) + + +class FakePathlibPathModule(object): + """Patches `pathlib.Path` by passing all calls to FakePathlibModule.""" + fake_pathlib = None + + def __init__(self, filesystem): + if self.fake_pathlib is None: + self.__class__.fake_pathlib = FakePathlibModule(filesystem) + + def __call__(self, *args, **kwargs): + return self.fake_pathlib.Path(*args, **kwargs) + + def __getattr__(self, name): + return getattr(self.fake_pathlib.Path, name) diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index e3786364..58009a6f 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -194,28 +194,8 @@ def test_own_path_module(self): if pathlib: - class FakePathlibPathModule(object): - """Patches `pathlib.Path` by passing all calls to FakePathlibModule.""" - fake_pathlib = None - - def __init__(self, filesystem): - if self.fake_pathlib is None: - from pyfakefs.fake_pathlib import FakePathlibModule - self.__class__.fake_pathlib = FakePathlibModule(filesystem) - - def __call__(self, *args, **kwargs): - return self.fake_pathlib.Path(*args, **kwargs) - - def __getattr__(self, name): - return getattr(self.fake_pathlib.Path, name) - class PatchPathlibPathTest(TestPyfakefsUnittestBase): - """Shows how to patch a class inside a module.""" - def __init__(self, methodName='RunTest'): - modules_to_patch = {'pathlib.Path': FakePathlibPathModule} - super(PatchPathlibPathTest, self).__init__( - methodName, modules_to_patch=modules_to_patch) - + """Shows that pathlib.Path is correctly patched.""" def test_path_exists(self): file_path = '/foo/bar' self.fs.create_dir(file_path) diff --git a/pyfakefs/tests/import_as_example.py b/pyfakefs/tests/import_as_example.py index 86fb2e6e..7ebb8ad6 100644 --- a/pyfakefs/tests/import_as_example.py +++ b/pyfakefs/tests/import_as_example.py @@ -17,18 +17,18 @@ import os as my_os try: - import pathlib + from pathlib import Path except ImportError: try: - import pathlib2 as pathlib + from pathlib2 import Path except ImportError: - pathlib = None + Path = None def check_if_exists(filepath): return my_os.path.exists(filepath) -if pathlib: +if Path: def check_if_path_exists(filepath): - return pathlib.Path(filepath).exists() + return Path(filepath).exists()