Skip to content

Commit

Permalink
Added handling for opening a link pointing to a path ending with sep
Browse files Browse the repository at this point in the history
- fixes #397
  • Loading branch information
mrbean-bremen committed May 26, 2018
1 parent 42ea6a0 commit 98d3524
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4772,7 +4772,13 @@ def call(self, file_, mode='r', buffering=-1, encoding=None,
else:
if open_modes.must_exist:
error_fct(errno.ENOENT, file_path)
if self.filesystem.ends_with_path_separator(file_path):
if self.filesystem.islink(file_path):
link_object = self.filesystem.resolve(file_path,
follow_symlinks=False)
target_path = link_object.contents
else:
target_path = file_path
if self.filesystem.ends_with_path_separator(target_path):
error = (errno.EINVAL if self.filesystem.is_windows_fs
else errno.ENOENT if self.filesystem.is_macos
else errno.EISDIR)
Expand Down
23 changes: 22 additions & 1 deletion pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,28 @@ def test_rename_link_with_trailing_sep_to_self_posix(self):
self.assert_raises_os_error(
errno.ENOTDIR, self.os.rename, path + self.os.sep, path)

# hard link related tests
def check_open_broken_symlink_to_path_with_trailing_sep(self, error):
# Regression tests for #397
self.skip_if_symlink_not_supported()
target_path = self.make_path('target') + self.os.sep
link_path = self.make_path('link')
self.os.symlink(target_path, link_path)
self.assert_raises_io_error(error, self.open, link_path, 'a')
self.assert_raises_io_error(error, self.open, link_path, 'w')

def test_open_broken_symlink_to_path_with_trailing_sep_linux(self):
self.check_linux_only()
self.check_open_broken_symlink_to_path_with_trailing_sep(errno.EISDIR)

def test_open_broken_symlink_to_path_with_trailing_sep_macos(self):
self.check_macos_only()
self.check_open_broken_symlink_to_path_with_trailing_sep(errno.ENOENT)

def test_open_broken_symlink_to_path_with_trailing_sep_windows(self):
self.check_windows_only()
self.check_open_broken_symlink_to_path_with_trailing_sep(errno.EINVAL)

# hard link related tests
def test_link_bogus(self):
# trying to create a link from a non-existent file should fail
self.skip_if_symlink_not_supported()
Expand Down

0 comments on commit 98d3524

Please sign in to comment.