Rev 2677: (Andrew Bennetts) Remove Repository.__eq__/__ne__ methods, replace with has_same_location method. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Aug 7 09:28:37 BST 2007


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

------------------------------------------------------------
revno: 2677
revision-id: pqm at pqm.ubuntu.com-20070807082835-sxq0vmfbvsebps5z
parent: pqm at pqm.ubuntu.com-20070807071734-qrnzeyjktp2f7022
parent: andrew.bennetts at canonical.com-20070807074727-5vmpk09r98lyef00
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2007-08-07 09:28:35 +0100
message:
  (Andrew Bennetts) Remove Repository.__eq__/__ne__ methods, replace with has_same_location method.
added:
  bzrlib/tests/repository_implementations/test_has_same_location.py test_has_same_locati-20070807074648-2i2ah82fbe83iys7-1
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.5
    merged: andrew.bennetts at canonical.com-20070807074727-5vmpk09r98lyef00
    parent: andrew.bennetts at canonical.com-20070807070458-pk3iwe1999bk1lz1
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: repository-equality
    timestamp: Tue 2007-08-07 17:47:27 +1000
    message:
      Move has_same_location tests to a new file.
    ------------------------------------------------------------
    revno: 2671.1.4
    merged: andrew.bennetts at canonical.com-20070807070458-pk3iwe1999bk1lz1
    parent: andrew.bennetts at canonical.com-20070807014056-al2nq16umo9f5gc5
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: repository-equality
    timestamp: Tue 2007-08-07 17:04:58 +1000
    message:
      Rename is_same_repository to has_same_location, thanks Aaron!
    ------------------------------------------------------------
    revno: 2671.1.3
    merged: andrew.bennetts at canonical.com-20070807014056-al2nq16umo9f5gc5
    parent: andrew.bennetts at canonical.com-20070806051155-t570bk2i3gcnebwr
    committer: Andrew Bennetts <andrew.bennetts at canonical.com>
    branch nick: repository-equality
    timestamp: Tue 2007-08-07 11:40:56 +1000
    message:
      Remove Repository.__eq__/__ne__ methods, replace with is_same_repository method.
=== added file 'bzrlib/tests/repository_implementations/test_has_same_location.py'
--- a/bzrlib/tests/repository_implementations/test_has_same_location.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/repository_implementations/test_has_same_location.py	2007-08-07 07:47:27 +0000
@@ -0,0 +1,118 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Tests for implementations of Repository.has_same_location."""
+
+from bzrlib import bzrdir
+from bzrlib.tests.repository_implementations import TestCaseWithRepository
+from bzrlib.transport import get_transport
+
+
+class TestHasSameLocation(TestCaseWithRepository):
+    """Tests for Repository.has_same_location method."""
+
+    def assertSameRepo(self, a, b):
+        """Asserts that two objects are the same repository.
+
+        This method does the comparison both ways (`a.has_same_location(b)` as
+        well as `b.has_same_location(a)`) to make sure both objects'
+        `has_same_location` methods give the same results.
+        """
+        self.assertTrue(a.has_same_location(b),
+                        "%r is not the same repository as %r" % (a, b))
+        self.assertTrue(b.has_same_location(a),
+                        "%r is the same as %r, but not vice versa" % (a, b))
+
+    def assertDifferentRepo(self, a, b):
+        """Asserts that two objects are the not same repository.
+
+        This method does the comparison both ways (`a.has_same_location(b)` as
+        well as `b.has_same_location(a)`) to make sure both objects'
+        `has_same_location` methods give the same results.
+
+        :seealso: assertDifferentRepo
+        """
+        self.assertFalse(a.has_same_location(b),
+                         "%r is not the same repository as %r" % (a, b))
+        self.assertFalse(b.has_same_location(a),
+                         "%r is the same as %r, but not vice versa" % (a, b))
+
+    def test_same_repo_instance(self):
+        """A repository object is the same repository as itself."""
+        repo = self.make_repository('.')
+        self.assertSameRepo(repo, repo)
+
+    def test_same_repo_location(self):
+        """Different repository objects for the same location are the same."""
+        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.assertSameRepo(repo, reopened_repo)
+
+    def test_different_repos_not_equal(self):
+        """Repositories at different locations are not the same."""
+        repo_one = self.make_repository('one')
+        repo_two = self.make_repository('two')
+        self.assertDifferentRepo(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 the same.
+
+        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.assertDifferentRepo(repo, backup_repo)
+
+    def test_different_format_not_equal(self):
+        """Different format repositories are comparable and not the same.
+
+        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()
+        self.assertDifferentRepo(repo, other_repo)
+
+

=== modified file 'NEWS'
--- a/NEWS	2007-08-07 06:38:33 +0000
+++ b/NEWS	2007-08-07 08:28:35 +0000
@@ -203,8 +203,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)
+    * New ``Repository.has_same_location`` method that reports if two
+      repository objects refer to the same repository (although with some risk
+      of false negatives).  (Andrew Bennetts)
 
     * InterTree.compare now passes require_versioned on correctly.
       (Marius Kruger)

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2007-08-06 05:11:55 +0000
+++ b/bzrlib/remote.py	2007-08-07 07:04:58 +0000
@@ -249,13 +249,10 @@
         self._lock_count = 0
         self._leave_lock = False
 
-    def __eq__(self, other):
+    def has_same_location(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-08-06 01:59:55 +0000
+++ b/bzrlib/repository.py	2007-08-07 07:04:58 +0000
@@ -235,15 +235,18 @@
         return '%s(%r)' % (self.__class__.__name__, 
                            self.bzrdir.transport.base)
 
-    def __eq__(self, other):
+    def has_same_location(self, other):
+        """Returns a boolean indicating if this repository is at the same
+        location as another repository.
+
+        This might return False even when two repository objects are accessing
+        the same physical repository via different URLs.
+        """
         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-08-06 01:59:55 +0000
+++ b/bzrlib/tests/repository_implementations/__init__.py	2007-08-07 07:47:27 +0000
@@ -101,6 +101,7 @@
         'bzrlib.tests.repository_implementations.test_break_lock',
         'bzrlib.tests.repository_implementations.test_commit_builder',
         'bzrlib.tests.repository_implementations.test_fileid_involved',
+        'bzrlib.tests.repository_implementations.test_has_same_location',
         'bzrlib.tests.repository_implementations.test_iter_reverse_revision_history',
         'bzrlib.tests.repository_implementations.test_pack',
         'bzrlib.tests.repository_implementations.test_reconcile',

=== modified file 'bzrlib/tests/repository_implementations/test_repository.py'
--- a/bzrlib/tests/repository_implementations/test_repository.py	2007-08-06 01:59:55 +0000
+++ b/bzrlib/tests/repository_implementations/test_repository.py	2007-08-07 07:47:27 +0000
@@ -422,109 +422,6 @@
         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