From c7f463d21359e74598e7d5c591196717f61e8662 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Sat, 18 May 2019 20:32:17 +0200 Subject: [PATCH] Make sure fake DirEntry implements os.PathLike if needed - for Python >= 3.6 and if using os.scandir (e.g. not the scandir package) - fixes #483 --- CHANGES.md | 2 ++ pyfakefs/fake_scandir.py | 14 ++++++++++++-- pyfakefs/tests/fake_os_test.py | 11 ++++++++++- pyfakefs/tests/test_utils.py | 4 ++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c9b8430a..ebfb3422 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ The release versions are PyPi releases. * added basic support for `os.pipe` (see [#473](../../issues/473)) #### Fixes + * fake `DirEntry` now implements `os.PathLike` in Python >= 3.6 + (see [#483](../../issues/483)) * fixed incorrect argument name for `os.makedirs` (see [#481](../../issues/481)) * avoid pytest warning under Python 2.7 (see [#466](../../issues/466)) diff --git a/pyfakefs/fake_scandir.py b/pyfakefs/fake_scandir.py index 9a528038..03df0ffa 100644 --- a/pyfakefs/fake_scandir.py +++ b/pyfakefs/fake_scandir.py @@ -16,12 +16,18 @@ and the standalone function available in the standalone `scandir` python package. """ +import os import sys -from pyfakefs.extra_packages import use_scandir_package +from pyfakefs.extra_packages import use_scandir_package, use_builtin_scandir +if sys.version_info >= (3, 6) and use_builtin_scandir: + BaseClass = os.PathLike +else: + BaseClass = object -class DirEntry(object): + +class DirEntry(BaseClass): """Emulates os.DirEntry. Note that we did not enforce keyword only arguments.""" @@ -101,6 +107,10 @@ def stat(self, follow_symlinks=True): self._statresult = file_object.stat_result.copy() return self._statresult + if sys.version_info >= (3, 6) and use_builtin_scandir: + def __fspath__(self): + return self.path + class ScanDirIter: """Iterator for DirEntry objects returned from `scandir()` diff --git a/pyfakefs/tests/fake_os_test.py b/pyfakefs/tests/fake_os_test.py index bdfc286d..cd947435 100644 --- a/pyfakefs/tests/fake_os_test.py +++ b/pyfakefs/tests/fake_os_test.py @@ -25,7 +25,9 @@ from pyfakefs import fake_filesystem from pyfakefs.fake_filesystem import FakeFileOpen, is_root -from pyfakefs.extra_packages import use_scandir, use_scandir_package +from pyfakefs.extra_packages import ( + use_scandir, use_scandir_package, use_builtin_scandir +) from pyfakefs.tests.test_utils import DummyTime, TestCase, RealFsTestCase @@ -4823,6 +4825,13 @@ def test_stat_ino_dev(self): self.assertEqual(file_stat.st_dev, self.dir_entries[3].stat().st_dev) + @unittest.skipIf(sys.version_info < (3, 6) or not use_builtin_scandir, + 'Path-like objects have been introduced in Python 3.6') + def test_path_like(self): + self.assertTrue(isinstance(self.dir_entries[0], os.PathLike)) + self.assertEqual(self.dir_path, os.fspath(self.dir_entries[0])) + self.assertEqual(self.file_path, os.fspath(self.dir_entries[1])) + class RealScandirTest(FakeScandirTest): def use_real_fs(self): diff --git a/pyfakefs/tests/test_utils.py b/pyfakefs/tests/test_utils.py index 10298dd3..22c6ca26 100644 --- a/pyfakefs/tests/test_utils.py +++ b/pyfakefs/tests/test_utils.py @@ -369,8 +369,8 @@ def setUp(self): self.open = fake_filesystem.FakeFileOpen(self.filesystem) self.os = fake_filesystem.FakeOsModule(self.filesystem) self.create_basepath() - elif not os.environ.get('TEST_REAL_FS'): - self.skip_real_fs() + # elif not os.environ.get('TEST_REAL_FS'): + # self.skip_real_fs() self.setUpFileSystem()