Skip to content

Commit

Permalink
Added support for fake os.path.samefile
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Jun 12, 2017
1 parent b157fd2 commit 27ff519
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The release versions are PyPi releases.
## Version 3.3 (as yet unreleased)

#### New Features
* Added fake `os.path.samefile` implementation ([#193](../../issues/193))

#### Infrastructure

Expand Down
11 changes: 11 additions & 0 deletions fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,17 @@ def testRealpathVsAbspath(self):
self.assertEqual('!george!washington!bridge',
self.os.path.realpath('bridge'))

@unittest.skipIf(TestCase.is_windows and sys.version_info < (3,2),
'No Windows support before 3.2')
def testSamefile(self):
file_path1 = '!foo!bar!baz'
file_path2 = '!foo!bar!boo'
self.filesystem.CreateFile(file_path1)
self.filesystem.CreateFile(file_path2)
self.assertTrue(self.path.samefile(file_path1, file_path1))
self.assertFalse(self.path.samefile(file_path1, file_path2))
self.assertTrue(self.path.samefile(file_path1, '!foo!..!foo!bar!..!bar!baz'))

def testExists(self):
file_path = 'foo!bar!baz'
self.filesystem.CreateFile(file_path)
Expand Down
17 changes: 17 additions & 0 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,23 @@ def realpath(self, filename):
path, ok = self._joinrealpath(filename[:0], filename, {})
return self.abspath(path)

if sys.platform != 'win32' or sys.version_info >= (3, 2):
def samefile(self, path1, path2):
"""Return whether path1 and path2 point to the same file.
Windows support new in Python 3.2.
New in pyfakefs 3.3.
Args:
path1: first file path or path object (Python >=3.6)
path2: second file path or path object (Python >=3.6)
Raises:
OSError: if one of the paths does not point to an existing file system object.
"""
stat1 = self.filesystem.GetStat(path1)
stat2 = self.filesystem.GetStat(path2)
return stat1.st_ino == stat2.st_ino and stat1.st_dev == stat2.st_dev

def _joinrealpath(self, path, rest, seen):
"""Join two paths, normalizing and eliminating any symbolic links
encountered in the second path.
Expand Down

0 comments on commit 27ff519

Please sign in to comment.