~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_foreign.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2011 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
91
91
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
92
92
    """A Dummy VCS Branch."""
93
93
 
 
94
    @property
 
95
    def user_transport(self):
 
96
        return self.bzrdir.user_transport
 
97
 
94
98
    def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
95
99
        self._format = _format
96
100
        self._base = a_bzrdir.transport.base
141
145
    repository_class = DummyForeignVcsRepository
142
146
    _commit_builder_class = DummyForeignCommitBuilder
143
147
 
144
 
    def get_format_string(self):
 
148
    @classmethod
 
149
    def get_format_string(cls):
145
150
        return "Dummy Foreign Vcs Repository"
146
151
 
147
152
    def get_format_description(self):
148
153
        return "Dummy Foreign Vcs Repository"
149
154
 
150
155
 
 
156
def branch_history(graph, revid):
 
157
    ret = list(graph.iter_lefthand_ancestry(revid,
 
158
        (revision.NULL_REVISION,)))
 
159
    ret.reverse()
 
160
    return ret
 
161
 
 
162
 
151
163
class InterToDummyVcsBranch(branch.GenericInterBranch):
152
164
 
153
165
    @staticmethod
165
177
        try:
166
178
            graph = self.source.repository.get_graph()
167
179
            # This just handles simple cases, but that's good enough for tests
168
 
            my_history = self.target.revision_history()
 
180
            my_history = branch_history(self.target.repository.get_graph(),
 
181
                result.old_revid)
169
182
            if stop_revision is None:
170
183
                stop_revision = self.source.last_revision()
171
 
            their_history = list(graph.iter_lefthand_ancestry(stop_revision,
172
 
                (revision.NULL_REVISION,)))
173
 
            their_history.reverse()
 
184
            their_history = branch_history(graph, stop_revision)
174
185
            if their_history[:min(len(my_history), len(their_history))] != my_history:
175
186
                raise errors.DivergedBranches(self.target, self.source)
176
187
            todo = their_history[len(my_history):]
190
201
                else:
191
202
                    parent_revids = [parent_revid]
192
203
                builder = self.target.get_commit_builder(parent_revids, 
193
 
                        self.target.get_config(), rev.timestamp,
 
204
                        self.target.get_config_stack(), rev.timestamp,
194
205
                        rev.timezone, rev.committer, rev.properties,
195
206
                        new_revid)
196
207
                try:
197
 
                    for path, ie in tree.inventory.iter_entries():
 
208
                    parent_tree = self.target.repository.revision_tree(
 
209
                        parent_revid)
 
210
                    for path, ie in tree.iter_entries_by_dir():
198
211
                        new_ie = ie.copy()
199
212
                        new_ie.revision = None
200
213
                        builder.record_entry_contents(new_ie, 
201
 
                            [self.target.repository.revision_tree(parent_revid).inventory],
 
214
                            [parent_tree.root_inventory],
202
215
                            path, tree, 
203
216
                            (ie.kind, ie.text_size, ie.executable, ie.text_sha1))
204
217
                    builder.finish_inventory()
219
232
 
220
233
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
221
234
 
222
 
    def get_format_string(self):
 
235
    @classmethod
 
236
    def get_format_string(cls):
223
237
        return "Branch for Testing"
224
238
 
225
 
    def __init__(self):
226
 
        super(DummyForeignVcsBranchFormat, self).__init__()
227
 
        self._matchingbzrdir = DummyForeignVcsDirFormat()
 
239
    @property
 
240
    def _matchingbzrdir(self):
 
241
        return DummyForeignVcsDirFormat()
228
242
 
229
243
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
230
244
            found_repository=None):
 
245
        if name is None:
 
246
            name = a_bzrdir._get_selected_branch()
231
247
        if not _found:
232
248
            raise NotImplementedError
233
249
        try:
239
255
            return DummyForeignVcsBranch(_format=self,
240
256
                              _control_files=control_files,
241
257
                              a_bzrdir=a_bzrdir,
242
 
                              _repository=found_repository)
 
258
                              _repository=found_repository,
 
259
                              name=name)
243
260
        except errors.NoSuchFile:
244
261
            raise errors.NotBranchError(path=transport.base)
245
262
 
303
320
        self.root_transport.put_bytes(".bzr", "foo")
304
321
        return super(DummyForeignVcsDir, self).create_workingtree()
305
322
 
306
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
307
 
        if name is not None:
 
323
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
 
324
                    possible_transports=None):
 
325
        if name is None:
 
326
            name = self._get_selected_branch()
 
327
        if name != "":
308
328
            raise errors.NoColocatedBranchSupport(self)
309
329
        return self._format.get_branch_format().open(self, _found=True)
310
330
 
311
331
    def cloning_metadir(self, stacked=False):
312
332
        """Produce a metadir suitable for cloning with."""
313
 
        return bzrdir.format_registry.make_bzrdir("default")
 
333
        return controldir.format_registry.make_bzrdir("default")
314
334
 
315
335
    def checkout_metadir(self):
316
336
        return self.cloning_metadir()
319
339
               recurse='down', possible_transports=None,
320
340
               accelerator_tree=None, hardlink=False, stacked=False,
321
341
               source_branch=None):
322
 
        # dirstate doesn't cope with accelerator_trees well 
 
342
        # dirstate doesn't cope with accelerator_trees well
323
343
        # that have a different control dir
324
 
        return super(DummyForeignVcsDir, self).sprout(url=url, 
325
 
                revision_id=revision_id, force_new_repo=force_new_repo, 
326
 
                recurse=recurse, possible_transports=possible_transports, 
327
 
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
 
344
        return super(DummyForeignVcsDir, self).sprout(
 
345
            url=url,
 
346
            revision_id=revision_id, force_new_repo=force_new_repo,
 
347
            recurse=recurse, possible_transports=possible_transports,
 
348
            hardlink=hardlink, stacked=stacked, source_branch=source_branch)
328
349
 
329
350
 
330
351
def register_dummy_foreign_for_test(testcase):
331
352
    controldir.ControlDirFormat.register_prober(DummyForeignProber)
332
353
    testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
333
 
        DummyForeignProber)
 
354
                        DummyForeignProber)
334
355
    repository.format_registry.register(DummyForeignVcsRepositoryFormat())
335
356
    testcase.addCleanup(repository.format_registry.remove,
336
 
            DummyForeignVcsRepositoryFormat())
 
357
                        DummyForeignVcsRepositoryFormat())
337
358
    branch.format_registry.register(DummyForeignVcsBranchFormat())
338
359
    testcase.addCleanup(branch.format_registry.remove,
339
 
            DummyForeignVcsBranchFormat())
 
360
                        DummyForeignVcsBranchFormat())
340
361
    # We need to register the optimiser to make the dummy appears really
341
362
    # different from a regular bzr repository.
342
363
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
375
396
        reg = foreign.ForeignVcsRegistry()
376
397
        vcs = DummyForeignVcs()
377
398
        reg.register("dummy", vcs, "Dummy VCS")
378
 
        self.assertEquals((
 
399
        self.assertEqual((
379
400
            ("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
380
401
            reg.parse_revision_id("dummy-v1:some-foreign-revid"))
381
402
 
387
408
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
388
409
        rev = foreign.ForeignRevision(("a", "foreign", "revid"),
389
410
                                      mapp, "roundtripped-revid")
390
 
        self.assertEquals("", rev.inventory_sha1)
391
 
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
392
 
        self.assertEquals(mapp, rev.mapping)
 
411
        self.assertEqual("", rev.inventory_sha1)
 
412
        self.assertEqual(("a", "foreign", "revid"), rev.foreign_revid)
 
413
        self.assertEqual(mapp, rev.mapping)
393
414
 
394
415
 
395
416
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
411
432
        foreign.update_workingtree_fileids(wt, target_basis)
412
433
        wt.lock_read()
413
434
        try:
414
 
            self.assertEquals(set([root_id, "bla-b"]), set(wt.inventory))
 
435
            self.assertEqual(set([root_id, "bla-b"]), set(wt.all_file_ids()))
415
436
        finally:
416
437
            wt.unlock()
417
438
 
426
447
    def test_create(self):
427
448
        """Test we can create dummies."""
428
449
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
429
 
        dir = bzrdir.BzrDir.open("d")
430
 
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
 
450
        dir = controldir.ControlDir.open("d")
 
451
        self.assertEqual("A Dummy VCS Dir", dir._format.get_format_string())
431
452
        dir.open_repository()
432
453
        dir.open_branch()
433
454
        dir.open_workingtree()
435
456
    def test_sprout(self):
436
457
        """Test we can clone dummies and that the format is not preserved."""
437
458
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
438
 
        dir = bzrdir.BzrDir.open("d")
 
459
        dir = controldir.ControlDir.open("d")
439
460
        newdir = dir.sprout("e")
440
 
        self.assertNotEquals("A Dummy VCS Dir",
441
 
                             newdir._format.get_format_string())
 
461
        self.assertNotEqual("A Dummy VCS Dir",
 
462
                            newdir._format.get_format_string())
442
463
 
443
464
    def test_push_not_supported(self):
444
465
        source_tree = self.make_branch_and_tree("source")
445
 
        target_tree = self.make_branch_and_tree("target", 
446
 
            format=DummyForeignVcsDirFormat())
447
 
        self.assertRaises(errors.NoRoundtrippingSupport, 
448
 
            source_tree.branch.push, target_tree.branch)
 
466
        target_tree = self.make_branch_and_tree(
 
467
            "target", format=DummyForeignVcsDirFormat())
 
468
        self.assertRaises(errors.NoRoundtrippingSupport,
 
469
                          source_tree.branch.push, target_tree.branch)
449
470
 
450
471
    def test_lossy_push_empty(self):
451
472
        source_tree = self.make_branch_and_tree("source")
452
 
        target_tree = self.make_branch_and_tree("target", 
453
 
            format=DummyForeignVcsDirFormat())
 
473
        target_tree = self.make_branch_and_tree(
 
474
            "target", format=DummyForeignVcsDirFormat())
454
475
        pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
455
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
456
 
        self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
457
 
        self.assertEquals({}, pushresult.revidmap)
 
476
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
 
477
        self.assertEqual(revision.NULL_REVISION, pushresult.new_revid)
 
478
        self.assertEqual({}, pushresult.revidmap)
458
479
 
459
480
    def test_lossy_push_simple(self):
460
481
        source_tree = self.make_branch_and_tree("source")
461
482
        self.build_tree(['source/a', 'source/b'])
462
483
        source_tree.add(['a', 'b'])
463
484
        revid1 = source_tree.commit("msg")
464
 
        target_tree = self.make_branch_and_tree("target", 
465
 
            format=DummyForeignVcsDirFormat())
 
485
        target_tree = self.make_branch_and_tree(
 
486
            "target", format=DummyForeignVcsDirFormat())
466
487
        target_tree.branch.lock_write()
467
488
        try:
468
 
            pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
 
489
            pushresult = source_tree.branch.push(
 
490
                target_tree.branch, lossy=True)
469
491
        finally:
470
492
            target_tree.branch.unlock()
471
 
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
472
 
        self.assertEquals({revid1:target_tree.branch.last_revision()}, 
473
 
                           pushresult.revidmap)
474
 
        self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)
 
493
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
 
494
        self.assertEqual({revid1: target_tree.branch.last_revision()},
 
495
                         pushresult.revidmap)
 
496
        self.assertEqual(pushresult.revidmap[revid1], pushresult.new_revid)