Skip to content

Commit 34dbbf7

Browse files
committed
Fixed handling of current path in lresolve() / os.lstat()
- fixes #516
1 parent e801c65 commit 34dbbf7

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ the proposed changes so you can be ready.
2020
functions
2121

2222
#### Fixes
23+
* Fixed handling of relative paths in `lresolve` / `os.lstat`
24+
(see [#516](../../issues/516))
2325
* Fixed handling of byte string paths
2426
(see [#517](../../issues/517))
2527
* Fixed `os.walk` if path ends with path separator

pyfakefs/fake_filesystem.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,8 @@ def absnormpath(self, path):
14421442
cwd = self._matching_string(path, self.cwd)
14431443
if not path:
14441444
path = self.path_separator
1445+
if path == self._matching_string(path, '.'):
1446+
path = cwd
14451447
elif not self._starts_with_root_path(path):
14461448
# Prefix relative paths with cwd, if cwd is not root.
14471449
root_name = self._matching_string(path, self.root.name)
@@ -2002,12 +2004,16 @@ def lresolve(self, path):
20022004
OSError: if the object is not found.
20032005
"""
20042006
path = make_string_path(path)
2007+
if not path:
2008+
raise OSError(errno.ENOENT, path)
20052009
if path == self.root.name:
20062010
# The root directory will never be a link
20072011
return self.root
20082012

20092013
# remove trailing separator
20102014
path = self._path_without_trailing_separators(path)
2015+
if path == self._matching_string(path, '.'):
2016+
path = self.cwd
20112017
path = self._original_path(path)
20122018

20132019
parent_directory, child_name = self.splitpath(path)
@@ -2022,7 +2028,8 @@ def lresolve(self, path):
20222028
self.raise_os_error(errno.ENOENT, path)
20232029
if not parent_obj.st_mode & PERM_READ:
20242030
self.raise_os_error(errno.EACCES, parent_directory)
2025-
return parent_obj.get_entry(child_name)
2031+
return (parent_obj.get_entry(child_name) if child_name
2032+
else parent_obj)
20262033
except KeyError:
20272034
self.raise_os_error(errno.ENOENT, path)
20282035

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)

pyfakefs/tests/fake_os_test.py

+11
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ def test_lstat_with_byte_string(self):
347347
stat_bytes = self.os.lstat(base_path_bytes)
348348
self.assertEqual(stat_bytes, stat_str)
349349

350+
def test_stat_with_current_dir(self):
351+
# regression test for #516
352+
stat_result = self.os.stat('.')
353+
lstat_result = self.os.lstat('.')
354+
self.assertEqual(stat_result, lstat_result)
355+
350356
def test_exists_with_trailing_sep(self):
351357
# regression test for #364
352358
file_path = self.make_path('alpha')
@@ -359,6 +365,11 @@ def test_mkdir_with_trailing_sep(self):
359365
self.os.mkdir(dir_path + self.os.sep + self.os.sep)
360366
self.assertTrue(self.os.path.exists(dir_path))
361367

368+
def test_readlink_empty_path(self):
369+
self.check_posix_only()
370+
self.assert_raises_os_error(errno.ENOENT,
371+
self.os.readlink, '')
372+
362373
def test_readlink_ending_with_sep_posix(self):
363374
# regression test for #359
364375
self.check_posix_only()

0 commit comments

Comments
 (0)