Skip to content

Commit

Permalink
Make sure fake DirEntry implements os.PathLike if needed
Browse files Browse the repository at this point in the history
- for Python >= 3.6 and if using os.scandir
  (e.g. not the scandir package)
- fixes #483
  • Loading branch information
mrbean-bremen committed May 20, 2019
1 parent b18174b commit c7f463d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 12 additions & 2 deletions pyfakefs/fake_scandir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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()`
Expand Down
11 changes: 10 additions & 1 deletion pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pyfakefs/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c7f463d

Please sign in to comment.