/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/interrepository_implementations/test_interrepository.py

Merge bzr.dev r3466

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
import bzrlib.bzrdir as bzrdir
23
23
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
24
24
import bzrlib.errors as errors
25
 
from bzrlib.errors import (FileExists,
26
 
                           NoSuchRevision,
27
 
                           NoSuchFile,
28
 
                           UninitializableFormat,
29
 
                           NotBranchError,
30
 
                           )
 
25
import bzrlib.gpg
31
26
from bzrlib.inventory import Inventory
32
27
import bzrlib.repofmt.weaverepo as weaverepo
33
28
import bzrlib.repository as repository
39
34
    TestNotApplicable,
40
35
    TestSkipped,
41
36
    )
42
 
from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
43
 
from bzrlib.transport import get_transport
44
 
 
45
 
 
46
 
class TestCaseWithInterRepository(TestCaseWithBzrDir):
47
 
 
48
 
    def setUp(self):
49
 
        super(TestCaseWithInterRepository, self).setUp()
50
 
 
51
 
    def make_branch(self, relpath, format=None):
52
 
        repo = self.make_repository(relpath, format=format)
53
 
        return repo.bzrdir.create_branch()
54
 
 
55
 
    def make_bzrdir(self, relpath, format=None):
56
 
        try:
57
 
            url = self.get_url(relpath)
58
 
            segments = url.split('/')
59
 
            if segments and segments[-1] not in ('', '.'):
60
 
                parent = '/'.join(segments[:-1])
61
 
                t = get_transport(parent)
62
 
                try:
63
 
                    t.mkdir(segments[-1])
64
 
                except FileExists:
65
 
                    pass
66
 
            if format is None:
67
 
                format = self.repository_format._matchingbzrdir
68
 
            return format.initialize(url)
69
 
        except UninitializableFormat:
70
 
            raise TestSkipped("Format %s is not initializable." % format)
71
 
 
72
 
    def make_repository(self, relpath, format=None):
73
 
        made_control = self.make_bzrdir(relpath, format=format)
74
 
        return self.repository_format.initialize(made_control)
75
 
 
76
 
    def make_to_repository(self, relpath):
77
 
        made_control = self.make_bzrdir(relpath,
78
 
            self.repository_format_to._matchingbzrdir)
79
 
        return self.repository_format_to.initialize(made_control)
 
37
from bzrlib.tests.interrepository_implementations import (
 
38
    TestCaseWithInterRepository,
 
39
    )
80
40
 
81
41
 
82
42
def check_old_format_lock_error(repository_format):
130
90
        ## self.assertEqual(self.interrepo_class, interrepo.__class__)
131
91
        pass
132
92
 
133
 
    def test_fetch(self):
134
 
        tree_a = self.make_branch_and_tree('a')
135
 
        self.build_tree(['a/foo'])
136
 
        tree_a.add('foo', 'file1')
137
 
        tree_a.commit('rev1', rev_id='rev1')
138
 
        def check_push_rev1(repo):
139
 
            # ensure the revision is missing.
140
 
            self.assertRaises(NoSuchRevision, repo.get_revision, 'rev1')
141
 
            # fetch with a limit of NULL_REVISION and an explicit progress bar.
142
 
            repo.fetch(tree_a.branch.repository,
143
 
                       revision_id=NULL_REVISION,
144
 
                       pb=bzrlib.progress.DummyProgress())
145
 
            # nothing should have been pushed
146
 
            self.assertFalse(repo.has_revision('rev1'))
147
 
            # fetch with a default limit (grab everything)
148
 
            repo.fetch(tree_a.branch.repository)
149
 
            # check that b now has all the data from a's first commit.
150
 
            rev = repo.get_revision('rev1')
151
 
            tree = repo.revision_tree('rev1')
152
 
            tree.lock_read()
153
 
            self.addCleanup(tree.unlock)
154
 
            tree.get_file_text('file1')
155
 
            for file_id in tree:
156
 
                if tree.inventory[file_id].kind == "file":
157
 
                    tree.get_file(file_id).read()
158
 
 
159
 
        # makes a target version repo 
160
 
        repo_b = self.make_to_repository('b')
161
 
        check_push_rev1(repo_b)
162
 
 
163
 
    def test_fetch_missing_basis_text(self):
164
 
        """If fetching a delta, we should die if a basis is not present."""
165
 
        tree = self.make_branch_and_tree('tree')
166
 
        self.build_tree(['tree/a'])
167
 
        tree.add(['a'], ['a-id'])
168
 
        tree.commit('one', rev_id='rev-one')
169
 
        self.build_tree_contents([('tree/a', 'new contents\n')])
170
 
        tree.commit('two', rev_id='rev-two')
171
 
 
172
 
        to_repo = self.make_to_repository('to_repo')
173
 
        # We build a broken revision so that we can test the fetch code dies
174
 
        # properly. So copy the inventory and revision, but not the text.
175
 
        to_repo.lock_write()
176
 
        try:
177
 
            to_repo.start_write_group()
178
 
            inv = tree.branch.repository.get_inventory('rev-one')
179
 
            to_repo.add_inventory('rev-one', inv, [])
180
 
            rev = tree.branch.repository.get_revision('rev-one')
181
 
            to_repo.add_revision('rev-one', rev, inv=inv)
182
 
            to_repo.commit_write_group()
183
 
        finally:
184
 
            to_repo.unlock()
185
 
 
186
 
        # Implementations can either copy the missing basis text, or raise an
187
 
        # exception
188
 
        try:
189
 
            to_repo.fetch(tree.branch.repository, 'rev-two')
190
 
        except errors.RevisionNotPresent, e:
191
 
            # If an exception is raised, the revision should not be in the
192
 
            # target.
193
 
            self.assertRaises((errors.NoSuchRevision, errors.RevisionNotPresent),
194
 
                              to_repo.revision_tree, 'rev-two')
195
 
        else:
196
 
            # If not exception is raised, then the basis text should be
197
 
            # available.
198
 
            to_repo.lock_read()
199
 
            try:
200
 
                rt = to_repo.revision_tree('rev-one')
201
 
                self.assertEqual('contents of tree/a\n',
202
 
                                 rt.get_file_text('a-id'))
203
 
            finally:
204
 
                to_repo.unlock()
205
 
 
206
 
    def test_fetch_missing_revision_same_location_fails(self):
207
 
        repo_a = self.make_repository('.')
208
 
        repo_b = repository.Repository.open('.')
209
 
        try:
210
 
            self.assertRaises(errors.NoSuchRevision, repo_b.fetch, repo_a, revision_id='XXX')
211
 
        except errors.LockError, e:
212
 
            check_old_format_lock_error(self.repository_format)
213
 
 
214
 
    def test_fetch_same_location_trivial_works(self):
215
 
        repo_a = self.make_repository('.')
216
 
        repo_b = repository.Repository.open('.')
217
 
        try:
218
 
            repo_a.fetch(repo_b)
219
 
        except errors.LockError, e:
220
 
            check_old_format_lock_error(self.repository_format)
221
 
 
222
 
    def test_fetch_missing_text_other_location_fails(self):
223
 
        source_tree = self.make_branch_and_tree('source')
224
 
        source = source_tree.branch.repository
225
 
        target = self.make_to_repository('target')
226
 
    
227
 
        # start by adding a file so the data knit for the file exists in
228
 
        # repositories that have specific files for each fileid.
229
 
        self.build_tree(['source/id'])
230
 
        source_tree.add(['id'], ['id'])
231
 
        source_tree.commit('a', rev_id='a')
232
 
        # now we manually insert a revision with an inventory referencing
233
 
        # 'id' at revision 'b', but we do not insert revision b.
234
 
        # this should ensure that the new versions of files are being checked
235
 
        # for during pull operations
236
 
        inv = source.get_inventory('a')
237
 
        source.lock_write()
238
 
        source.start_write_group()
239
 
        inv['id'].revision = 'b'
240
 
        inv.revision_id = 'b'
241
 
        sha1 = source.add_inventory('b', inv, ['a'])
242
 
        rev = Revision(timestamp=0,
243
 
                       timezone=None,
244
 
                       committer="Foo Bar <foo@example.com>",
245
 
                       message="Message",
246
 
                       inventory_sha1=sha1,
247
 
                       revision_id='b')
248
 
        rev.parent_ids = ['a']
249
 
        source.add_revision('b', rev)
250
 
        source.commit_write_group()
251
 
        source.unlock()
252
 
        self.assertRaises(errors.RevisionNotPresent, target.fetch, source)
253
 
        self.assertFalse(target.has_revision('b'))
254
 
 
255
 
    def test_fetch_funky_file_id(self):
256
 
        from_tree = self.make_branch_and_tree('tree')
257
 
        if sys.platform == 'win32':
258
 
            from_repo = from_tree.branch.repository
259
 
            check_repo_format_for_funky_id_on_win32(from_repo)
260
 
        self.build_tree(['tree/filename'])
261
 
        from_tree.add('filename', 'funky-chars<>%&;"\'')
262
 
        from_tree.commit('commit filename')
263
 
        to_repo = self.make_to_repository('to')
264
 
        to_repo.fetch(from_tree.branch.repository, from_tree.get_parent_ids()[0])
265
 
 
266
93
 
267
94
class TestCaseWithComplexRepository(TestCaseWithInterRepository):
268
95
 
324
151
        self.assertFalse(repo_a.has_revision('pizza'))
325
152
        self.assertFalse(repo_b.has_revision('pizza'))
326
153
        # Asking specifically for an absent revision errors.
327
 
        self.assertRaises(NoSuchRevision, repo_b.search_missing_revision_ids, repo_a,
328
 
            revision_id='pizza', find_ghosts=True)
329
 
        self.assertRaises(NoSuchRevision, repo_b.search_missing_revision_ids, repo_a,
330
 
            revision_id='pizza', find_ghosts=False)
 
154
        self.assertRaises(errors.NoSuchRevision,
 
155
            repo_b.search_missing_revision_ids, repo_a, revision_id='pizza',
 
156
            find_ghosts=True)
 
157
        self.assertRaises(errors.NoSuchRevision,
 
158
            repo_b.search_missing_revision_ids, repo_a, revision_id='pizza',
 
159
            find_ghosts=False)
331
160
 
332
161
    def test_search_missing_revision_ids_revision_limited(self):
333
162
        # revision ids in repository A that are not referenced by the
403
232
        # rev must not be corrupt now
404
233
        self.assertEqual([None, 'ghost', 'references', 'tip'],
405
234
            missing_ghost.get_ancestry('tip'))
406
 
 
407
 
 
408
 
class TestFetchDependentData(TestCaseWithInterRepository):
409
 
 
410
 
    def test_reference(self):
411
 
        from_tree = self.make_branch_and_tree('tree')
412
 
        to_repo = self.make_to_repository('to')
413
 
        if (not from_tree.supports_tree_reference() or
414
 
            not from_tree.branch.repository._format.supports_tree_reference or
415
 
            not to_repo._format.supports_tree_reference):
416
 
            raise TestNotApplicable("Need subtree support.")
417
 
        subtree = self.make_branch_and_tree('tree/subtree')
418
 
        subtree.commit('subrev 1')
419
 
        from_tree.add_reference(subtree)
420
 
        tree_rev = from_tree.commit('foo')
421
 
        # now from_tree has a last-modified of subtree of the rev id of the
422
 
        # commit for foo, and a reference revision of the rev id of the commit
423
 
        # for subrev 1
424
 
        to_repo.fetch(from_tree.branch.repository, tree_rev)
425
 
        # to_repo should have a file_graph for from_tree.path2id('subtree') and
426
 
        # revid tree_rev.
427
 
        to_repo.lock_read()
428
 
        try:
429
 
            file_vf = to_repo.weave_store.get_weave(
430
 
                from_tree.path2id('subtree'), to_repo.get_transaction())
431
 
            self.assertEqual([tree_rev], file_vf.get_ancestry([tree_rev]))
432
 
        finally:
433
 
            to_repo.unlock()