Skip to content

Commit f92c9fc

Browse files
committed
Patch 'Path' if imported from pathlib or pathlib2
- see #440
1 parent 51781b9 commit f92c9fc

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This version of pyfakefs does not support Python 3.3. Python 3.3 users shall
77
keep using pyfakefs 3.4.3, or upgrade to a newer Python version.
88

99
#### New Features
10+
* automatically patch `Path` if imported like `from pathlib import Path`
11+
([#440](../../issues/440))
1012
* added side_effect option to fake files ([#433](../../pull/433))
1113
* parameter `patch_path` has been removed from `UnitTest` and `Patcher`,
1214
the correct patching of `path` imports is now done automatically

pyfakefs/fake_filesystem_unittest.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from pyfakefs import fake_filesystem
6565
from pyfakefs import fake_filesystem_shutil
6666
from pyfakefs import mox3_stubout
67-
from pyfakefs.extra_packages import pathlib, use_scandir
67+
from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir
6868

6969
if pathlib:
7070
from pyfakefs import fake_pathlib
@@ -313,14 +313,18 @@ def __init__(self, additional_skip_names=None,
313313
'shutil': fake_filesystem_shutil.FakeShutilModule,
314314
'io': fake_filesystem.FakeIoModule,
315315
}
316+
self._class_modules = {}
316317
if pathlib:
317318
self._fake_module_classes[
318319
'pathlib'] = fake_pathlib.FakePathlibModule
320+
self._fake_module_classes[
321+
'Path'] = fake_pathlib.FakePathlibPathModule
322+
mod_name = 'pathlib2' if pathlib2 is not None else 'pathlib'
323+
self._class_modules['Path'] = mod_name
319324
if use_scandir:
320325
self._fake_module_classes[
321326
'scandir'] = fake_scandir.FakeScanDirModule
322327

323-
self._class_modules = {}
324328
if modules_to_patch is not None:
325329
for name, fake_module in modules_to_patch.items():
326330
if '.' in name:

pyfakefs/fake_pathlib.py

+15
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,18 @@ class PosixPath(FakePath, PurePosixPath):
670670
def __getattr__(self, name):
671671
"""Forwards any unfaked calls to the standard pathlib module."""
672672
return getattr(self._pathlib_module, name)
673+
674+
675+
class FakePathlibPathModule(object):
676+
"""Patches `pathlib.Path` by passing all calls to FakePathlibModule."""
677+
fake_pathlib = None
678+
679+
def __init__(self, filesystem):
680+
if self.fake_pathlib is None:
681+
self.__class__.fake_pathlib = FakePathlibModule(filesystem)
682+
683+
def __call__(self, *args, **kwargs):
684+
return self.fake_pathlib.Path(*args, **kwargs)
685+
686+
def __getattr__(self, name):
687+
return getattr(self.fake_pathlib.Path, name)

pyfakefs/tests/fake_filesystem_unittest_test.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,8 @@ def test_own_path_module(self):
194194

195195

196196
if pathlib:
197-
class FakePathlibPathModule(object):
198-
"""Patches `pathlib.Path` by passing all calls to FakePathlibModule."""
199-
fake_pathlib = None
200-
201-
def __init__(self, filesystem):
202-
if self.fake_pathlib is None:
203-
from pyfakefs.fake_pathlib import FakePathlibModule
204-
self.__class__.fake_pathlib = FakePathlibModule(filesystem)
205-
206-
def __call__(self, *args, **kwargs):
207-
return self.fake_pathlib.Path(*args, **kwargs)
208-
209-
def __getattr__(self, name):
210-
return getattr(self.fake_pathlib.Path, name)
211-
212197
class PatchPathlibPathTest(TestPyfakefsUnittestBase):
213-
"""Shows how to patch a class inside a module."""
214-
def __init__(self, methodName='RunTest'):
215-
modules_to_patch = {'pathlib.Path': FakePathlibPathModule}
216-
super(PatchPathlibPathTest, self).__init__(
217-
methodName, modules_to_patch=modules_to_patch)
218-
198+
"""Shows that pathlib.Path is correctly patched."""
219199
def test_path_exists(self):
220200
file_path = '/foo/bar'
221201
self.fs.create_dir(file_path)

pyfakefs/tests/import_as_example.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
import os as my_os
1818

1919
try:
20-
import pathlib
20+
from pathlib import Path
2121
except ImportError:
2222
try:
23-
import pathlib2 as pathlib
23+
from pathlib2 import Path
2424
except ImportError:
25-
pathlib = None
25+
Path = None
2626

2727

2828
def check_if_exists(filepath):
2929
return my_os.path.exists(filepath)
3030

3131

32-
if pathlib:
32+
if Path:
3333
def check_if_path_exists(filepath):
34-
return pathlib.Path(filepath).exists()
34+
return Path(filepath).exists()

0 commit comments

Comments
 (0)