Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable comparing filesystems with == #519

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.."""
Expand Down
6 changes: 6 additions & 0 deletions fs/tarfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ def __str__(self):
# type: () -> Text
return "<TarFS-write '{}'>".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
Expand Down Expand Up @@ -303,6 +306,9 @@ def __str__(self):
# type: () -> Text
return "<TarFS '{}'>".format(self._file)

def __eq__(self, other):
return isinstance(other, self.__class__) and self._file == other._file

if six.PY2:

def _encode(self, s):
Expand Down
6 changes: 6 additions & 0 deletions fs/zipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ def __str__(self):
# type: () -> Text
return "<zipfs-write '{}'>".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
Expand Down Expand Up @@ -305,6 +308,9 @@ def __str__(self):
# type: () -> Text
return "<zipfs '{}'>".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."""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_osfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 26 additions & 0 deletions tests/test_tarfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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://")
Expand Down