diff --git a/CHANGES.md b/CHANGES.md index d0b6e6d1..f8b8ec5f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,10 @@ the proposed changes so you can be ready. ## Version 3.7 (as yet unreleased) +### Fixes + * raise for `os.scandir` with non-existing directory + (see [#498](../../issues/498)) + ## [Version 3.6.1](https://pypi.python.org/pypi/pyfakefs/3.6.1) ### Fixes diff --git a/pyfakefs/fake_scandir.py b/pyfakefs/fake_scandir.py index 3b9271e9..1645c0ff 100644 --- a/pyfakefs/fake_scandir.py +++ b/pyfakefs/fake_scandir.py @@ -16,10 +16,12 @@ and the standalone function available in the standalone `scandir` python package. """ +import errno import os import sys from pyfakefs.extra_packages import use_scandir_package, use_builtin_scandir +from pyfakefs.helpers import IS_PY2 if sys.version_info >= (3, 6) and use_builtin_scandir: BaseClass = os.PathLike @@ -132,11 +134,15 @@ def __init__(self, filesystem, path): else: self.abspath = self.filesystem.absnormpath(path) self.path = path - contents = {} try: contents = self.filesystem.confirmdir(self.abspath).contents - except OSError: - pass + except OSError as ex: + if IS_PY2 and self.filesystem.is_windows_fs: + if ex.errno == errno.ENOENT: + # for some reason, under Python 2 / Windows + # raises "No such process" for non-existing path + raise OSError(errno.ESRCH, str(ex), ex.filename) + raise self.contents_iter = iter(contents) def __iter__(self): diff --git a/pyfakefs/tests/fake_os_test.py b/pyfakefs/tests/fake_os_test.py index bf06c3ff..8956ca9c 100644 --- a/pyfakefs/tests/fake_os_test.py +++ b/pyfakefs/tests/fake_os_test.py @@ -23,6 +23,8 @@ import time import unittest +from pyfakefs.helpers import IS_PY2 + from pyfakefs import fake_filesystem from pyfakefs.fake_filesystem import FakeFileOpen, is_root from pyfakefs.extra_packages import ( @@ -111,6 +113,8 @@ def test_get_cwd(self): self.assertEqual(self.os.getcwd(), dirname) def test_listdir(self): + self.assert_raises_os_error( + errno.ENOENT, self.os.listdir, 'non_existing/fake_dir') directory = self.make_path('xyzzy', 'plugh') files = ['foo', 'bar', 'baz'] for f in files: @@ -4941,6 +4945,18 @@ def test_path_like(self): self.assertEqual(self.os.path.join(self.scandir_path(), 'file'), os.fspath(self.dir_entries[1])) + @unittest.skipIf(IS_PY2 and TestCase.is_windows, + 'Exception subtype differs') + def test_non_existing_dir(self): + self.assert_raises_os_error( + errno.ENOENT, self.scandir, 'non_existing/fake_dir') + + @unittest.skipIf(not IS_PY2 or not TestCase.is_windows, + 'Exception subtype differs') + def test_non_existing_dir(self): + self.assert_raises_os_error( + errno.ESRCH, self.scandir, 'non_existing/fake_dir') + class RealScandirTest(FakeScandirTest): def use_real_fs(self):