Skip to content

Commit

Permalink
Added handling for readlink for broken link with trailing separator
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed May 18, 2018
1 parent 51fddae commit 3a17916
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
12 changes: 9 additions & 3 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2567,11 +2567,17 @@ def readlink(self, path):
if self.ends_with_path_separator(path):
if not self.is_windows_fs and self.exists(path):
self.raise_os_error(errno.EINVAL, path)
if not self.is_macos and not self.exists(link_obj.path):
error = errno.EINVAL if self.is_windows_fs else errno.ELOOP
if not self.exists(link_obj.path):
if self.is_windows_fs:
error = errno.EINVAL
elif link_obj.path == link_obj.contents:
if self.is_macos:
return
error = errno.ELOOP
else:
error = errno.ENOENT
self.raise_os_error(error, link_obj.path)


return link_obj.contents

def makedir(self, dir_name, mode=PERM_DEF):
Expand Down
19 changes: 16 additions & 3 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,20 +637,23 @@ def test_broken_symlink_with_trailing_separator_windows(self):
self.assert_raises_os_error(errno.EINVAL, self.os.symlink,
link_path + self.os.sep, link_path + self.os.sep)

def test_circular_readlink_with_trailing_separator_372_linux(self):
def test_circular_readlink_with_trailing_separator_linux(self):
# Regression test for #372
self.check_linux_only()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
self.assert_raises_os_error(errno.ELOOP, self.os.readlink,
file_path + self.os.sep)

def test_circular_readlink_with_trailing_separator_372_macos(self):
def test_circular_readlink_with_trailing_separator_macos(self):
# Regression test for #372
self.check_macos_only()
file_path = self.make_path('foo')
self.os.symlink(file_path, file_path)
print(self.os.readlink(file_path + self.os.sep))

def test_circular_readlink_with_trailing_separator_372_windows(self):
def test_circular_readlink_with_trailing_separator_windows(self):
# Regression test for #372
self.check_windows_only()
self.skip_if_symlink_not_supported()
file_path = self.make_path('foo')
Expand Down Expand Up @@ -2219,6 +2222,16 @@ def test_rename_broken_link_with_trailing_sep_windows(self):
self.assert_raises_os_error(
errno.EINVAL, self.os.rename, link_path, self.make_path('target'))

def test_readlink_broken_link_with_trailing_sep_posix(self):
self.check_posix_only()
link_path = self.create_broken_link_path_with_trailing_sep()
self.assert_raises_os_error(errno.ENOENT, self.os.readlink, link_path)

def test_readlink_broken_link_with_trailing_sep_windows(self):
self.check_windows_only()
link_path = self.create_broken_link_path_with_trailing_sep()
self.assert_raises_os_error(errno.EINVAL, self.os.readlink, link_path)

def test_islink_broken_link_with_trailing_sep(self):
link_path = self.create_broken_link_path_with_trailing_sep()
self.assertFalse(self.os.path.islink(link_path))
Expand Down

0 comments on commit 3a17916

Please sign in to comment.