/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
1
# (C) 2005, 2006 Canonical Ltd
2
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.
7
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.
12
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
16
17
"""Tests for InterRepository implementastions."""
18
19
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
20
import bzrlib
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
21
import bzrlib.bzrdir as bzrdir
22
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
23
import bzrlib.errors as errors
24
from bzrlib.errors import (FileExists,
25
                           NoSuchRevision,
26
                           NoSuchFile,
27
                           UninitializableFormat,
28
                           NotBranchError,
29
                           )
30
import bzrlib.repository as repository
31
from bzrlib.revision import NULL_REVISION
32
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
33
from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
34
from bzrlib.transport import get_transport
35
36
37
class TestCaseWithInterRepository(TestCaseWithBzrDir):
38
39
    def setUp(self):
40
        super(TestCaseWithInterRepository, self).setUp()
41
42
    def make_branch(self, relpath):
43
        repo = self.make_repository(relpath)
44
        return repo.bzrdir.create_branch()
45
46
    def make_bzrdir(self, relpath, bzrdir_format=None):
47
        try:
48
            url = self.get_url(relpath)
49
            segments = url.split('/')
50
            if segments and segments[-1] not in ('', '.'):
51
                parent = '/'.join(segments[:-1])
52
                t = get_transport(parent)
53
                try:
54
                    t.mkdir(segments[-1])
55
                except FileExists:
56
                    pass
57
            if bzrdir_format is None:
58
                bzrdir_format = self.repository_format._matchingbzrdir
59
            return bzrdir_format.initialize(url)
60
        except UninitializableFormat:
61
            raise TestSkipped("Format %s is not initializable.")
62
63
    def make_repository(self, relpath):
64
        made_control = self.make_bzrdir(relpath)
65
        return self.repository_format.initialize(made_control)
66
67
    def make_to_repository(self, relpath):
68
        made_control = self.make_bzrdir(relpath,
69
            self.repository_format_to._matchingbzrdir)
1534.1.30 by Robert Collins
Test that we get the right optimiser back in the InterRepository tests.
70
        return self.repository_format_to.initialize(made_control)
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
71
72
73
class TestInterRepository(TestCaseWithInterRepository):
74
1534.1.30 by Robert Collins
Test that we get the right optimiser back in the InterRepository tests.
75
    def test_interrepository_get_returns_correct_optimiser(self):
76
        # we assume the optimising code paths are triggered
77
        # by the type of the repo not the transport - at this point.
78
        # we may need to update this test if this changes.
79
        source_repo = self.make_repository("source")
80
        target_repo = self.make_to_repository("target")
81
        interrepo = repository.InterRepository.get(source_repo, target_repo)
82
        self.assertEqual(self.interrepo_class, interrepo.__class__)
83
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
84
    def test_fetch(self):
85
        tree_a = self.make_branch_and_tree('a')
86
        self.build_tree(['a/foo'])
87
        tree_a.add('foo', 'file1')
88
        tree_a.commit('rev1', rev_id='rev1')
89
        def check_push_rev1(repo):
90
            # ensure the revision is missing.
91
            self.assertRaises(NoSuchRevision, repo.get_revision, 'rev1')
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
92
            # fetch with a limit of NULL_REVISION and an explicit progress bar.
93
            repo.fetch(tree_a.branch.repository,
94
                       revision_id=NULL_REVISION,
95
                       pb=bzrlib.progress.DummyProgress())
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
96
            # nothing should have been pushed
97
            self.assertFalse(repo.has_revision('rev1'))
98
            # fetch with a default limit (grab everything)
99
            repo.fetch(tree_a.branch.repository)
100
            # check that b now has all the data from a's first commit.
101
            rev = repo.get_revision('rev1')
102
            tree = repo.revision_tree('rev1')
103
            tree.get_file_text('file1')
104
            for file_id in tree:
105
                if tree.inventory[file_id].kind == "file":
106
                    tree.get_file(file_id).read()
107
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
108
        # makes a target version repo 
109
        repo_b = self.make_to_repository('b')
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
110
        check_push_rev1(repo_b)
111
        
112
    def test_fetch_missing_revision_same_location_fails(self):
113
        repo_a = self.make_repository('.')
114
        repo_b = repository.Repository.open('.')
115
        self.assertRaises(errors.NoSuchRevision, repo_b.fetch, repo_a, revision_id='XXX')
116
117
    def test_fetch_same_location_trivial_works(self):
118
        repo_a = self.make_repository('.')
119
        repo_b = repository.Repository.open('.')
120
        repo_a.fetch(repo_b)
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
121
122
123
class TestCaseWithComplexRepository(TestCaseWithInterRepository):
124
125
    def setUp(self):
126
        super(TestCaseWithComplexRepository, self).setUp()
127
        tree_a = self.make_branch_and_tree('a')
128
        self.bzrdir = tree_a.branch.bzrdir
129
        # add a corrupt inventory 'orphan'
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
130
        inv_file = tree_a.branch.repository.control_weaves.get_weave(
131
            'inventory', 
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
132
            tree_a.branch.repository.get_transaction())
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
133
        inv_file.add_lines('orphan', [], [])
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
134
        # add a real revision 'rev1'
135
        tree_a.commit('rev1', rev_id='rev1', allow_pointless=True)
136
        # add a real revision 'rev2' based on rev1
137
        tree_a.commit('rev2', rev_id='rev2', allow_pointless=True)
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
138
        # and sign 'rev2'
139
        tree_a.branch.repository.sign_revision('rev2',
140
            bzrlib.gpg.LoopbackGPGStrategy(None))
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
141
142
    def test_missing_revision_ids(self):
143
        # revision ids in repository A but not B are returned, fake ones
144
        # are stripped. (fake meaning no revision object, but an inventory 
145
        # as some formats keyed off inventory data in the past.
146
        # make a repository to compare against that claims to have rev1
147
        repo_b = self.make_to_repository('rev1_only')
148
        repo_a = self.bzrdir.open_repository()
149
        repo_b.fetch(repo_a, 'rev1')
150
        # check the test will be valid
151
        self.assertFalse(repo_b.has_revision('rev2'))
152
        self.assertEqual(['rev2'],
153
                         repo_b.missing_revision_ids(repo_a))
154
155
    def test_missing_revision_ids_revision_limited(self):
156
        # revision ids in repository A that are not referenced by the
157
        # requested revision are not returned.
158
        # make a repository to compare against that is empty
159
        repo_b = self.make_to_repository('empty')
160
        repo_a = self.bzrdir.open_repository()
161
        self.assertEqual(['rev1'],
162
                         repo_b.missing_revision_ids(repo_a, revision_id='rev1'))
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
163
        
164
    def test_fetch_preserves_signatures(self):
165
        from_repo = self.bzrdir.open_repository()
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
166
        from_signature = from_repo.get_signature_text('rev2')
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
167
        to_repo = self.make_to_repository('target')
168
        to_repo.fetch(from_repo)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
169
        to_signature = to_repo.get_signature_text('rev2')
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
170
        self.assertEqual(from_signature, to_signature)
171
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
172
173
class TestCaseWithGhosts(TestCaseWithInterRepository):
174
175
    def setUp(self):
176
        super(TestCaseWithGhosts, self).setUp()
177
        # we want two repositories at this point
178
        # one with a revision that is a ghost in the other
179
        # repository.
180
181
        # 'ghost' is a ghost in missing_ghost and not in with_ghost_rev
182
        inv = bzrlib.tree.EmptyTree().inventory
183
        repo = self.make_repository('with_ghost_rev')
184
        sha1 = repo.add_inventory('ghost', inv, [])
185
        rev = bzrlib.revision.Revision(timestamp=0,
186
                                       timezone=None,
187
                                       committer="Foo Bar <foo@example.com>",
188
                                       message="Message",
189
                                       inventory_sha1=sha1,
190
                                       revision_id='ghost')
191
        rev.parent_ids = []
192
        repo.add_revision('ghost', rev)
193
         
194
        repo = self.make_to_repository('missing_ghost')
195
        sha1 = repo.add_inventory('with_ghost', inv, [])
196
        rev = bzrlib.revision.Revision(timestamp=0,
197
                                       timezone=None,
198
                                       committer="Foo Bar <foo@example.com>",
199
                                       message="Message",
200
                                       inventory_sha1=sha1,
201
                                       revision_id='with_ghost')
202
        rev.parent_ids = ['ghost']
203
        repo.add_revision('with_ghost', rev)
204
205
    def test_fetch_all_fixes_up_ghost(self):
206
        # fetching from a repo with a current ghost unghosts it in referencing
207
        # revisions.
208
        repo = repository.Repository.open('missing_ghost')
209
        rev = repo.get_revision('with_ghost')
210
        from_repo = repository.Repository.open('with_ghost_rev')
211
        repo.fetch(from_repo)
212
        # rev must not be corrupt now
213
        rev = repo.get_revision('with_ghost')
214
        self.assertEqual([None, 'ghost', 'with_ghost'], repo.get_ancestry('with_ghost'))