Skip to content

Commit

Permalink
Fixed handling of current path in lresolve() / os.lstat()
Browse files Browse the repository at this point in the history
- fixes #516
  • Loading branch information
mrbean-bremen committed Feb 19, 2020
1 parent e801c65 commit 34dbbf7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ the proposed changes so you can be ready.
functions

#### Fixes
* Fixed handling of relative paths in `lresolve` / `os.lstat`
(see [#516](../../issues/516))
* Fixed handling of byte string paths
(see [#517](../../issues/517))
* Fixed `os.walk` if path ends with path separator
Expand Down
9 changes: 8 additions & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,8 @@ def absnormpath(self, path):
cwd = self._matching_string(path, self.cwd)
if not path:
path = self.path_separator
if path == self._matching_string(path, '.'):
path = cwd
elif not self._starts_with_root_path(path):
# Prefix relative paths with cwd, if cwd is not root.
root_name = self._matching_string(path, self.root.name)
Expand Down Expand Up @@ -2002,12 +2004,16 @@ def lresolve(self, path):
OSError: if the object is not found.
"""
path = make_string_path(path)
if not path:
raise OSError(errno.ENOENT, path)
if path == self.root.name:
# The root directory will never be a link
return self.root

# remove trailing separator
path = self._path_without_trailing_separators(path)
if path == self._matching_string(path, '.'):
path = self.cwd
path = self._original_path(path)

parent_directory, child_name = self.splitpath(path)
Expand All @@ -2022,7 +2028,8 @@ def lresolve(self, path):
self.raise_os_error(errno.ENOENT, path)
if not parent_obj.st_mode & PERM_READ:
self.raise_os_error(errno.EACCES, parent_directory)
return parent_obj.get_entry(child_name)
return (parent_obj.get_entry(child_name) if child_name
else parent_obj)
except KeyError:
self.raise_os_error(errno.ENOENT, path)

Expand Down
6 changes: 5 additions & 1 deletion pyfakefs/tests/fake_filesystem_vs_real_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ def _compare_behaviors(self, method_name, path, real, fake,
# pylint: disable=C6403

def _error_class(exc):
return (exc and exc.__class__.__name__) or 'None'
if exc:
if hasattr(exc, 'errno'):
return '{}({})'.format(exc.__class__.__name__, exc.errno)
return exc.__class__.__name__
return 'None'

real_err, real_value = self._get_real_value(method_name, path, real)
fake_err, fake_value = self._get_fake_value(method_name, path, fake)
Expand Down
11 changes: 11 additions & 0 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ def test_lstat_with_byte_string(self):
stat_bytes = self.os.lstat(base_path_bytes)
self.assertEqual(stat_bytes, stat_str)

def test_stat_with_current_dir(self):
# regression test for #516
stat_result = self.os.stat('.')
lstat_result = self.os.lstat('.')
self.assertEqual(stat_result, lstat_result)

def test_exists_with_trailing_sep(self):
# regression test for #364
file_path = self.make_path('alpha')
Expand All @@ -359,6 +365,11 @@ def test_mkdir_with_trailing_sep(self):
self.os.mkdir(dir_path + self.os.sep + self.os.sep)
self.assertTrue(self.os.path.exists(dir_path))

def test_readlink_empty_path(self):
self.check_posix_only()
self.assert_raises_os_error(errno.ENOENT,
self.os.readlink, '')

def test_readlink_ending_with_sep_posix(self):
# regression test for #359
self.check_posix_only()
Expand Down

0 comments on commit 34dbbf7

Please sign in to comment.