Skip to content

Commit

Permalink
Make sure fs modules in pathlib are patched
Browse files Browse the repository at this point in the history
- pathlib shall not be added to skip names
- removed unneeded checks for pathlib
- fixes #527
  • Loading branch information
mrbean-bremen committed Mar 30, 2020
1 parent a47e256 commit 292c07f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The released versions correspond to PyPi releases.
#### Fixes
* Do not override global warnings setting in `Deprecator`
(see [#526](../../issues/526))
* Make sure filesystem modules in `pathlib` are patched
(see [#527](../../issues/527))

## [Version 4.0.2](https://pypi.python.org/pypi/pyfakefs/4.0.2)

Expand Down
26 changes: 10 additions & 16 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@

from pyfakefs import fake_filesystem
from pyfakefs import fake_filesystem_shutil
from pyfakefs import fake_pathlib
from pyfakefs import mox3_stubout
from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir

if pathlib:
from pyfakefs import fake_pathlib

if use_scandir:
from pyfakefs import fake_scandir
Expand Down Expand Up @@ -335,10 +334,6 @@ class Patcher:
IS_WINDOWS = sys.platform in ('win32', 'cygwin')

SKIPNAMES = {'os', 'path', 'io', 'genericpath', OS_MODULE, PATH_MODULE}
if pathlib:
SKIPNAMES.add('pathlib')
if pathlib2:
SKIPNAMES.add('pathlib2')

def __init__(self, additional_skip_names=None,
modules_to_reload=None, modules_to_patch=None,
Expand Down Expand Up @@ -404,18 +399,17 @@ def _init_fake_module_classes(self):
# class modules maps class names against a list of modules they can
# be contained in - this allows for alternative modules like
# `pathlib` and `pathlib2`
self._class_modules['Path'] = []
if pathlib:
self._class_modules['Path'] = []
if pathlib:
self._fake_module_classes[
'pathlib'] = fake_pathlib.FakePathlibModule
self._class_modules['Path'].append('pathlib')
if pathlib2:
self._fake_module_classes[
'pathlib2'] = fake_pathlib.FakePathlibModule
self._class_modules['Path'].append('pathlib2')
self._fake_module_classes[
'Path'] = fake_pathlib.FakePathlibPathModule
'pathlib'] = fake_pathlib.FakePathlibModule
self._class_modules['Path'].append('pathlib')
if pathlib2:
self._fake_module_classes[
'pathlib2'] = fake_pathlib.FakePathlibModule
self._class_modules['Path'].append('pathlib2')
self._fake_module_classes[
'Path'] = fake_pathlib.FakePathlibPathModule
if use_scandir:
self._fake_module_classes[
'scandir'] = fake_scandir.FakeScanDirModule
Expand Down
6 changes: 3 additions & 3 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def lchmod(self, pathobj, mode):

_fake_accessor = _FakeAccessor()

flavour = pathlib._Flavour if pathlib else object
flavour = pathlib._Flavour


class _FakeFlavour(flavour):
Expand Down Expand Up @@ -443,7 +443,7 @@ def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern)).fullmatch


path_module = pathlib.Path if pathlib else object
path_module = pathlib.Path


class FakePath(path_module):
Expand Down Expand Up @@ -649,7 +649,7 @@ class FakePathlibModule:
`fake_pathlib_module = fake_filesystem.FakePathlibModule(filesystem)`
"""

PurePath = pathlib.PurePath if pathlib else object
PurePath = pathlib.PurePath

def __init__(self, filesystem):
"""
Expand Down
16 changes: 5 additions & 11 deletions pyfakefs/tests/all_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@
import sys
import unittest

from pyfakefs.extra_packages import pathlib
from pyfakefs.tests import dynamic_patch_test, fake_stat_time_test
from pyfakefs.tests import fake_open_test
from pyfakefs.tests import fake_os_test
from pyfakefs.tests import example_test
from pyfakefs.tests import fake_filesystem_glob_test
from pyfakefs.tests import fake_filesystem_shutil_test
from pyfakefs.tests import fake_filesystem_test
from pyfakefs.tests import fake_filesystem_unittest_test
from pyfakefs.tests import fake_tempfile_test
from pyfakefs.tests import fake_filesystem_vs_real_test
from pyfakefs.tests import fake_open_test
from pyfakefs.tests import fake_os_test
from pyfakefs.tests import fake_pathlib_test
from pyfakefs.tests import fake_tempfile_test
from pyfakefs.tests import mox3_stubout_test

if pathlib:
from pyfakefs.tests import fake_pathlib_test


class AllTests(unittest.TestSuite):
"""A test suite that runs all tests for pyfakefs at once."""
Expand All @@ -53,11 +50,8 @@ def suite(self): # pylint: disable-msg=C6409
loader.loadTestsFromModule(example_test),
loader.loadTestsFromModule(mox3_stubout_test),
loader.loadTestsFromModule(dynamic_patch_test),
loader.loadTestsFromModule(fake_pathlib_test)
])
if pathlib:
self.addTests([
loader.loadTestsFromModule(fake_pathlib_test)
])
return self


Expand Down
1 change: 0 additions & 1 deletion pyfakefs/tests/dynamic_patch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def test_shutil_patch(self):
self.fs.set_disk_usage(100)
self.assertEqual(100, shutil.disk_usage('/').total)

@unittest.skipIf(not pathlib, 'only run if pathlib is available')
def test_pathlib_path_patch(self):
file_path = 'test.txt'
path = pathlib.Path(file_path)
Expand Down
24 changes: 17 additions & 7 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def test_shutil(self):
shutil.rmtree('/test/dir1')
self.assertFalse(self.fs.exists('/test/dir1'))

@unittest.skipIf(not pathlib, "only run if pathlib is available")
def test_fakepathlib(self):
with pathlib.Path('/fake_file.txt') as p:
with p.open('w') as f:
Expand Down Expand Up @@ -147,12 +146,11 @@ def test_import_path_from_os(self):
self.assertTrue(
pyfakefs.tests.import_as_example.check_if_exists2(file_path))

if pathlib:
def test_import_path_from_pathlib(self):
file_path = '/foo/bar'
self.fs.create_dir(file_path)
self.assertTrue(
pyfakefs.tests.import_as_example.check_if_exists3(file_path))
def test_import_path_from_pathlib(self):
file_path = '/foo/bar'
self.fs.create_dir(file_path)
self.assertTrue(
pyfakefs.tests.import_as_example.check_if_exists3(file_path))

def test_import_function_from_os_path(self):
file_path = '/foo/bar'
Expand Down Expand Up @@ -646,5 +644,17 @@ def test_file_copied_again(self):
self.assertFalse(os.path.isdir('./test2/'))


class PathlibTest(TestCase):
"""Regression test for #527"""

@patchfs
def test_cwd(self, fs):
"""Make sure fake file system is used for os in pathlib"""
self.assertEqual(os.path.sep, str(pathlib.Path.cwd()))
dot_abs = pathlib.Path(".").absolute()
self.assertEqual(os.path.sep, str(dot_abs))
self.assertTrue(dot_abs.exists())


if __name__ == "__main__":
unittest.main()
3 changes: 1 addition & 2 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,5 +1036,4 @@ def test_add_existing_real_directory_with_pathlib_path(self):


if __name__ == '__main__':
if pathlib:
unittest.main()
unittest.main()

0 comments on commit 292c07f

Please sign in to comment.