Skip to content

Commit 06714c7

Browse files
mrbean-bremenjmcgeheeiv
authored andcommitted
Added support for fake os.path.samefile (#201)
- fixes #193
1 parent 84be83d commit 06714c7

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ The release versions are PyPi releases.
44
## Version 3.3 (as yet unreleased)
55

66
#### New Features
7+
* Added fake `os.path.samefile` implementation ([#193](../../issues/193))
78
* Added support for `ns` argument in `os.utime()` (Python >= 3.3) ([#192](../../issues/192)).
89
* Added nanosecond time members in `os.stat_result` (Python >= 3.3) ([#196](../../issues/196)).
910

fake_filesystem_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,17 @@ def testRealpathVsAbspath(self):
26572657
self.assertEqual('!george!washington!bridge',
26582658
self.os.path.realpath('bridge'))
26592659

2660+
@unittest.skipIf(TestCase.is_windows and sys.version_info < (3,2),
2661+
'No Windows support before 3.2')
2662+
def testSamefile(self):
2663+
file_path1 = '!foo!bar!baz'
2664+
file_path2 = '!foo!bar!boo'
2665+
self.filesystem.CreateFile(file_path1)
2666+
self.filesystem.CreateFile(file_path2)
2667+
self.assertTrue(self.path.samefile(file_path1, file_path1))
2668+
self.assertFalse(self.path.samefile(file_path1, file_path2))
2669+
self.assertTrue(self.path.samefile(file_path1, '!foo!..!foo!bar!..!bar!baz'))
2670+
26602671
def testExists(self):
26612672
file_path = 'foo!bar!baz'
26622673
self.filesystem.CreateFile(file_path)

pyfakefs/fake_filesystem.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,23 @@ def realpath(self, filename):
27922792
path, ok = self._joinrealpath(filename[:0], filename, {})
27932793
return self.abspath(path)
27942794

2795+
if sys.platform != 'win32' or sys.version_info >= (3, 2):
2796+
def samefile(self, path1, path2):
2797+
"""Return whether path1 and path2 point to the same file.
2798+
Windows support new in Python 3.2.
2799+
New in pyfakefs 3.3.
2800+
2801+
Args:
2802+
path1: first file path or path object (Python >=3.6)
2803+
path2: second file path or path object (Python >=3.6)
2804+
2805+
Raises:
2806+
OSError: if one of the paths does not point to an existing file system object.
2807+
"""
2808+
stat1 = self.filesystem.GetStat(path1)
2809+
stat2 = self.filesystem.GetStat(path2)
2810+
return stat1.st_ino == stat2.st_ino and stat1.st_dev == stat2.st_dev
2811+
27952812
def _joinrealpath(self, path, rest, seen):
27962813
"""Join two paths, normalizing and eliminating any symbolic links
27972814
encountered in the second path.

0 commit comments

Comments
 (0)