Skip to content

Commit

Permalink
Added handling of missing directory read access in docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Nov 3, 2019
1 parent 4f237ea commit b38ffac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
15 changes: 7 additions & 8 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
from pyfakefs.helpers import (
FakeStatResult, FileBufferIO, IS_PY2, NullFileBufferIO,
is_int_type, is_byte_string, is_unicode_string,
make_string_path, text_type, IS_WIN
)
make_string_path, text_type, IS_WIN,
IN_DOCKER)

__pychecker__ = 'no-reimportself'

Expand Down Expand Up @@ -1973,7 +1973,7 @@ def get_object_from_normpath(self, file_path, check_read_perm=True):
file_path: Specifies target FakeFile object to retrieve, with a
path that has already been normalized/resolved.
check_read_perm: If True, raises OSError if a parent directory
does not have read permission
does not have read permission (MacOS or Docker container only)
Returns:
The FakeFile object corresponding to file_path.
Expand All @@ -1999,7 +1999,8 @@ def get_object_from_normpath(self, file_path, check_read_perm=True):
self.raise_io_error(errno.ENOTDIR, file_path)
self.raise_io_error(errno.ENOENT, file_path)
target_object = target_object.get_entry(component)
if (self.is_macos and check_read_perm and target_object and
if ((self.is_macos or IN_DOCKER) and check_read_perm and
target_object and
not target_object.st_mode & PERM_READ):
self.raise_os_error(errno.EACCES, target_object.path)
except KeyError:
Expand All @@ -2013,7 +2014,7 @@ def get_object(self, file_path, check_read_perm=True):
Args:
file_path: Specifies the target FakeFile object to retrieve.
check_read_perm: If True, raises OSError if a parent directory
does not have read permission under MacOS
does not have read permission (MacOS or Docker container only)
Returns:
The FakeFile object corresponding to `file_path`.
Expand All @@ -2035,7 +2036,7 @@ def resolve(self, file_path, follow_symlinks=True, allow_fd=False,
otherwise the object linked to.
allow_fd: If `True`, `file_path` may be an open file descriptor
check_read_perm: If True, raises OSError if a parent directory
does not have read permission under MacOS
does not have read permission (MacOS or Docker container only)
Returns:
The FakeFile object corresponding to `file_path`.
Expand Down Expand Up @@ -2997,8 +2998,6 @@ def confirmdir(self, target_directory):
else:
error_nr = errno.ENOTDIR
self.raise_os_error(error_nr, target_directory, 267)
# if not directory.st_mode & PERM_READ:
# self.raise_os_error(errno.EACCES, directory)
return directory

def remove(self, path):
Expand Down
1 change: 1 addition & 0 deletions pyfakefs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
IS_PY2 = sys.version_info[0] < 3
IS_PYPY = platform.python_implementation() == 'PyPy'
IS_WIN = sys.platform == 'win32'
IN_DOCKER = os.path.exists('/.dockerenv')

try:
text_type = unicode # Python 2
Expand Down
20 changes: 9 additions & 11 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import time
import unittest

from pyfakefs.helpers import IS_PY2
from pyfakefs.helpers import IS_PY2, IN_DOCKER

from pyfakefs import fake_filesystem
from pyfakefs.fake_filesystem import FakeFileOpen, is_root
Expand Down Expand Up @@ -4864,7 +4864,7 @@ def test_inode(self):
if self.is_windows:
self.skipTest(
'inode seems not to work in scandir module under Windows')
if os.path.exists('/.dockerenv'):
if IN_DOCKER:
self.skipTest(
'inode seems not to work in a Docker container')
self.assertEqual(self.os.stat(self.dir_path).st_ino,
Expand Down Expand Up @@ -5064,16 +5064,14 @@ def setUp(self):
self.create_file(self.file_path)
self.os.chmod(self.dir_path, 0o000)

@unittest.skipIf(TestCase.is_macos, 'Linux behavior')
def test_listdir_unreadable_dir_linux(self):
self.assertEqual(['some_file'], self.os.listdir(self.dir_path))

@unittest.skipIf(not TestCase.is_macos, 'MacOS behavior')
def test_listdir_unreadable_dir_macos(self):
self.assert_raises_os_error(
errno.EACCES, self.os.listdir, self.dir_path)
def test_listdir_unreadable_dir(self):
if self.is_macos or IN_DOCKER:
self.assert_raises_os_error(
errno.EACCES, self.os.listdir, self.dir_path)
else:
self.assertEqual(['some_file'], self.os.listdir(self.dir_path))

def test_stat_file_in_unreadable_dir_linux(self):
def test_stat_file_in_unreadable_dir(self):
self.assertEqual(0, self.os.stat(self.file_path).st_size)


Expand Down
4 changes: 2 additions & 2 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

if pathlib is not None:
from pyfakefs import fake_pathlib, fake_filesystem
from pyfakefs.helpers import text_type
from pyfakefs.helpers import text_type, IN_DOCKER
from pyfakefs.tests.test_utils import RealFsTestCase, TestCase

is_windows = sys.platform == 'win32'
Expand Down Expand Up @@ -430,7 +430,7 @@ def test_iterdir_in_unreadable_dir(self):
self.create_file(file_path)
self.os.chmod(dir_path, 0o000)
iter = self.path(dir_path).iterdir()
if self.is_macos:
if self.is_macos or IN_DOCKER:
self.assert_raises_os_error(errno.EACCES, list, iter)
else:
path = str(list(iter)[0])
Expand Down

0 comments on commit b38ffac

Please sign in to comment.