Skip to content

Commit

Permalink
Added handling for creating broken symlinks with trailing separator
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed May 4, 2018
1 parent 01d9913 commit e07381d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2459,8 +2459,20 @@ def create_symlink(self, file_path, link_target, create_missing_dirs=True):
if self.ends_with_path_separator(file_path):
if self.exists(file_path):
self.raise_os_error(errno.EEXIST, file_path)
if not self.is_windows_fs:
self.raise_os_error(errno.ENOENT, file_path)
if self.exists(link_target):
if not self.is_windows_fs:
self.raise_os_error(errno.ENOENT, file_path)
else:
if self.is_windows_fs:
self.raise_os_error(errno.EINVAL, link_target)
elif self.is_macos:
# to avoid EEXIST exception, remove the link
# if it already exists
if self.exists(file_path, check_link=True):
self.remove_object(file_path)
else:
self.raise_os_error(errno.EEXIST, link_target)


# resolve the link path only if it is not a link itself
if not self.islink(file_path):
Expand Down
25 changes: 25 additions & 0 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,31 @@ def test_readlink_raises_if_path_is_none(self):
self.skip_if_symlink_not_supported()
self.assertRaises(TypeError, self.os.readlink, None)

def test_broken_symlink_with_trailing_separator_linux(self):
self.check_linux_only()
file_path = self.make_path('foo')
link_path = self.make_path('link')
self.os.symlink(file_path, link_path)
self.assert_raises_os_error(errno.EEXIST, self.os.symlink,
link_path + self.os.sep, link_path + self.os.sep)

def test_broken_symlink_with_trailing_separator_macos(self):
# regression test for #371
self.check_macos_only()
file_path = self.make_path('foo')
link_path = self.make_path('link')
self.os.symlink(file_path, link_path)
self.os.symlink(link_path + self.os.sep, link_path + self.os.sep)

def test_broken_symlink_with_trailing_separator_windows(self):
self.check_windows_only()
self.skip_if_symlink_not_supported()
file_path = self.make_path('foo')
link_path = self.make_path('link')
self.os.symlink(file_path, link_path)
self.assert_raises_os_error(errno.EINVAL, self.os.symlink,
link_path + self.os.sep, link_path + self.os.sep)

def test_readlink_with_links_in_path(self):
self.skip_if_symlink_not_supported()
self.create_symlink(self.make_path('meyer', 'lemon', 'pie'),
Expand Down

0 comments on commit e07381d

Please sign in to comment.