1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for implementations of Repository.has_same_location."""
19
from bzrlib import bzrdir
20
from bzrlib.tests.repository_implementations import TestCaseWithRepository
21
from bzrlib.transport import get_transport
24
class TestHasSameLocation(TestCaseWithRepository):
25
"""Tests for Repository.has_same_location method."""
27
def assertSameRepo(self, a, b):
28
"""Asserts that two objects are the same repository.
30
This method does the comparison both ways (`a.has_same_location(b)` as
31
well as `b.has_same_location(a)`) to make sure both objects'
32
`has_same_location` methods give the same results.
34
self.assertTrue(a.has_same_location(b),
35
"%r is not the same repository as %r" % (a, b))
36
self.assertTrue(b.has_same_location(a),
37
"%r is the same as %r, but not vice versa" % (a, b))
39
def assertDifferentRepo(self, a, b):
40
"""Asserts that two objects are the not same repository.
42
This method does the comparison both ways (`a.has_same_location(b)` as
43
well as `b.has_same_location(a)`) to make sure both objects'
44
`has_same_location` methods give the same results.
46
:seealso: assertDifferentRepo
48
self.assertFalse(a.has_same_location(b),
49
"%r is not the same repository as %r" % (a, b))
50
self.assertFalse(b.has_same_location(a),
51
"%r is the same as %r, but not vice versa" % (a, b))
53
def test_same_repo_instance(self):
54
"""A repository object is the same repository as itself."""
55
repo = self.make_repository('.')
56
self.assertSameRepo(repo, repo)
58
def test_same_repo_location(self):
59
"""Different repository objects for the same location are the same."""
60
repo = self.make_repository('.')
61
reopened_repo = repo.bzrdir.open_repository()
63
repo is reopened_repo,
64
"This test depends on reopened_repo being a different instance of "
66
self.assertSameRepo(repo, reopened_repo)
68
def test_different_repos_not_equal(self):
69
"""Repositories at different locations are not the same."""
70
repo_one = self.make_repository('one')
71
repo_two = self.make_repository('two')
72
self.assertDifferentRepo(repo_one, repo_two)
74
def test_same_bzrdir_different_control_files_not_equal(self):
75
"""Repositories in the same bzrdir, but with different control files,
78
This can happens e.g. when upgrading a repository. This test mimics how
79
CopyConverter creates a second repository in one bzrdir.
81
repo = self.make_repository('repo')
83
control_transport = repo.control_files._transport
84
except AttributeError:
85
# This test only applies to repository formats with control_files.
87
if control_transport.base == repo.bzrdir.transport.base:
88
# This test only applies to repository formats where the repo
89
# control_files are separate from other bzrdir files, i.e. metadir
92
control_transport.copy_tree('.', '../repository.backup')
93
backup_transport = control_transport.clone('../repository.backup')
94
backup_repo = repo._format.open(repo.bzrdir, _found=True,
95
_override_transport=backup_transport)
97
self.assertDifferentRepo(repo, backup_repo)
99
def test_different_format_not_equal(self):
100
"""Different format repositories are comparable and not the same.
102
Comparing different format repository objects should give a negative
103
result, rather than trigger an exception (which could happen with a
104
naive __eq__ implementation, e.g. due to missing attributes).
106
repo = self.make_repository('repo')
107
other_repo = self.make_repository('other', format='default')
108
if repo._format == other_repo._format:
109
# We're testing the default format! So we have to use a non-default
110
# format for other_repo.
111
get_transport(self.get_vfs_only_url()).delete_tree('other')
112
other_repo = self.make_repository('other', format='metaweave')
113
# Make sure the other_repo is not a RemoteRepository.
114
other_bzrdir = bzrdir.BzrDir.open(self.get_vfs_only_url('other'))
115
other_repo = other_bzrdir.open_repository()
116
self.assertDifferentRepo(repo, other_repo)