Skip to content
Permalink
Browse files

Make sure pathlib2 and scandir package are used if available

- adapted tests to test pathlib2 and scandir package in all versions
- see #462
  • Loading branch information...
mrbean-bremen committed Jan 2, 2019
1 parent 81af553 commit ebd0f46af632652664786c98f13a3544c36d55c3
@@ -8,6 +8,6 @@
# Older versions might work ok, the versions chosen here are just the latest
# available at the time of writing.

pathlib2>=2.3.2;python_version<'3.4'
pathlib2>=2.3.2

scandir>=1.8;python_version<'3.5'
scandir>=1.8
@@ -16,25 +16,32 @@

try:
import pathlib2

pathlib = None
except ImportError:
try:
import pathlib

pathlib2 = None
except ImportError:
pathlib = None
pathlib2 = None

use_pathlib = pathlib or pathlib2

try:
import scandir
use_scandir = True

use_scandir_package = True
use_builtin_scandir = False
except ImportError:
try:
from os import scandir
use_scandir = True

use_builtin_scandir = True
use_scandir_package = False
except ImportError:
use_scandir = False
use_builtin_scandir = False
use_scandir_package = False

use_scandir = use_scandir_package or use_builtin_scandir
@@ -66,9 +66,9 @@
from pyfakefs import fake_filesystem
from pyfakefs import fake_filesystem_shutil
from pyfakefs import mox3_stubout
from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir
from pyfakefs.extra_packages import pathlib, pathlib2, use_scandir, use_pathlib

if pathlib:
if use_pathlib:
from pyfakefs import fake_pathlib

if use_scandir:
@@ -313,6 +313,8 @@ class Patcher(object):
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, use_dynamic_patch=True,
@@ -356,12 +358,12 @@ def __init__(self, additional_skip_names=None,
# be contained in - this allows for alternative modules like
# `pathlib` and `pathlib2`
self._class_modules = {}
if pathlib:
if use_pathlib:
mod_name = 'pathlib2' if pathlib2 is not None else 'pathlib'
self._fake_module_classes[
'pathlib'] = fake_pathlib.FakePathlibModule
mod_name] = fake_pathlib.FakePathlibModule
self._fake_module_classes[
'Path'] = fake_pathlib.FakePathlibPathModule
mod_name = 'pathlib2' if pathlib2 is not None else 'pathlib'
'Path'] = fake_pathlib.FakePathlibPathModule
self._class_modules['Path'] = [mod_name]
if use_scandir:
self._fake_module_classes[
@@ -47,6 +47,9 @@
from pyfakefs.helpers import text_type
from pyfakefs.fake_filesystem import FakeFileOpen, FakeFilesystem

if pathlib is None:
pathlib = pathlib2


def init_module(filesystem):
"""Initializes the fake module with the fake file system."""
@@ -21,16 +21,25 @@
from pyfakefs import extra_packages

if extra_packages.pathlib2:
extra_packages.pathlib = None
extra_packages.pathlib2 = None
try:
import pathlib
except ImportError:
pathlib = None
extra_packages.pathlib = pathlib
extra_packages.use_pathlib = pathlib

if extra_packages.use_scandir_package:
extra_packages.use_scandir = False
extra_packages.use_scandir_package = False
try:
from os import scandir
except ImportError:
scandir = None
extra_packages.scandir = scandir
extra_packages.use_scandir = scandir

from pyfakefs.tests.all_tests import AllTests # noqa: E402


if __name__ == '__main__':
result = unittest.TextTestRunner(verbosity=2).run(AllTests().suite())
sys.exit(int(not result.wasSuccessful()))
@@ -17,13 +17,14 @@
import unittest

from pyfakefs import fake_filesystem_unittest
from pyfakefs.extra_packages import pathlib
from pyfakefs.extra_packages import pathlib, pathlib2, use_pathlib


class TestPyfakefsUnittestBase(fake_filesystem_unittest.TestCase):
def setUp(self):
"""Set up the fake file system"""
self.setUpPyfakefs()
self.pathlib = pathlib or pathlib2


@unittest.skipIf((3, ) < sys.version_info < (3, 3),
@@ -61,21 +62,21 @@ 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')
@unittest.skipIf(not use_pathlib, 'only run if pathlib is available')
def test_pathlib_patch(self):
file_path = 'test.txt'
path = pathlib.Path(file_path)
path = self.pathlib.Path(file_path)
with path.open('w') as f:
f.write('test')

self.assertTrue(self.fs.exists(file_path))
file_object = self.fs.get_object(file_path)
self.assertEqual('test', file_object.contents)

@unittest.skipIf(not pathlib, 'only run if pathlib is available')
@unittest.skipIf(not use_pathlib, 'only run if pathlib is available')
def test_pathlib_path_patch(self):
file_path = 'test.txt'
path = pathlib.Path(file_path)
path = self.pathlib.Path(file_path)
with path.open('w') as f:
f.write('test')

@@ -28,7 +28,7 @@
from unittest import TestCase

from pyfakefs import fake_filesystem_unittest, fake_filesystem
from pyfakefs.extra_packages import pathlib
from pyfakefs.extra_packages import pathlib, pathlib2, use_pathlib
from pyfakefs.fake_filesystem_unittest import Patcher, Pause
import pyfakefs.tests.import_as_example
from pyfakefs.helpers import IS_PYPY, IS_PY2
@@ -125,9 +125,10 @@ 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")
@unittest.skipIf(not use_pathlib, "only run if pathlib is available")
def test_fakepathlib(self):
with pathlib.Path('/fake_file.txt') as p:
imported_pathlib = pathlib or pathlib2
with imported_pathlib.Path('/fake_file.txt') as p:
with p.open('w') as f:
f.write('text')
is_windows = sys.platform.startswith('win')
@@ -52,7 +52,7 @@ def skip_if_pathlib_36_is_available():
class RealPathlibTestCase(RealFsTestCase):
def __init__(self, methodName='runTest'):
super(RealPathlibTestCase, self).__init__(methodName)
self.pathlib = pathlib
self.pathlib = pathlib or pathlib2
self.path = None

def setUp(self):
@@ -13,9 +13,13 @@
# Used as SUT for pytest_fixture_test.py

try:
from pathlib import Path
from pathlib2 import Path

EXAMPLE_FILE = Path('/test') / 'file'

except ImportError:
EXAMPLE_FILE = None
try:
from pathlib import Path

EXAMPLE_FILE = Path('/test') / 'file'
except ImportError:
EXAMPLE_FILE = None

0 comments on commit ebd0f46

Please sign in to comment.
You can’t perform that action at this time.