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

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2008-2011 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 .. import (
 
21
from bzrlib import (
22
22
    branch,
 
23
    bzrdir,
23
24
    controldir,
24
25
    errors,
25
26
    foreign,
29
30
    revision,
30
31
    tests,
31
32
    trace,
32
 
    )
33
 
from ..bzr import (
34
 
    branch as bzrbranch,
35
 
    bzrdir,
36
33
    vf_repository,
37
34
    )
38
35
 
39
 
from ..bzr import groupcompress_repo
 
36
from bzrlib.repofmt import groupcompress_repo
40
37
 
41
38
# This is the dummy foreign revision control system, used 
42
39
# mainly here in the testsuite to test the foreign VCS infrastructure.
53
50
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
54
51
 
55
52
    def __eq__(self, other):
56
 
        return isinstance(self, type(other))
 
53
        return type(self) == type(other)
57
54
 
58
55
    def revision_id_bzr_to_foreign(self, bzr_revid):
59
56
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
91
88
        return "%s|%s|%s" % foreign_revid
92
89
 
93
90
 
94
 
class DummyForeignVcsBranch(bzrbranch.BzrBranch6, foreign.ForeignBranch):
 
91
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
95
92
    """A Dummy VCS Branch."""
96
93
 
97
94
    @property
98
95
    def user_transport(self):
99
 
        return self.controldir.user_transport
 
96
        return self.bzrdir.user_transport
100
97
 
101
 
    def __init__(self, _format, _control_files, a_controldir, *args, **kwargs):
 
98
    def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
102
99
        self._format = _format
103
 
        self._base = a_controldir.transport.base
 
100
        self._base = a_bzrdir.transport.base
104
101
        self._ignore_fallbacks = False
105
 
        self.controldir = a_controldir
 
102
        self.bzrdir = a_bzrdir
106
103
        foreign.ForeignBranch.__init__(self,
107
104
            DummyForeignVcsMapping(DummyForeignVcs()))
108
 
        bzrbranch.BzrBranch6.__init__(self, _format, _control_files, a_controldir,
 
105
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
109
106
            *args, **kwargs)
110
107
 
111
108
    def _get_checkout_format(self, lightweight=False):
112
109
        """Return the most suitable metadir for a checkout of this branch.
113
110
        Weaves are used if this branch's repository uses weaves.
114
111
        """
115
 
        return self.controldir.checkout_metadir()
 
112
        return self.bzrdir.checkout_metadir()
116
113
 
117
114
    def import_last_revision_info_and_tags(self, source, revno, revid,
118
115
                                           lossy=False):
125
122
 
126
123
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
127
124
 
128
 
    def _generate_revision_if_needed(self, revid):
 
125
    def _generate_revision_if_needed(self):
129
126
        mapping = DummyForeignVcsMapping(DummyForeignVcs())
130
127
        if self._lossy:
131
128
            self._new_revision_id = mapping.revision_id_foreign_to_bzr(
132
129
                (str(self._timestamp), str(self._timezone), "UNKNOWN"))
133
130
            self.random_revid = False
134
 
        elif revid is not None:
135
 
            self._new_revision_id = revid
 
131
        elif self._new_revision_id is not None:
136
132
            self.random_revid = False
137
133
        else:
138
134
            self._new_revision_id = self._gen_revision_id()
151
147
 
152
148
    @classmethod
153
149
    def get_format_string(cls):
154
 
        return b"Dummy Foreign Vcs Repository"
 
150
        return "Dummy Foreign Vcs Repository"
155
151
 
156
152
    def get_format_description(self):
157
153
        return "Dummy Foreign Vcs Repository"
193
189
            for revid in todo:
194
190
                rev = self.source.repository.get_revision(revid)
195
191
                tree = self.source.repository.revision_tree(revid)
196
 
                def get_file_with_stat(path, file_id=None):
197
 
                    return (tree.get_file(path, file_id), None)
 
192
                def get_file_with_stat(file_id, path=None):
 
193
                    return (tree.get_file(file_id), None)
198
194
                tree.get_file_with_stat = get_file_with_stat
199
195
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
200
196
                    (str(rev.timestamp), str(rev.timezone),
204
200
                    parent_revids = []
205
201
                else:
206
202
                    parent_revids = [parent_revid]
207
 
                builder = self.target.get_commit_builder(parent_revids,
 
203
                builder = self.target.get_commit_builder(parent_revids, 
208
204
                        self.target.get_config_stack(), rev.timestamp,
209
205
                        rev.timezone, rev.committer, rev.properties,
210
206
                        new_revid)
211
207
                try:
212
208
                    parent_tree = self.target.repository.revision_tree(
213
209
                        parent_revid)
214
 
                    iter_changes = tree.iter_changes(parent_tree)
215
 
                    list(builder.record_iter_changes(
216
 
                        tree, parent_revid, iter_changes))
 
210
                    for path, ie in tree.iter_entries_by_dir():
 
211
                        new_ie = ie.copy()
 
212
                        new_ie.revision = None
 
213
                        builder.record_entry_contents(new_ie, 
 
214
                            [parent_tree.root_inventory],
 
215
                            path, tree, 
 
216
                            (ie.kind, ie.text_size, ie.executable, ie.text_sha1))
217
217
                    builder.finish_inventory()
218
218
                except:
219
219
                    builder.abort()
220
220
                    raise
221
221
                revidmap[revid] = builder.commit(rev.message)
222
 
                self.target.set_last_revision_info(parent_revno+1,
 
222
                self.target.set_last_revision_info(parent_revno+1, 
223
223
                    revidmap[revid])
224
 
                trace.mutter('lossily pushed revision %s -> %s',
 
224
                trace.mutter('lossily pushed revision %s -> %s', 
225
225
                    revid, revidmap[revid])
226
226
        finally:
227
227
            self.source.unlock()
230
230
        return result
231
231
 
232
232
 
233
 
class DummyForeignVcsBranchFormat(bzrbranch.BzrBranchFormat6):
 
233
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
234
234
 
235
235
    @classmethod
236
236
    def get_format_string(cls):
237
 
        return b"Branch for Testing"
 
237
        return "Branch for Testing"
238
238
 
239
239
    @property
240
 
    def _matchingcontroldir(self):
 
240
    def _matchingbzrdir(self):
241
241
        return DummyForeignVcsDirFormat()
242
242
 
243
 
    def open(self, a_controldir, name=None, _found=False, ignore_fallbacks=False,
 
243
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
244
244
            found_repository=None):
245
245
        if name is None:
246
 
            name = a_controldir._get_selected_branch()
 
246
            name = a_bzrdir._get_selected_branch()
247
247
        if not _found:
248
248
            raise NotImplementedError
249
249
        try:
250
 
            transport = a_controldir.get_branch_transport(None, name=name)
 
250
            transport = a_bzrdir.get_branch_transport(None, name=name)
251
251
            control_files = lockable_files.LockableFiles(transport, 'lock',
252
252
                                                         lockdir.LockDir)
253
253
            if found_repository is None:
254
 
                found_repository = a_controldir.find_repository()
 
254
                found_repository = a_bzrdir.find_repository()
255
255
            return DummyForeignVcsBranch(_format=self,
256
256
                              _control_files=control_files,
257
 
                              a_controldir=a_controldir,
 
257
                              a_bzrdir=a_bzrdir,
258
258
                              _repository=found_repository,
259
259
                              name=name)
260
260
        except errors.NoSuchFile:
266
266
 
267
267
    @classmethod
268
268
    def get_format_string(cls):
269
 
        return b"A Dummy VCS Dir"
 
269
        return "A Dummy VCS Dir"
270
270
 
271
271
    @classmethod
272
272
    def get_format_description(cls):
321
321
        return super(DummyForeignVcsDir, self).create_workingtree()
322
322
 
323
323
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
324
 
                    possible_transports=None):
 
324
            possible_transports=None):
325
325
        if name is None:
326
326
            name = self._get_selected_branch()
327
327
        if name != "":
330
330
 
331
331
    def cloning_metadir(self, stacked=False):
332
332
        """Produce a metadir suitable for cloning with."""
333
 
        return controldir.format_registry.make_controldir("default")
 
333
        return controldir.format_registry.make_bzrdir("default")
334
334
 
335
335
    def checkout_metadir(self):
336
336
        return self.cloning_metadir()
339
339
               recurse='down', possible_transports=None,
340
340
               accelerator_tree=None, hardlink=False, stacked=False,
341
341
               source_branch=None):
342
 
        # dirstate doesn't cope with accelerator_trees well
 
342
        # dirstate doesn't cope with accelerator_trees well 
343
343
        # that have a different control dir
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)
 
344
        return super(DummyForeignVcsDir, self).sprout(url=url, 
 
345
                revision_id=revision_id, force_new_repo=force_new_repo, 
 
346
                recurse=recurse, possible_transports=possible_transports, 
 
347
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
349
348
 
350
349
 
351
350
def register_dummy_foreign_for_test(testcase):
352
351
    controldir.ControlDirFormat.register_prober(DummyForeignProber)
353
352
    testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
354
 
                        DummyForeignProber)
 
353
        DummyForeignProber)
355
354
    repository.format_registry.register(DummyForeignVcsRepositoryFormat())
356
355
    testcase.addCleanup(repository.format_registry.remove,
357
 
                        DummyForeignVcsRepositoryFormat())
 
356
            DummyForeignVcsRepositoryFormat())
358
357
    branch.format_registry.register(DummyForeignVcsBranchFormat())
359
358
    testcase.addCleanup(branch.format_registry.remove,
360
 
                        DummyForeignVcsBranchFormat())
 
359
            DummyForeignVcsBranchFormat())
361
360
    # We need to register the optimiser to make the dummy appears really
362
361
    # different from a regular bzr repository.
363
362
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
376
375
 
377
376
    @classmethod
378
377
    def known_formats(cls):
379
 
        return {DummyForeignVcsDirFormat()}
 
378
        return set([DummyForeignVcsDirFormat()])
380
379
 
381
380
 
382
381
class ForeignVcsRegistryTests(tests.TestCase):
396
395
        reg = foreign.ForeignVcsRegistry()
397
396
        vcs = DummyForeignVcs()
398
397
        reg.register("dummy", vcs, "Dummy VCS")
399
 
        self.assertEqual((
 
398
        self.assertEquals((
400
399
            ("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
401
400
            reg.parse_revision_id("dummy-v1:some-foreign-revid"))
402
401
 
408
407
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
409
408
        rev = foreign.ForeignRevision(("a", "foreign", "revid"),
410
409
                                      mapp, "roundtripped-revid")
411
 
        self.assertEqual("", rev.inventory_sha1)
412
 
        self.assertEqual(("a", "foreign", "revid"), rev.foreign_revid)
413
 
        self.assertEqual(mapp, rev.mapping)
 
410
        self.assertEquals("", rev.inventory_sha1)
 
411
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
 
412
        self.assertEquals(mapp, rev.mapping)
414
413
 
415
414
 
416
415
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
418
417
 
419
418
    def test_update_workingtree(self):
420
419
        wt = self.make_branch_and_tree('br1')
421
 
        self.build_tree_contents([('br1/bla', b'original contents\n')])
 
420
        self.build_tree_contents([('br1/bla', 'original contents\n')])
422
421
        wt.add('bla', 'bla-a')
423
422
        wt.commit('bla-a')
424
423
        root_id = wt.get_root_id()
425
 
        target = wt.controldir.sprout('br2').open_workingtree()
426
 
        target.unversion(['bla'])
 
424
        target = wt.bzrdir.sprout('br2').open_workingtree()
 
425
        target.unversion(['bla-a'])
427
426
        target.add('bla', 'bla-b')
428
427
        target.commit('bla-b')
429
428
        target_basis = target.basis_tree()
432
431
        foreign.update_workingtree_fileids(wt, target_basis)
433
432
        wt.lock_read()
434
433
        try:
435
 
            self.assertEqual({'', "bla"}, set(wt.all_versioned_paths()))
 
434
            self.assertEquals(set([root_id, "bla-b"]), set(wt.all_file_ids()))
436
435
        finally:
437
436
            wt.unlock()
438
437
 
448
447
        """Test we can create dummies."""
449
448
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
450
449
        dir = controldir.ControlDir.open("d")
451
 
        self.assertEqual("A Dummy VCS Dir", dir._format.get_format_string())
 
450
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
452
451
        dir.open_repository()
453
452
        dir.open_branch()
454
453
        dir.open_workingtree()
458
457
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
459
458
        dir = controldir.ControlDir.open("d")
460
459
        newdir = dir.sprout("e")
461
 
        self.assertNotEqual("A Dummy VCS Dir",
462
 
                            newdir._format.get_format_string())
 
460
        self.assertNotEquals("A Dummy VCS Dir",
 
461
                             newdir._format.get_format_string())
463
462
 
464
463
    def test_push_not_supported(self):
465
464
        source_tree = self.make_branch_and_tree("source")
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)
 
465
        target_tree = self.make_branch_and_tree("target", 
 
466
            format=DummyForeignVcsDirFormat())
 
467
        self.assertRaises(errors.NoRoundtrippingSupport, 
 
468
            source_tree.branch.push, target_tree.branch)
470
469
 
471
470
    def test_lossy_push_empty(self):
472
471
        source_tree = self.make_branch_and_tree("source")
473
 
        target_tree = self.make_branch_and_tree(
474
 
            "target", format=DummyForeignVcsDirFormat())
 
472
        target_tree = self.make_branch_and_tree("target", 
 
473
            format=DummyForeignVcsDirFormat())
475
474
        pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
476
 
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
477
 
        self.assertEqual(revision.NULL_REVISION, pushresult.new_revid)
478
 
        self.assertEqual({}, pushresult.revidmap)
 
475
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
 
476
        self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
 
477
        self.assertEquals({}, pushresult.revidmap)
479
478
 
480
479
    def test_lossy_push_simple(self):
481
480
        source_tree = self.make_branch_and_tree("source")
482
481
        self.build_tree(['source/a', 'source/b'])
483
482
        source_tree.add(['a', 'b'])
484
483
        revid1 = source_tree.commit("msg")
485
 
        target_tree = self.make_branch_and_tree(
486
 
            "target", format=DummyForeignVcsDirFormat())
 
484
        target_tree = self.make_branch_and_tree("target", 
 
485
            format=DummyForeignVcsDirFormat())
487
486
        target_tree.branch.lock_write()
488
487
        try:
489
488
            pushresult = source_tree.branch.push(
490
489
                target_tree.branch, lossy=True)
491
490
        finally:
492
491
            target_tree.branch.unlock()
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)
 
492
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
 
493
        self.assertEquals({revid1:target_tree.branch.last_revision()}, 
 
494
                           pushresult.revidmap)
 
495
        self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)