Skip to content

Commit 5c71f47

Browse files
committed
Fixed handling of current path in lresolve() / os.lstat()
- fixes #516 - back-ported from master
1 parent 1e92a3e commit 5c71f47

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

Diff for: CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ the proposed changes so you can be ready.
1212
This version backports some fixes from master.
1313

1414
#### Fixes
15+
* Fixed handling of relative paths in `lresolve` / `os.lstat`
16+
(see [#516](../../issues/516))
1517
* Fixed `os.walk` if path ends with path separator
1618
(see [#512](../../issues/512))
1719
* Fixed handling of empty path in `os.makedirs`

Diff for: pyfakefs/fake_filesystem.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,8 @@ def absnormpath(self, path):
15111511
cwd = self._matching_string(path, self.cwd)
15121512
if not path:
15131513
path = self.path_separator
1514+
if path == self._matching_string(path, '.'):
1515+
path = cwd
15141516
elif not self._starts_with_root_path(path):
15151517
# Prefix relative paths with cwd, if cwd is not root.
15161518
root_name = self._matching_string(path, self.root.name)
@@ -2082,12 +2084,16 @@ def lresolve(self, path):
20822084
IOError: if the object is not found.
20832085
"""
20842086
path = make_string_path(path)
2087+
if not path:
2088+
raise OSError(errno.ENOENT, path)
20852089
if path == self.root.name:
20862090
# The root directory will never be a link
20872091
return self.root
20882092

20892093
# remove trailing separator
20902094
path = self._path_without_trailing_separators(path)
2095+
if path == self._matching_string(path, '.'):
2096+
path = self.cwd
20912097
path = self._original_path(path)
20922098

20932099
parent_directory, child_name = self.splitpath(path)
@@ -2102,7 +2108,8 @@ def lresolve(self, path):
21022108
self.raise_io_error(errno.ENOENT, path)
21032109
if not parent_obj.st_mode & PERM_READ:
21042110
self.raise_os_error(errno.EACCES, parent_directory)
2105-
return parent_obj.get_entry(child_name)
2111+
return (parent_obj.get_entry(child_name) if child_name
2112+
else parent_obj)
21062113
except KeyError:
21072114
self.raise_io_error(errno.ENOENT, path)
21082115

Diff for: pyfakefs/tests/fake_filesystem_vs_real_test.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ def _compare_behaviors(self, method_name, path, real, fake,
172172
# pylint: disable=C6403
173173

174174
def _error_class(exc):
175-
return (exc and exc.__class__.__name__) or 'None'
175+
if exc:
176+
if hasattr(exc, 'errno'):
177+
return '{}({})'.format(exc.__class__.__name__, exc.errno)
178+
return exc.__class__.__name__
179+
return 'None'
176180

177181
real_err, real_value = self._get_real_value(method_name, path, real)
178182
fake_err, fake_value = self._get_fake_value(method_name, path, fake)

Diff for: pyfakefs/tests/fake_os_test.py

+23
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,24 @@ def test_lstat_trailing_sep(self):
350350
self.assertEqual(stat, self.os.lstat(
351351
self.base_path + self.path_separator() + self.path_separator()))
352352

353+
def test_stat_with_byte_string(self):
354+
stat_str = self.os.stat(self.base_path)
355+
base_path_bytes = self.base_path.encode('utf8')
356+
stat_bytes = self.os.stat(base_path_bytes)
357+
self.assertEqual(stat_bytes, stat_str)
358+
359+
def test_lstat_with_byte_string(self):
360+
stat_str = self.os.lstat(self.base_path)
361+
base_path_bytes = self.base_path.encode('utf8')
362+
stat_bytes = self.os.lstat(base_path_bytes)
363+
self.assertEqual(stat_bytes, stat_str)
364+
365+
def test_stat_with_current_dir(self):
366+
# regression test for #516
367+
stat_result = self.os.stat('.')
368+
lstat_result = self.os.lstat('.')
369+
self.assertEqual(stat_result, lstat_result)
370+
353371
def test_exists_with_trailing_sep(self):
354372
# regression test for #364
355373
file_path = self.make_path('alpha')
@@ -362,6 +380,11 @@ def test_mkdir_with_trailing_sep(self):
362380
self.os.mkdir(dir_path + self.os.sep + self.os.sep)
363381
self.assertTrue(self.os.path.exists(dir_path))
364382

383+
def test_readlink_empty_path(self):
384+
self.check_posix_only()
385+
self.assert_raises_os_error(errno.ENOENT,
386+
self.os.readlink, '')
387+
365388
def test_readlink_ending_with_sep_posix(self):
366389
# regression test for #359
367390
self.check_posix_only()

0 commit comments

Comments
 (0)