From c589050090b7e34e6b3da90d83144502a035c7a9 Mon Sep 17 00:00:00 2001 From: Thomas Feldmann Date: Fri, 28 Jan 2022 11:32:27 +0100 Subject: [PATCH 1/2] add basic `__eq__` checks --- fs/base.py | 12 ++++++++++++ fs/tarfs.py | 6 ++++++ fs/zipfs.py | 6 ++++++ tests/test_osfs.py | 13 +++++++++++++ tests/test_tarfs.py | 26 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+) diff --git a/fs/base.py b/fs/base.py index 83c36218..bfdeb1c8 100644 --- a/fs/base.py +++ b/fs/base.py @@ -130,6 +130,18 @@ def __exit__( """Close filesystem on exit.""" self.close() + def __eq__(self, other): + if isinstance(other, self.__class__): + try: + return self.getsyspath("/") == other.getsyspath("/") + except errors.NoSysPath: + pass + try: + return self.geturl("/") == other.geturl("/") + except errors.NoURL: + pass + return False + @property def glob(self): """`~fs.glob.BoundGlobber`: a globber object..""" diff --git a/fs/tarfs.py b/fs/tarfs.py index 0e808fe1..42483644 100644 --- a/fs/tarfs.py +++ b/fs/tarfs.py @@ -185,6 +185,9 @@ def __str__(self): # type: () -> Text return "".format(self._file) + def __eq__(self, other): + return isinstance(other, self.__class__) and self._file == other._file + def delegate_path(self, path): # type: (Text) -> Tuple[FS, Text] return self._temp_fs, path @@ -303,6 +306,9 @@ def __str__(self): # type: () -> Text return "".format(self._file) + def __eq__(self, other): + return isinstance(other, self.__class__) and self._file == other._file + if six.PY2: def _encode(self, s): diff --git a/fs/zipfs.py b/fs/zipfs.py index 12d8a668..e6b587a1 100644 --- a/fs/zipfs.py +++ b/fs/zipfs.py @@ -226,6 +226,9 @@ def __str__(self): # type: () -> Text return "".format(self._file) + def __eq__(self, other): + return isinstance(other, self.__class__) and self._file == other._file + def delegate_path(self, path): # type: (Text) -> Tuple[FS, Text] return self._temp_fs, path @@ -305,6 +308,9 @@ def __str__(self): # type: () -> Text return "".format(self._file) + def __eq__(self, other): + return isinstance(other, self.__class__) and self._file == other._file + def _path_to_zip_name(self, path): # type: (Text) -> str """Convert a path to a zip file name.""" diff --git a/tests/test_osfs.py b/tests/test_osfs.py index 7fdc5590..2ff0a712 100644 --- a/tests/test_osfs.py +++ b/tests/test_osfs.py @@ -229,3 +229,16 @@ def test_complex_geturl(self): def test_geturl_return_no_url(self): self.assertRaises(errors.NoURL, self.fs.geturl, "test/path", "upload") + + def test_equality(self): + dir_path = tempfile.mkdtemp() + t1 = open_fs(dir_path, create=True) + t2 = open_fs(dir_path, create=True) + self.assertEqual(t1, t2) + + another_dir_path = tempfile.mkdtemp() + t3 = open_fs(another_dir_path, create=True) + self.assertNotEqual(t1, t3) + + another_fs = open_fs("mem://") + self.assertNotEqual(t1, another_fs) diff --git a/tests/test_tarfs.py b/tests/test_tarfs.py index fc3f0779..93280782 100644 --- a/tests/test_tarfs.py +++ b/tests/test_tarfs.py @@ -306,6 +306,32 @@ def test_getinfo(self): self.assertIs(info.type, ResourceType.directory) +class TestEquality(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmpfs = open_fs("temp://") + + @classmethod + def tearDownClass(cls): + cls.tmpfs.close() + + def test_equality(self): + p1 = self.tmpfs.getospath("test.tar") + p2 = self.tmpfs.getospath("other.tar") + + with tarfs.TarFS("test.tar", write=True) as fw, tarfs.TarFS( + "test.tar" + ) as fr, tarfs.TarFS("test.tar") as fr2, tarfs.TarFS( + "other.tar" + ) as other, open_fs( + "mem://" + ) as mem: + self.assertEqual(fr, fr2) + self.assertNotEqual(fw, fr) + self.assertNotEqual(fr, mem) + self.assertNotEqual(fw, mem) + + class TestReadTarFSMem(TestReadTarFS): def make_source_fs(self): return open_fs("mem://") From ef003ff4c3526eb87d7845da386a2e011bc4dc6a Mon Sep 17 00:00:00 2001 From: Thomas Feldmann Date: Fri, 28 Jan 2022 11:34:21 +0100 Subject: [PATCH 2/2] add contributor --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 7e3fcbde..d1820428 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -38,6 +38,7 @@ Many thanks to the following developers for contributing to this project: - [Silvan Spross](https://github.com/sspross) - [@sqwishy](https://github.com/sqwishy) - [Sven Schliesing](https://github.com/muffl0n) +- [Thomas Feldmann](https://github.com/tfeldmann) - [Tim Gates](https://github.com/timgates42/) - [@tkossak](https://github.com/tkossak) - [Todd Levi](https://github.com/televi)