From 3a17916c8ee513b1eb0b9c66574410882ab1d7bf Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Fri, 18 May 2018 20:20:07 +0200 Subject: [PATCH] Added handling for readlink for broken link with trailing separator - see #396 --- pyfakefs/fake_filesystem.py | 12 +++++++++--- pyfakefs/tests/fake_os_test.py | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index ddeecc7a..93cf284e 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -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): diff --git a/pyfakefs/tests/fake_os_test.py b/pyfakefs/tests/fake_os_test.py index 95896134..544f6816 100644 --- a/pyfakefs/tests/fake_os_test.py +++ b/pyfakefs/tests/fake_os_test.py @@ -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') @@ -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))