Added support for fake os.path.samefile (#201)
- fixes #193
diff --git a/CHANGES.md b/CHANGES.md
index e42eadc..d0c0be6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -4,6 +4,7 @@
## Version 3.3 (as yet unreleased)
#### New Features
+ * Added fake `os.path.samefile` implementation ([#193](../../issues/193))
* Added support for `ns` argument in `os.utime()` (Python >= 3.3) ([#192](../../issues/192)).
* Added nanosecond time members in `os.stat_result` (Python >= 3.3) ([#196](../../issues/196)).
diff --git a/fake_filesystem_test.py b/fake_filesystem_test.py
index 2798e77..e82cd90 100755
--- a/fake_filesystem_test.py
+++ b/fake_filesystem_test.py
@@ -2657,6 +2657,17 @@
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)
diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py
index e7fa39d..3ad9394 100644
--- a/pyfakefs/fake_filesystem.py
+++ b/pyfakefs/fake_filesystem.py
@@ -2792,6 +2792,23 @@
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.