Rev 2674: (Andrew Bennetts) Add support for comparing Repositories with == and != operators. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Mon Aug 6 06:52:31 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2674
revision-id: pqm at pqm.ubuntu.com-20070806055227-fzq7gamylj0eo610
parent: pqm at pqm.ubuntu.com-20070806040104-af06p8gf7bsb3eze
parent: andrew.bennetts at canonical.com-20070806051155-t570bk2i3gcnebwr
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2007-08-06 06:52:27 +0100
message:
  (Andrew Bennetts) Add support for comparing Repositories with == and != operators.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
  bzrlib/repository.py           rev_storage.py-20051111201905-119e9401e46257e3
  bzrlib/tests/repository_implementations/__init__.py __init__.py-20060131092037-9564957a7d4a841b
  bzrlib/tests/repository_implementations/test_repository.py test_repository.py-20060131092128-ad07f494f5c9d26c
    ------------------------------------------------------------
    revno: 2671.1.2
    merged: andrew.bennetts at canonical.com-20070806051155-t570bk2i3gcnebwr
    parent: andrew.bennetts at canonical.com-20070806015955-m36ovt3lahluxjzx
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: repository-equality
    timestamp: Mon 2007-08-06 15:11:55 +1000
    message:
      Compare URLs in RemoteRepository.__eq__, rather than '_client' attributes.
    ------------------------------------------------------------
    revno: 2671.1.1
    merged: andrew.bennetts at canonical.com-20070806015955-m36ovt3lahluxjzx
    parent: pqm at pqm.ubuntu.com-20070803043116-l7u1uypblmx1uxnr
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: repository-equality
    timestamp: Mon 2007-08-06 11:59:55 +1000
    message:
      Add support for comparing Repositories with == and != operators.
=== modified file 'NEWS'
--- a/NEWS	2007-08-02 07:22:05 +0000
+++ b/NEWS	2007-08-06 01:59:55 +0000
@@ -195,6 +195,9 @@
     * ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
       files that are stored on a transport. (Robert Collins)
 
+    * ``Repository`` objects can now be compared with ``==`` and ``!=`` to
+      determine if they are the same repository.  (Andrew Bennetts)
+
   TESTING:
 
     * Remove selftest ``--clean-output``, ``--numbered-dirs`` and

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2007-07-25 00:52:21 +0000
+++ b/bzrlib/remote.py	2007-08-06 05:11:55 +0000
@@ -249,6 +249,13 @@
         self._lock_count = 0
         self._leave_lock = False
 
+    def __eq__(self, other):
+        return (self.__class__ == other.__class__ and
+                self.bzrdir.transport.base == other.bzrdir.transport.base)
+        
+    def __ne__(self, other):
+        return not self == other
+
     def _ensure_real(self):
         """Ensure that there is a _real_repository set.
 

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2007-07-31 02:07:34 +0000
+++ b/bzrlib/repository.py	2007-08-06 01:59:55 +0000
@@ -235,6 +235,15 @@
         return '%s(%r)' % (self.__class__.__name__, 
                            self.bzrdir.transport.base)
 
+    def __eq__(self, other):
+        if self.__class__ is not other.__class__:
+            return False
+        return (self.control_files._transport.base ==
+                other.control_files._transport.base)
+
+    def __ne__(self, other):
+        return not self == other
+
     def is_locked(self):
         return self.control_files.is_locked()
 

=== modified file 'bzrlib/tests/repository_implementations/__init__.py'
--- a/bzrlib/tests/repository_implementations/__init__.py	2007-07-12 12:07:13 +0000
+++ b/bzrlib/tests/repository_implementations/__init__.py	2007-08-06 01:59:55 +0000
@@ -91,7 +91,7 @@
             return repo
         else:
             return super(TestCaseWithRepository, self).make_repository(
-                relpath, format)
+                relpath, format=format)
 
 
 

=== modified file 'bzrlib/tests/repository_implementations/test_repository.py'
--- a/bzrlib/tests/repository_implementations/test_repository.py	2007-07-25 00:52:21 +0000
+++ b/bzrlib/tests/repository_implementations/test_repository.py	2007-08-06 01:59:55 +0000
@@ -422,6 +422,109 @@
         self.assertEqual(repo._serializer.format_num, format)
 
 
+class TestRepositoryEquality(TestCaseWithRepository):
+
+    def strictAssertEqual(self, a, b):
+        """Like assertEqual, but also checks the `!=` operator is consistent.
+
+        i.e. if `a == b` *and* `a != b`, this method will fail.
+
+        This can happen when a class defines an `__eq__` but doesn't define an
+        `__ne__`.
+        """
+        self.assertEqual(a, b)
+        self.failIf(
+            a != b,
+            "%r and %r are both equal and not equal!  Class probably defines "
+            "__eq__ without also defining __ne__." % (a, b))
+
+    def strictAssertNotEqual(self, a, b):
+        """Like assertNotEqual, but also checks the `==` operator is consistent.
+
+        i.e. if `a != b` *and* `a == b`, this method will fail.
+
+        This can happen when a class defines an `__eq__` but doesn't define an
+        `__ne__`.
+
+        :seealso: strictAssertEqual
+        """
+        self.assertNotEqual(a, b)
+        self.failIf(
+            a == b,
+            "%r and %r are both equal and not equal!  Class probably defines "
+            "__eq__ without also defining __ne__." % (a, b))
+
+    def test_same_repo_instance_is_equal(self):
+        """A repository object is always equal to itself."""
+        repo = self.make_repository('.')
+        self.strictAssertEqual(repo, repo)
+
+    def test_same_repo_location_is_equal(self):
+        """Different repository objects connected to the same location are
+        equal.
+        """
+        repo = self.make_repository('.')
+        reopened_repo = repo.bzrdir.open_repository()
+        self.failIf(
+            repo is reopened_repo,
+            "This test depends on reopened_repo being a different instance of "
+            "the same repo.")
+        self.strictAssertEqual(repo, reopened_repo)
+
+    def test_different_repos_not_equal(self):
+        """Repositories at different locations are not equal."""
+        repo_one = self.make_repository('one')
+        repo_two = self.make_repository('two')
+        self.strictAssertNotEqual(repo_one, repo_two)
+
+    def test_same_bzrdir_different_control_files_not_equal(self):
+        """Repositories in the same bzrdir, but with different control files,
+        are not equal.
+
+        This can happens e.g. when upgrading a repository.  This test mimics how
+        CopyConverter creates a second repository in one bzrdir.
+        """
+        repo = self.make_repository('repo')
+        try:
+            control_transport = repo.control_files._transport
+        except AttributeError:
+            # This test only applies to repository formats with control_files.
+            return
+        if control_transport.base == repo.bzrdir.transport.base:
+            # This test only applies to repository formats where the repo
+            # control_files are separate from other bzrdir files, i.e. metadir
+            # formats.
+            return
+        control_transport.copy_tree('.', '../repository.backup')
+        backup_transport = control_transport.clone('../repository.backup')
+        backup_repo = repo._format.open(repo.bzrdir, _found=True,
+                                        _override_transport=backup_transport)
+
+        self.strictAssertNotEqual(repo, backup_repo)
+
+    def test_different_format_not_equal(self):
+        """Different format repositories are comparable and not equal.
+
+        Comparing different format repository objects should give a negative
+        result, rather than trigger an exception (which could happen with a
+        naive __eq__ implementation, e.g. due to missing attributes).
+        """
+        repo = self.make_repository('repo')
+        other_repo = self.make_repository('other', format='default')
+        if repo._format == other_repo._format:
+            # We're testing the default format!  So we have to use a non-default
+            # format for other_repo.
+            get_transport(self.get_vfs_only_url()).delete_tree('other')
+            other_repo = self.make_repository('other', format='metaweave')
+        # Make sure the other_repo is not a RemoteRepository.
+        other_bzrdir = bzrdir.BzrDir.open(self.get_vfs_only_url('other'))
+        other_repo = other_bzrdir.open_repository()
+        # Compare both ways, to make sure the __eq__ on both repositories cope
+        # with comparing against a different class.
+        self.strictAssertNotEqual(repo, other_repo)
+        self.strictAssertNotEqual(other_repo, repo)
+
+
 class TestRepositoryLocking(TestCaseWithRepository):
 
     def test_leave_lock_in_place(self):




More information about the bazaar-commits mailing list