Skip to content

Commit

Permalink
Patch 'Path' if imported from pathlib or pathlib2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Oct 15, 2018
1 parent 51781b9 commit f277169
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 1 addition & 21 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pyfakefs/tests/import_as_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit f277169

Please sign in to comment.