Skip to content

Commit

Permalink
Make sure symlinks are considered in some functions
Browse files Browse the repository at this point in the history
- resolve path for os.path.size and os.path.getmtime/getatime/getctime
- see #210
  • Loading branch information
mrbean-bremen committed Jun 17, 2017
1 parent 1924447 commit 7e03f13
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -12,6 +12,7 @@ The release versions are PyPi releases.
#### Infrastructure

#### Fixes
* Incorrect handling of symlinks in `os.path.size` and other functions ([#210](../../issues/210)).
* Failed to create directory with symlink as parent ([#215](../../issues/215)).
* Incorrect error handling during directory creation ([#209](../../issues/209)).
* Creating files in read-only directory was possible ([#203](../../issues/203)).
Expand Down
16 changes: 13 additions & 3 deletions fake_filesystem_test.py
Expand Up @@ -632,6 +632,15 @@ def testGetsize(self):
self.filesystem.CreateFile(file_path, contents='1234567')
self.assertEqual(7, self.path.getsize('FOO/BAR/BAZ'))

def testGetsizeWithLoopingSymlink(self):
self.filesystem.is_windows_fs = False
dir_path = '/foo/bar'
self.filesystem.CreateDirectory(dir_path)
link_path = dir_path + "/link"
link_target = link_path + "/link"
self.os.symlink(link_target, link_path)
self.assertRaisesOSError(errno.ELOOP, self.os.path.getsize, link_path)

def testGetMtime(self):
test_file = self.filesystem.CreateFile('foo/bar1.txt')
test_file.SetMTime(24)
Expand Down Expand Up @@ -1458,6 +1467,7 @@ def testMkdirRaisesIfParentIsReadOnly(self):
self.assertRaises(Exception, self.os.mkdir, directory)

def testMkdirWithWithSymlinkParent(self):
self.filesystem.is_windows_fs = False
dir_path = '/foo/bar'
self.filesystem.CreateDirectory(dir_path)
link_path = '/foo/link'
Expand Down Expand Up @@ -1494,9 +1504,9 @@ def testMakedirsRaisesIfParentIsBrokenLink(self):
def testMakedirsRaisesIfParentIsLoopingLink(self):
dir_path = '/foo/bar'
self.filesystem.CreateDirectory(dir_path)
link_target = dir_path + "/link"
link_path = link_target + "/link"
self.os.symlink(link_path, link_target)
link_path = dir_path + "/link"
link_target = link_path + "/link"
self.os.symlink(link_target, link_path)
self.assertRaisesOSError(errno.ELOOP, self.os.makedirs, link_path)

def testMakedirsRaisesIfAccessDenied(self):
Expand Down
8 changes: 4 additions & 4 deletions pyfakefs/fake_filesystem.py
Expand Up @@ -2644,7 +2644,7 @@ def getsize(self, path):
file size in bytes.
"""
try:
file_obj = self.filesystem.GetObject(path)
file_obj = self.filesystem.ResolveObject(path)
return file_obj.st_size
except IOError as exc:
raise os.error(exc.errno, exc.strerror)
Expand Down Expand Up @@ -2698,7 +2698,7 @@ def getmtime(self, path):
OSError: if the file does not exist.
"""
try:
file_obj = self.filesystem.GetObject(path)
file_obj = self.filesystem.ResolveObject(path)
except IOError as exc:
raise OSError(errno.ENOENT, str(exc))
return file_obj.st_mtime
Expand All @@ -2718,7 +2718,7 @@ def getatime(self, path):
OSError: if the file does not exist.
"""
try:
file_obj = self.filesystem.GetObject(path)
file_obj = self.filesystem.ResolveObject(path)
except IOError as exc:
raise OSError(errno.ENOENT, str(exc))
return file_obj.st_atime
Expand All @@ -2736,7 +2736,7 @@ def getctime(self, path):
OSError: if the file does not exist.
"""
try:
file_obj = self.filesystem.GetObject(path)
file_obj = self.filesystem.ResolveObject(path)
except IOError as exc:
raise OSError(errno.ENOENT, str(exc))
return file_obj.st_ctime
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist=py26,py27,py33,py34,py35,py36
envlist=py26,py27,py33,py34,py35,py36,py37,pypy

[testenv]
deps = -rrequirements.txt
Expand Down

0 comments on commit 7e03f13

Please sign in to comment.