Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more OS specific handling for file paths ending with separator #382

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,8 @@ def stat(self, entry_path, follow_symlinks=True):
try:
file_object = self.resolve(
entry_path, follow_symlinks, allow_fd=True)
self.raise_for_filepath_ending_with_separator(entry_path,
file_object)
self.raise_for_filepath_ending_with_separator(
entry_path, file_object, follow_symlinks)

return file_object.stat_result.copy()
except IOError as io_error:
Expand All @@ -1074,12 +1074,29 @@ def stat(self, entry_path, follow_symlinks=True):
self.raise_os_error(io_error.errno, entry_path, winerror=winerror)

def raise_for_filepath_ending_with_separator(self, entry_path,
file_object):
if (self.ends_with_path_separator(entry_path) and
S_ISREG(file_object.st_mode)):
error_nr = (errno.EINVAL if self.is_windows_fs
else errno.ENOTDIR)
self.raise_os_error(error_nr, entry_path)
file_object,
follow_symlinks=True,
macos_handling=False):
if self.ends_with_path_separator(entry_path):
if S_ISLNK(file_object.st_mode):
try:
link_object = self.resolve(entry_path)
except (IOError, OSError):
if self.is_macos:
return
raise
if not follow_symlinks or self.is_windows_fs or self.is_macos:
file_object = link_object
if self.is_windows_fs:
is_error = S_ISREG(file_object.st_mode)
elif self.is_macos and macos_handling:
is_error = not S_ISLNK(file_object.st_mode)
else:
is_error = not S_ISDIR(file_object.st_mode)
if is_error:
error_nr = (errno.EINVAL if self.is_windows_fs
else errno.ENOTDIR)
self.raise_os_error(error_nr, entry_path)

def chmod(self, path, mode, follow_symlinks=True):
"""Change the permissions of a file as encoded in integer mode.
Expand Down Expand Up @@ -1603,11 +1620,20 @@ def ends_with_path_separator(self, file_path):
return False
file_path = make_string_path(file_path)
return (file_path and
file_path not in (self.path_separator,
self.alternative_path_separator) and
(file_path.endswith(self._path_separator(file_path)) or
self.alternative_path_separator is not None and
file_path.endswith(
self._alternative_path_separator(file_path))))

def is_filepath_ending_with_separator(self, path):
if not self.ends_with_path_separator(path):
return False
while self.ends_with_path_separator(path):
path = path[:-1]
return self.isfile(path)

def _directory_content(self, directory, component):
if not isinstance(directory, FakeDirectory):
return None, None
Expand Down Expand Up @@ -1642,8 +1668,7 @@ def exists(self, file_path, check_link=False):
if not file_path:
return False
try:
if (self.ends_with_path_separator(file_path) and
self.isfile(file_path)):
if self.is_filepath_ending_with_separator(file_path):
return False
file_path = self.resolve_path(file_path)
except (IOError, OSError):
Expand Down Expand Up @@ -2621,8 +2646,10 @@ def _is_of_type(self, path, st_flag, follow_symlinks=True):
raise TypeError
try:
obj = self.resolve(path, follow_symlinks,
allow_trailing_separator=False)
allow_trailing_separator=True)
if obj:
self.raise_for_filepath_ending_with_separator(
path, obj, macos_handling=True)
return S_IFMT(obj.st_mode) == st_flag
except (IOError, OSError):
return False
Expand Down
74 changes: 69 additions & 5 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,18 @@ def test_read_link_ending_with_sep_posix(self):
self.assert_raises_os_error(errno.EINVAL,
self.os.readlink, link_path + self.os.sep)

def test_lstat_symlink_with_trailing_sep(self):
def test_lstat_symlink_with_trailing_sep_linux(self):
# regression test for #366
self.check_linux_only()
self.skip_if_symlink_not_supported()
link_path = self.make_path('foo')
self.os.symlink(self.base_path, link_path)
# used to raise
self.assertTrue(self.os.lstat(link_path + self.os.sep).st_mode)

def test_lstat_symlink_with_trailing_sep_macos(self):
# regression test for #366
self.check_macos_only()
self.skip_if_symlink_not_supported()
link_path = self.make_path('foo')
self.os.symlink(self.base_path, link_path)
Expand All @@ -377,18 +387,31 @@ def test_read_link_ending_with_sep_windows(self):
self.assertEqual(self.base_path,
self.os.readlink(link_path + self.os.sep))

def test_symlink_with_trailing_sep_windows(self):
def test_islink_with_trailing_sep_windows(self):
self.check_windows_only()
self.skip_if_symlink_not_supported()
link_path = self.make_path('foo')
self.os.symlink(self.base_path, link_path)
self.assertTrue(self.os.path.islink(link_path + self.os.path.sep))

def test_symlink_with_trailing_sep_posix(self):
self.check_posix_only()
def test_islink_with_trailing_sep_linux(self):
self.check_linux_only()
link_path = self.make_path('foo')
self.os.symlink(self.base_path, link_path)
self.assertFalse(self.os.path.islink(link_path + self.os.sep))

def test_islink_with_trailing_sep_macos(self):
self.check_macos_only()
link_path = self.make_path('foo')
self.os.symlink(self.base_path, link_path)
self.assertFalse(self.os.path.islink(link_path + self.os.path.sep))
self.assertFalse(self.os.path.islink(link_path + self.os.sep))

def test_islink_with_trailing_separator_macos(self):
# regression test for #373
self.check_macos_only()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
self.assertTrue(self.os.path.islink(file_path + self.os.sep))

def check_getsize_raises_with_trailing_separator(self, error_nr):
file_path = self.make_path('bar')
Expand Down Expand Up @@ -472,6 +495,47 @@ def test_open_raises_with_trailing_separator_windows(self):
self.check_windows_only()
self.check_open_raises_with_trailing_separator(errno.EINVAL)

def test_lexists_with_trailing_separator_linux_windows(self):
self.check_linux_and_windows()
self.skip_if_symlink_not_supported()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
self.assertFalse(self.os.path.lexists(file_path + self.os.sep))

def test_lexists_with_trailing_separator_macos(self):
# regression test for #373
self.check_macos_only()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
self.assertTrue(self.os.path.lexists(file_path + self.os.sep))

def test_islink_with_trailing_separator_linux_windows(self):
self.check_linux_and_windows()
self.skip_if_symlink_not_supported()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
self.assertFalse(self.os.path.islink(file_path + self.os.sep))

def test_islink_with_trailing_separator_macos(self):
# regression test for #373
self.check_macos_only()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
self.assertTrue(self.os.path.islink(file_path + self.os.sep))

def test_isfile_with_trailing_separator_linux_windows(self):
self.check_linux_and_windows()
file_path = self.make_path('foo')
self.create_file(file_path)
self.assertFalse(self.os.path.isfile(file_path + self.os.sep))

def test_isfile_with_trailing_separator_macos(self):
# regression test for #374
self.check_macos_only()
file_path = self.make_path('foo')
self.create_file(file_path)
self.assertFalse(self.os.path.isfile(file_path + self.os.sep))

def check_stat_with_trailing_separator(self, error_nr):
# regression test for #376
file_path = self.make_path('foo')
Expand Down