1
# Copyright (C) 2007, 2008 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 import (
23
from bzrlib.tests.repository_implementations import TestCaseWithRepository
24
from bzrlib.transport import get_transport
27
class TestHasSameLocation(TestCaseWithRepository):
28
"""Tests for Repository.has_same_location method."""
30
def assertSameRepo(self, a, b):
31
"""Asserts that two objects are the same repository.
33
This method does the comparison both ways (`a.has_same_location(b)` as
34
well as `b.has_same_location(a)`) to make sure both objects'
35
`has_same_location` methods give the same results.
37
self.assertTrue(a.has_same_location(b),
38
"%r is not the same repository as %r" % (a, b))
39
self.assertTrue(b.has_same_location(a),
40
"%r is the same as %r, but not vice versa" % (a, b))
42
def assertDifferentRepo(self, a, b):
43
"""Asserts that two objects are the not same repository.
45
This method does the comparison both ways (`a.has_same_location(b)` as
46
well as `b.has_same_location(a)`) to make sure both objects'
47
`has_same_location` methods give the same results.
49
:seealso: assertDifferentRepo
51
self.assertFalse(a.has_same_location(b),
52
"%r is not the same repository as %r" % (a, b))
53
self.assertFalse(b.has_same_location(a),
54
"%r is the same as %r, but not vice versa" % (a, b))
56
def test_same_repo_instance(self):
57
"""A repository object is the same repository as itself."""
58
repo = self.make_repository('.')
59
self.assertSameRepo(repo, repo)
61
def test_same_repo_location(self):
62
"""Different repository objects for the same location are the same."""
63
repo = self.make_repository('.')
64
reopened_repo = repo.bzrdir.open_repository()
66
repo is reopened_repo,
67
"This test depends on reopened_repo being a different instance of "
69
self.assertSameRepo(repo, reopened_repo)
71
def test_different_repos_not_equal(self):
72
"""Repositories at different locations are not the same."""
73
repo_one = self.make_repository('one')
74
repo_two = self.make_repository('two')
75
self.assertDifferentRepo(repo_one, repo_two)
77
def test_same_bzrdir_different_control_files_not_equal(self):
78
"""Repositories in the same bzrdir, but with different control files,
81
This can happens e.g. when upgrading a repository. This test mimics how
82
CopyConverter creates a second repository in one bzrdir.
84
repo = self.make_repository('repo')
86
control_transport = repo._transport
87
except AttributeError:
88
raise TestNotApplicable(
89
"%r has no transport" % (repo,))
90
if control_transport.base == repo.bzrdir.transport.base:
91
raise TestNotApplicable(
92
"%r has repository files directly in the bzrdir"
94
# This test only applies to repository formats where the repo
95
# control_files are separate from other bzrdir files, i.e. metadir
97
control_transport.copy_tree('.', '../repository.backup')
98
backup_transport = control_transport.clone('../repository.backup')
99
backup_repo = repo._format.open(repo.bzrdir, _found=True,
100
_override_transport=backup_transport)
101
self.assertDifferentRepo(repo, backup_repo)
103
def test_different_format_not_equal(self):
104
"""Different format repositories are comparable and not the same.
106
Comparing different format repository objects should give a negative
107
result, rather than trigger an exception (which could happen with a
108
naive __eq__ implementation, e.g. due to missing attributes).
110
repo = self.make_repository('repo')
111
other_repo = self.make_repository('other', format='default')
112
if repo._format == other_repo._format:
113
# We're testing the default format! So we have to use a non-default
114
# format for other_repo.
115
get_transport(self.get_vfs_only_url()).delete_tree('other')
116
other_repo = self.make_repository('other', format='metaweave')
117
# Make sure the other_repo is not a RemoteRepository.
118
other_bzrdir = bzrdir.BzrDir.open(self.get_vfs_only_url('other'))
119
other_repo = other_bzrdir.open_repository()
120
self.assertDifferentRepo(repo, other_repo)