From 27ff5196afef3ad43350292a3b1b70c3ed56ae4e Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Fri, 9 Jun 2017 16:32:40 +0200 Subject: [PATCH] Added support for fake os.path.samefile - fixes #193 --- CHANGES.md | 1 + fake_filesystem_test.py | 11 +++++++++++ pyfakefs/fake_filesystem.py | 17 +++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 8ae992c7..4bee865e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/fake_filesystem_test.py b/fake_filesystem_test.py index 84fcd466..c9362b6d 100755 --- a/fake_filesystem_test.py +++ b/fake_filesystem_test.py @@ -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) diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index f8ce8fee..a5f89f31 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -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.