/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 breezy/tests/test_foreign.py

  • Committer: Martin
  • Date: 2017-06-09 16:31:49 UTC
  • mto: This revision was merged to the branch mainline in revision 6673.
  • Revision ID: gzlist@googlemail.com-20170609163149-liveiasey25480q6
Make InventoryDeltaError use string formatting, and repr for fileids

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008-2012, 2016 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
18
18
"""Tests for foreign VCS utility code."""
19
19
 
20
20
 
21
 
from bzrlib import (
 
21
from .. import (
22
22
    branch,
 
23
    bzrbranch,
23
24
    bzrdir,
 
25
    controldir,
24
26
    errors,
25
27
    foreign,
26
28
    lockable_files,
27
29
    lockdir,
 
30
    repository,
28
31
    revision,
29
32
    tests,
30
33
    trace,
 
34
    vf_repository,
31
35
    )
32
36
 
 
37
from ..repofmt import groupcompress_repo
 
38
 
33
39
# This is the dummy foreign revision control system, used 
34
40
# mainly here in the testsuite to test the foreign VCS infrastructure.
35
41
# It is basically standard Bazaar with some minor modifications to 
45
51
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
46
52
 
47
53
    def __eq__(self, other):
48
 
        return type(self) == type(other)
 
54
        return isinstance(self, type(other))
49
55
 
50
56
    def revision_id_bzr_to_foreign(self, bzr_revid):
51
57
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
83
89
        return "%s|%s|%s" % foreign_revid
84
90
 
85
91
 
86
 
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
 
92
class DummyForeignVcsBranch(bzrbranch.BzrBranch6,foreign.ForeignBranch):
87
93
    """A Dummy VCS Branch."""
88
94
 
 
95
    @property
 
96
    def user_transport(self):
 
97
        return self.bzrdir.user_transport
 
98
 
89
99
    def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
90
100
        self._format = _format
91
101
        self._base = a_bzrdir.transport.base
92
102
        self._ignore_fallbacks = False
93
103
        self.bzrdir = a_bzrdir
94
 
        foreign.ForeignBranch.__init__(self, 
 
104
        foreign.ForeignBranch.__init__(self,
95
105
            DummyForeignVcsMapping(DummyForeignVcs()))
96
 
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir, 
 
106
        bzrbranch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
97
107
            *args, **kwargs)
98
108
 
99
 
 
100
 
class InterToDummyVcsBranch(branch.GenericInterBranch,
101
 
                            foreign.InterToForeignBranch):
 
109
    def _get_checkout_format(self, lightweight=False):
 
110
        """Return the most suitable metadir for a checkout of this branch.
 
111
        Weaves are used if this branch's repository uses weaves.
 
112
        """
 
113
        return self.bzrdir.checkout_metadir()
 
114
 
 
115
    def import_last_revision_info_and_tags(self, source, revno, revid,
 
116
                                           lossy=False):
 
117
        interbranch = InterToDummyVcsBranch(source, self)
 
118
        result = interbranch.push(stop_revision=revid, lossy=True)
 
119
        if lossy:
 
120
            revid = result.revidmap[revid]
 
121
        return (revno, revid)
 
122
 
 
123
 
 
124
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
 
125
 
 
126
    def _generate_revision_if_needed(self):
 
127
        mapping = DummyForeignVcsMapping(DummyForeignVcs())
 
128
        if self._lossy:
 
129
            self._new_revision_id = mapping.revision_id_foreign_to_bzr(
 
130
                (str(self._timestamp), str(self._timezone), "UNKNOWN"))
 
131
            self.random_revid = False
 
132
        elif self._new_revision_id is not None:
 
133
            self.random_revid = False
 
134
        else:
 
135
            self._new_revision_id = self._gen_revision_id()
 
136
            self.random_revid = True
 
137
 
 
138
 
 
139
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
 
140
    foreign.ForeignRepository):
 
141
    """Dummy foreign vcs repository."""
 
142
 
 
143
 
 
144
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
 
145
 
 
146
    repository_class = DummyForeignVcsRepository
 
147
    _commit_builder_class = DummyForeignCommitBuilder
 
148
 
 
149
    @classmethod
 
150
    def get_format_string(cls):
 
151
        return "Dummy Foreign Vcs Repository"
 
152
 
 
153
    def get_format_description(self):
 
154
        return "Dummy Foreign Vcs Repository"
 
155
 
 
156
 
 
157
def branch_history(graph, revid):
 
158
    ret = list(graph.iter_lefthand_ancestry(revid,
 
159
        (revision.NULL_REVISION,)))
 
160
    ret.reverse()
 
161
    return ret
 
162
 
 
163
 
 
164
class InterToDummyVcsBranch(branch.GenericInterBranch):
102
165
 
103
166
    @staticmethod
104
167
    def is_compatible(source, target):
105
168
        return isinstance(target, DummyForeignVcsBranch)
106
169
 
107
 
    def push(self, overwrite=False, stop_revision=None):
108
 
        raise errors.NoRoundtrippingSupport(self.source, self.target)
109
 
 
110
 
    def lossy_push(self, stop_revision=None):
 
170
    def push(self, overwrite=False, stop_revision=None, lossy=False):
 
171
        if not lossy:
 
172
            raise errors.NoRoundtrippingSupport(self.source, self.target)
111
173
        result = branch.BranchPushResult()
112
174
        result.source_branch = self.source
113
175
        result.target_branch = self.target
114
176
        result.old_revno, result.old_revid = self.target.last_revision_info()
115
177
        self.source.lock_read()
116
178
        try:
 
179
            graph = self.source.repository.get_graph()
117
180
            # This just handles simple cases, but that's good enough for tests
118
 
            my_history = self.target.revision_history()
119
 
            their_history = self.source.revision_history()
 
181
            my_history = branch_history(self.target.repository.get_graph(),
 
182
                result.old_revid)
 
183
            if stop_revision is None:
 
184
                stop_revision = self.source.last_revision()
 
185
            their_history = branch_history(graph, stop_revision)
120
186
            if their_history[:min(len(my_history), len(their_history))] != my_history:
121
187
                raise errors.DivergedBranches(self.target, self.source)
122
188
            todo = their_history[len(my_history):]
128
194
                    return (tree.get_file(file_id), None)
129
195
                tree.get_file_with_stat = get_file_with_stat
130
196
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
131
 
                    (str(rev.timestamp), str(rev.timezone), 
 
197
                    (str(rev.timestamp), str(rev.timezone),
132
198
                        str(self.target.revno())))
133
199
                parent_revno, parent_revid= self.target.last_revision_info()
134
200
                if parent_revid == revision.NULL_REVISION:
136
202
                else:
137
203
                    parent_revids = [parent_revid]
138
204
                builder = self.target.get_commit_builder(parent_revids, 
139
 
                        self.target.get_config(), rev.timestamp,
 
205
                        self.target.get_config_stack(), rev.timestamp,
140
206
                        rev.timezone, rev.committer, rev.properties,
141
207
                        new_revid)
142
208
                try:
143
 
                    for path, ie in tree.inventory.iter_entries():
 
209
                    parent_tree = self.target.repository.revision_tree(
 
210
                        parent_revid)
 
211
                    for path, ie in tree.iter_entries_by_dir():
144
212
                        new_ie = ie.copy()
145
213
                        new_ie.revision = None
146
214
                        builder.record_entry_contents(new_ie, 
147
 
                            [self.target.repository.revision_tree(parent_revid).inventory],
 
215
                            [parent_tree.root_inventory],
148
216
                            path, tree, 
149
217
                            (ie.kind, ie.text_size, ie.executable, ie.text_sha1))
150
218
                    builder.finish_inventory()
163
231
        return result
164
232
 
165
233
 
166
 
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
 
234
class DummyForeignVcsBranchFormat(bzrbranch.BzrBranchFormat6):
167
235
 
168
 
    def get_format_string(self):
 
236
    @classmethod
 
237
    def get_format_string(cls):
169
238
        return "Branch for Testing"
170
239
 
171
 
    def __init__(self):
172
 
        super(DummyForeignVcsBranchFormat, self).__init__()
173
 
        self._matchingbzrdir = DummyForeignVcsDirFormat()
 
240
    @property
 
241
    def _matchingbzrdir(self):
 
242
        return DummyForeignVcsDirFormat()
174
243
 
175
 
    def open(self, a_bzrdir, name=None, _found=False):
 
244
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
245
            found_repository=None):
 
246
        if name is None:
 
247
            name = a_bzrdir._get_selected_branch()
176
248
        if not _found:
177
249
            raise NotImplementedError
178
250
        try:
179
251
            transport = a_bzrdir.get_branch_transport(None, name=name)
180
252
            control_files = lockable_files.LockableFiles(transport, 'lock',
181
253
                                                         lockdir.LockDir)
 
254
            if found_repository is None:
 
255
                found_repository = a_bzrdir.find_repository()
182
256
            return DummyForeignVcsBranch(_format=self,
183
257
                              _control_files=control_files,
184
258
                              a_bzrdir=a_bzrdir,
185
 
                              _repository=a_bzrdir.find_repository())
 
259
                              _repository=found_repository,
 
260
                              name=name)
186
261
        except errors.NoSuchFile:
187
262
            raise errors.NotBranchError(path=transport.base)
188
263
 
205
280
    def get_branch_format(self):
206
281
        return DummyForeignVcsBranchFormat()
207
282
 
208
 
    @classmethod
209
 
    def probe_transport(klass, transport):
210
 
        """Return the .bzrdir style format present in a directory."""
211
 
        if not transport.has('.dummy'):
212
 
            raise errors.NotBranchError(path=transport.base)
213
 
        return klass()
 
283
    @property
 
284
    def repository_format(self):
 
285
        return DummyForeignVcsRepositoryFormat()
214
286
 
215
287
    def initialize_on_transport(self, transport):
216
288
        """Initialize a new bzrdir in the base directory of a Transport."""
244
316
        self._control_files = lockable_files.LockableFiles(self.transport,
245
317
            "lock", lockable_files.TransportLock)
246
318
 
247
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
248
 
        if name is not None:
 
319
    def create_workingtree(self):
 
320
        # dirstate requires a ".bzr" entry to exist
 
321
        self.root_transport.put_bytes(".bzr", "foo")
 
322
        return super(DummyForeignVcsDir, self).create_workingtree()
 
323
 
 
324
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
 
325
                    possible_transports=None):
 
326
        if name is None:
 
327
            name = self._get_selected_branch()
 
328
        if name != "":
249
329
            raise errors.NoColocatedBranchSupport(self)
250
330
        return self._format.get_branch_format().open(self, _found=True)
251
331
 
252
332
    def cloning_metadir(self, stacked=False):
253
333
        """Produce a metadir suitable for cloning with."""
254
 
        return bzrdir.format_registry.make_bzrdir("default")
 
334
        return controldir.format_registry.make_bzrdir("default")
 
335
 
 
336
    def checkout_metadir(self):
 
337
        return self.cloning_metadir()
255
338
 
256
339
    def sprout(self, url, revision_id=None, force_new_repo=False,
257
340
               recurse='down', possible_transports=None,
258
341
               accelerator_tree=None, hardlink=False, stacked=False,
259
342
               source_branch=None):
260
 
        # dirstate doesn't cope with accelerator_trees well 
 
343
        # dirstate doesn't cope with accelerator_trees well
261
344
        # that have a different control dir
262
 
        return super(DummyForeignVcsDir, self).sprout(url=url, 
263
 
                revision_id=revision_id, force_new_repo=force_new_repo, 
264
 
                recurse=recurse, possible_transports=possible_transports, 
265
 
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
 
345
        return super(DummyForeignVcsDir, self).sprout(
 
346
            url=url,
 
347
            revision_id=revision_id, force_new_repo=force_new_repo,
 
348
            recurse=recurse, possible_transports=possible_transports,
 
349
            hardlink=hardlink, stacked=stacked, source_branch=source_branch)
266
350
 
267
351
 
268
352
def register_dummy_foreign_for_test(testcase):
269
 
    bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
 
    testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
 
                        DummyForeignVcsDirFormat)
 
353
    controldir.ControlDirFormat.register_prober(DummyForeignProber)
 
354
    testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
 
355
                        DummyForeignProber)
 
356
    repository.format_registry.register(DummyForeignVcsRepositoryFormat())
 
357
    testcase.addCleanup(repository.format_registry.remove,
 
358
                        DummyForeignVcsRepositoryFormat())
 
359
    branch.format_registry.register(DummyForeignVcsBranchFormat())
 
360
    testcase.addCleanup(branch.format_registry.remove,
 
361
                        DummyForeignVcsBranchFormat())
272
362
    # We need to register the optimiser to make the dummy appears really
273
363
    # different from a regular bzr repository.
274
364
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
276
366
                        InterToDummyVcsBranch)
277
367
 
278
368
 
 
369
class DummyForeignProber(controldir.Prober):
 
370
 
 
371
    @classmethod
 
372
    def probe_transport(klass, transport):
 
373
        """Return the .bzrdir style format present in a directory."""
 
374
        if not transport.has('.dummy'):
 
375
            raise errors.NotBranchError(path=transport.base)
 
376
        return DummyForeignVcsDirFormat()
 
377
 
 
378
    @classmethod
 
379
    def known_formats(cls):
 
380
        return {DummyForeignVcsDirFormat()}
 
381
 
 
382
 
279
383
class ForeignVcsRegistryTests(tests.TestCase):
280
384
    """Tests for the ForeignVcsRegistry class."""
281
385
 
293
397
        reg = foreign.ForeignVcsRegistry()
294
398
        vcs = DummyForeignVcs()
295
399
        reg.register("dummy", vcs, "Dummy VCS")
296
 
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
297
 
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
 
400
        self.assertEqual((
 
401
            ("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
 
402
            reg.parse_revision_id("dummy-v1:some-foreign-revid"))
298
403
 
299
404
 
300
405
class ForeignRevisionTests(tests.TestCase):
304
409
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
305
410
        rev = foreign.ForeignRevision(("a", "foreign", "revid"),
306
411
                                      mapp, "roundtripped-revid")
307
 
        self.assertEquals("", rev.inventory_sha1)
308
 
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
309
 
        self.assertEquals(mapp, rev.mapping)
 
412
        self.assertEqual("", rev.inventory_sha1)
 
413
        self.assertEqual(("a", "foreign", "revid"), rev.foreign_revid)
 
414
        self.assertEqual(mapp, rev.mapping)
310
415
 
311
416
 
312
417
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
328
433
        foreign.update_workingtree_fileids(wt, target_basis)
329
434
        wt.lock_read()
330
435
        try:
331
 
            self.assertEquals(set([root_id, "bla-b"]), set(wt.inventory))
 
436
            self.assertEqual({root_id, "bla-b"}, set(wt.all_file_ids()))
332
437
        finally:
333
438
            wt.unlock()
334
439
 
343
448
    def test_create(self):
344
449
        """Test we can create dummies."""
345
450
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
346
 
        dir = bzrdir.BzrDir.open("d")
347
 
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
 
451
        dir = controldir.ControlDir.open("d")
 
452
        self.assertEqual("A Dummy VCS Dir", dir._format.get_format_string())
348
453
        dir.open_repository()
349
454
        dir.open_branch()
350
455
        dir.open_workingtree()
352
457
    def test_sprout(self):
353
458
        """Test we can clone dummies and that the format is not preserved."""
354
459
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
355
 
        dir = bzrdir.BzrDir.open("d")
 
460
        dir = controldir.ControlDir.open("d")
356
461
        newdir = dir.sprout("e")
357
 
        self.assertNotEquals("A Dummy VCS Dir",
358
 
                             newdir._format.get_format_string())
 
462
        self.assertNotEqual("A Dummy VCS Dir",
 
463
                            newdir._format.get_format_string())
359
464
 
360
465
    def test_push_not_supported(self):
361
466
        source_tree = self.make_branch_and_tree("source")
362
 
        target_tree = self.make_branch_and_tree("target", 
363
 
            format=DummyForeignVcsDirFormat())
364
 
        self.assertRaises(errors.NoRoundtrippingSupport, 
365
 
            source_tree.branch.push, target_tree.branch)
 
467
        target_tree = self.make_branch_and_tree(
 
468
            "target", format=DummyForeignVcsDirFormat())
 
469
        self.assertRaises(errors.NoRoundtrippingSupport,
 
470
                          source_tree.branch.push, target_tree.branch)
366
471
 
367
472
    def test_lossy_push_empty(self):
368
473
        source_tree = self.make_branch_and_tree("source")
369
 
        target_tree = self.make_branch_and_tree("target", 
370
 
            format=DummyForeignVcsDirFormat())
371
 
        pushresult = source_tree.branch.lossy_push(target_tree.branch)
372
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
373
 
        self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
374
 
        self.assertEquals({}, pushresult.revidmap)
 
474
        target_tree = self.make_branch_and_tree(
 
475
            "target", format=DummyForeignVcsDirFormat())
 
476
        pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
 
477
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
 
478
        self.assertEqual(revision.NULL_REVISION, pushresult.new_revid)
 
479
        self.assertEqual({}, pushresult.revidmap)
375
480
 
376
481
    def test_lossy_push_simple(self):
377
482
        source_tree = self.make_branch_and_tree("source")
378
483
        self.build_tree(['source/a', 'source/b'])
379
484
        source_tree.add(['a', 'b'])
380
485
        revid1 = source_tree.commit("msg")
381
 
        target_tree = self.make_branch_and_tree("target", 
382
 
            format=DummyForeignVcsDirFormat())
 
486
        target_tree = self.make_branch_and_tree(
 
487
            "target", format=DummyForeignVcsDirFormat())
383
488
        target_tree.branch.lock_write()
384
489
        try:
385
 
            pushresult = source_tree.branch.lossy_push(target_tree.branch)
 
490
            pushresult = source_tree.branch.push(
 
491
                target_tree.branch, lossy=True)
386
492
        finally:
387
493
            target_tree.branch.unlock()
388
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
389
 
        self.assertEquals({revid1:target_tree.branch.last_revision()}, 
390
 
                           pushresult.revidmap)
391
 
        self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)
 
494
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
 
495
        self.assertEqual({revid1: target_tree.branch.last_revision()},
 
496
                         pushresult.revidmap)
 
497
        self.assertEqual(pushresult.revidmap[revid1], pushresult.new_revid)