/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: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2012, 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
"""Tests for foreign VCS utility code."""
 
19
 
 
20
 
 
21
from .. import (
 
22
    branch,
 
23
    controldir,
 
24
    errors,
 
25
    foreign,
 
26
    lockable_files,
 
27
    lockdir,
 
28
    repository,
 
29
    revision,
 
30
    tests,
 
31
    trace,
 
32
    )
 
33
from ..bzr import (
 
34
    branch as bzrbranch,
 
35
    bzrdir,
 
36
    vf_repository,
 
37
    )
 
38
 
 
39
from ..bzr import groupcompress_repo
 
40
 
 
41
# This is the dummy foreign revision control system, used
 
42
# mainly here in the testsuite to test the foreign VCS infrastructure.
 
43
# It is basically standard Bazaar with some minor modifications to
 
44
# make it "foreign".
 
45
#
 
46
# It has the following differences to "regular" Bazaar:
 
47
# - The control directory is named ".dummy", not ".bzr".
 
48
# - The revision ids are tuples, not strings.
 
49
# - Doesn't support more than one parent natively
 
50
 
 
51
 
 
52
class DummyForeignVcsMapping(foreign.VcsMapping):
 
53
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
 
54
 
 
55
    def __eq__(self, other):
 
56
        return isinstance(self, type(other))
 
57
 
 
58
    def revision_id_bzr_to_foreign(self, bzr_revid):
 
59
        return tuple(bzr_revid[len(b"dummy-v1:"):].split(b"-")), self
 
60
 
 
61
    def revision_id_foreign_to_bzr(self, foreign_revid):
 
62
        return b"dummy-v1:%s-%s-%s" % foreign_revid
 
63
 
 
64
 
 
65
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
 
66
 
 
67
    def revision_id_bzr_to_foreign(self, revid):
 
68
        if not revid.startswith(b"dummy-"):
 
69
            raise errors.InvalidRevisionId(revid, None)
 
70
        mapping_version = revid[len(b"dummy-"):len(b"dummy-vx")]
 
71
        mapping = self.get(mapping_version)
 
72
        return mapping.revision_id_bzr_to_foreign(revid)
 
73
 
 
74
 
 
75
class DummyForeignVcs(foreign.ForeignVcs):
 
76
    """A dummy Foreign VCS, for use with testing.
 
77
 
 
78
    It has revision ids that are a tuple with three strings.
 
79
    """
 
80
 
 
81
    def __init__(self):
 
82
        self.mapping_registry = DummyForeignVcsMappingRegistry()
 
83
        self.mapping_registry.register(b"v1", DummyForeignVcsMapping(self),
 
84
                                       "Version 1")
 
85
        self.abbreviation = "dummy"
 
86
 
 
87
    def show_foreign_revid(self, foreign_revid):
 
88
        return {"dummy ding": "%s/%s\\%s" % foreign_revid}
 
89
 
 
90
    def serialize_foreign_revid(self, foreign_revid):
 
91
        return "%s|%s|%s" % foreign_revid
 
92
 
 
93
 
 
94
class DummyForeignVcsBranch(bzrbranch.BzrBranch6, foreign.ForeignBranch):
 
95
    """A Dummy VCS Branch."""
 
96
 
 
97
    @property
 
98
    def user_transport(self):
 
99
        return self.controldir.user_transport
 
100
 
 
101
    def __init__(self, _format, _control_files, a_controldir, *args, **kwargs):
 
102
        self._format = _format
 
103
        self._base = a_controldir.transport.base
 
104
        self._ignore_fallbacks = False
 
105
        self.controldir = a_controldir
 
106
        foreign.ForeignBranch.__init__(self,
 
107
                                       DummyForeignVcsMapping(DummyForeignVcs()))
 
108
        bzrbranch.BzrBranch6.__init__(self, _format, _control_files, a_controldir,
 
109
                                      *args, **kwargs)
 
110
 
 
111
    def _get_checkout_format(self, lightweight=False):
 
112
        """Return the most suitable metadir for a checkout of this branch.
 
113
        Weaves are used if this branch's repository uses weaves.
 
114
        """
 
115
        return self.controldir.checkout_metadir()
 
116
 
 
117
    def import_last_revision_info_and_tags(self, source, revno, revid,
 
118
                                           lossy=False):
 
119
        interbranch = InterToDummyVcsBranch(source, self)
 
120
        result = interbranch.push(stop_revision=revid, lossy=True)
 
121
        if lossy:
 
122
            revid = result.revidmap[revid]
 
123
        return (revno, revid)
 
124
 
 
125
 
 
126
class DummyForeignCommitBuilder(vf_repository.VersionedFileCommitBuilder):
 
127
 
 
128
    def _generate_revision_if_needed(self, revid):
 
129
        mapping = DummyForeignVcsMapping(DummyForeignVcs())
 
130
        if self._lossy:
 
131
            self._new_revision_id = mapping.revision_id_foreign_to_bzr(
 
132
                (b'%d' % self._timestamp,
 
133
                 str(self._timezone).encode('ascii'), b"UNKNOWN"))
 
134
            self.random_revid = False
 
135
        elif revid is not None:
 
136
            self._new_revision_id = revid
 
137
            self.random_revid = False
 
138
        else:
 
139
            self._new_revision_id = self._gen_revision_id()
 
140
            self.random_revid = True
 
141
 
 
142
 
 
143
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
 
144
                                foreign.ForeignRepository):
 
145
    """Dummy foreign vcs repository."""
 
146
 
 
147
 
 
148
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
 
149
 
 
150
    repository_class = DummyForeignVcsRepository
 
151
    _commit_builder_class = DummyForeignCommitBuilder
 
152
 
 
153
    @classmethod
 
154
    def get_format_string(cls):
 
155
        return b"Dummy Foreign Vcs Repository"
 
156
 
 
157
    def get_format_description(self):
 
158
        return "Dummy Foreign Vcs Repository"
 
159
 
 
160
 
 
161
def branch_history(graph, revid):
 
162
    ret = list(graph.iter_lefthand_ancestry(revid,
 
163
                                            (revision.NULL_REVISION,)))
 
164
    ret.reverse()
 
165
    return ret
 
166
 
 
167
 
 
168
class InterToDummyVcsBranch(branch.GenericInterBranch):
 
169
 
 
170
    @staticmethod
 
171
    def is_compatible(source, target):
 
172
        return isinstance(target, DummyForeignVcsBranch)
 
173
 
 
174
    def push(self, overwrite=False, stop_revision=None, lossy=False, tag_selector=None):
 
175
        if not lossy:
 
176
            raise errors.NoRoundtrippingSupport(self.source, self.target)
 
177
        result = branch.BranchPushResult()
 
178
        result.source_branch = self.source
 
179
        result.target_branch = self.target
 
180
        result.old_revno, result.old_revid = self.target.last_revision_info()
 
181
        self.source.lock_read()
 
182
        try:
 
183
            graph = self.source.repository.get_graph()
 
184
            # This just handles simple cases, but that's good enough for tests
 
185
            my_history = branch_history(self.target.repository.get_graph(),
 
186
                                        result.old_revid)
 
187
            if stop_revision is None:
 
188
                stop_revision = self.source.last_revision()
 
189
            their_history = branch_history(graph, stop_revision)
 
190
            if their_history[:min(len(my_history), len(their_history))] != my_history:
 
191
                raise errors.DivergedBranches(self.target, self.source)
 
192
            todo = their_history[len(my_history):]
 
193
            revidmap = {}
 
194
            for revid in todo:
 
195
                rev = self.source.repository.get_revision(revid)
 
196
                tree = self.source.repository.revision_tree(revid)
 
197
                def get_file_with_stat(path):
 
198
                    return (tree.get_file(path), None)
 
199
                tree.get_file_with_stat = get_file_with_stat
 
200
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
 
201
                    (b'%d' % rev.timestamp, str(rev.timezone).encode('ascii'),
 
202
                        str(self.target.revno()).encode('ascii')))
 
203
                parent_revno, parent_revid = self.target.last_revision_info()
 
204
                if parent_revid == revision.NULL_REVISION:
 
205
                    parent_revids = []
 
206
                else:
 
207
                    parent_revids = [parent_revid]
 
208
                builder = self.target.get_commit_builder(
 
209
                    parent_revids, self.target.get_config_stack(), rev.timestamp,
 
210
                    rev.timezone, rev.committer, rev.properties,
 
211
                    new_revid)
 
212
                try:
 
213
                    parent_tree = self.target.repository.revision_tree(
 
214
                        parent_revid)
 
215
                    iter_changes = tree.iter_changes(parent_tree)
 
216
                    list(builder.record_iter_changes(
 
217
                        tree, parent_revid, iter_changes))
 
218
                    builder.finish_inventory()
 
219
                except:
 
220
                    builder.abort()
 
221
                    raise
 
222
                revidmap[revid] = builder.commit(rev.message)
 
223
                self.target.set_last_revision_info(parent_revno + 1,
 
224
                                                   revidmap[revid])
 
225
                trace.mutter('lossily pushed revision %s -> %s',
 
226
                             revid, revidmap[revid])
 
227
        finally:
 
228
            self.source.unlock()
 
229
        result.new_revno, result.new_revid = self.target.last_revision_info()
 
230
        result.revidmap = revidmap
 
231
        return result
 
232
 
 
233
 
 
234
class DummyForeignVcsBranchFormat(bzrbranch.BzrBranchFormat6):
 
235
 
 
236
    @classmethod
 
237
    def get_format_string(cls):
 
238
        return b"Branch for Testing"
 
239
 
 
240
    @property
 
241
    def _matchingcontroldir(self):
 
242
        return DummyForeignVcsDirFormat()
 
243
 
 
244
    def open(self, a_controldir, name=None, _found=False, ignore_fallbacks=False,
 
245
             found_repository=None):
 
246
        if name is None:
 
247
            name = a_controldir._get_selected_branch()
 
248
        if not _found:
 
249
            raise NotImplementedError
 
250
        try:
 
251
            transport = a_controldir.get_branch_transport(None, name=name)
 
252
            control_files = lockable_files.LockableFiles(transport, 'lock',
 
253
                                                         lockdir.LockDir)
 
254
            if found_repository is None:
 
255
                found_repository = a_controldir.find_repository()
 
256
            return DummyForeignVcsBranch(_format=self,
 
257
                                         _control_files=control_files,
 
258
                                         a_controldir=a_controldir,
 
259
                                         _repository=found_repository,
 
260
                                         name=name)
 
261
        except errors.NoSuchFile:
 
262
            raise errors.NotBranchError(path=transport.base)
 
263
 
 
264
 
 
265
class DummyForeignVcsDirFormat(bzrdir.BzrDirMetaFormat1):
 
266
    """BzrDirFormat for the dummy foreign VCS."""
 
267
 
 
268
    @classmethod
 
269
    def get_format_string(cls):
 
270
        return b"A Dummy VCS Dir"
 
271
 
 
272
    @classmethod
 
273
    def get_format_description(cls):
 
274
        return "A Dummy VCS Dir"
 
275
 
 
276
    @classmethod
 
277
    def is_supported(cls):
 
278
        return True
 
279
 
 
280
    def get_branch_format(self):
 
281
        return DummyForeignVcsBranchFormat()
 
282
 
 
283
    @property
 
284
    def repository_format(self):
 
285
        return DummyForeignVcsRepositoryFormat()
 
286
 
 
287
    def initialize_on_transport(self, transport):
 
288
        """Initialize a new bzrdir in the base directory of a Transport."""
 
289
        # Since we don't have a .bzr directory, inherit the
 
290
        # mode from the root directory
 
291
        temp_control = lockable_files.LockableFiles(transport,
 
292
                                                    '', lockable_files.TransportLock)
 
293
        temp_control._transport.mkdir('.dummy',
 
294
                                      # FIXME: RBC 20060121 don't peek under
 
295
                                      # the covers
 
296
                                      mode=temp_control._dir_mode)
 
297
        del temp_control
 
298
        bzrdir_transport = transport.clone('.dummy')
 
299
        # NB: no need to escape relative paths that are url safe.
 
300
        control_files = lockable_files.LockableFiles(bzrdir_transport,
 
301
                                                     self._lock_file_name, self._lock_class)
 
302
        control_files.create_lock()
 
303
        return self.open(transport, _found=True)
 
304
 
 
305
    def _open(self, transport):
 
306
        return DummyForeignVcsDir(transport, self)
 
307
 
 
308
 
 
309
class DummyForeignVcsDir(bzrdir.BzrDirMeta1):
 
310
 
 
311
    def __init__(self, _transport, _format):
 
312
        self._format = _format
 
313
        self.transport = _transport.clone('.dummy')
 
314
        self.root_transport = _transport
 
315
        self._mode_check_done = False
 
316
        self._control_files = lockable_files.LockableFiles(self.transport,
 
317
                                                           "lock", lockable_files.TransportLock)
 
318
 
 
319
    def create_workingtree(self):
 
320
        # dirstate requires a ".bzr" entry to exist
 
321
        self.root_transport.put_bytes(".bzr", b"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 != "":
 
329
            raise errors.NoColocatedBranchSupport(self)
 
330
        return self._format.get_branch_format().open(self, _found=True)
 
331
 
 
332
    def cloning_metadir(self, stacked=False):
 
333
        """Produce a metadir suitable for cloning with."""
 
334
        return controldir.format_registry.make_controldir("default")
 
335
 
 
336
    def checkout_metadir(self):
 
337
        return self.cloning_metadir()
 
338
 
 
339
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
340
               recurse='down', possible_transports=None,
 
341
               accelerator_tree=None, hardlink=False, stacked=False,
 
342
               source_branch=None):
 
343
        # dirstate doesn't cope with accelerator_trees well
 
344
        # that have a different control dir
 
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)
 
350
 
 
351
 
 
352
def register_dummy_foreign_for_test(testcase):
 
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())
 
362
    # We need to register the optimiser to make the dummy appears really
 
363
    # different from a regular bzr repository.
 
364
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
 
365
    testcase.addCleanup(branch.InterBranch.unregister_optimiser,
 
366
                        InterToDummyVcsBranch)
 
367
 
 
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
 
 
383
class ForeignVcsRegistryTests(tests.TestCase):
 
384
    """Tests for the ForeignVcsRegistry class."""
 
385
 
 
386
    def test_parse_revision_id_no_dash(self):
 
387
        reg = foreign.ForeignVcsRegistry()
 
388
        self.assertRaises(errors.InvalidRevisionId,
 
389
                          reg.parse_revision_id, b"invalid")
 
390
 
 
391
    def test_parse_revision_id_unknown_mapping(self):
 
392
        reg = foreign.ForeignVcsRegistry()
 
393
        self.assertRaises(errors.InvalidRevisionId,
 
394
                          reg.parse_revision_id, b"unknown-foreignrevid")
 
395
 
 
396
    def test_parse_revision_id(self):
 
397
        reg = foreign.ForeignVcsRegistry()
 
398
        vcs = DummyForeignVcs()
 
399
        reg.register("dummy", vcs, "Dummy VCS")
 
400
        self.assertEqual((
 
401
            (b"some", b"foreign", b"revid"), DummyForeignVcsMapping(vcs)),
 
402
            reg.parse_revision_id(b"dummy-v1:some-foreign-revid"))
 
403
 
 
404
 
 
405
class ForeignRevisionTests(tests.TestCase):
 
406
    """Tests for the ForeignRevision class."""
 
407
 
 
408
    def test_create(self):
 
409
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
 
410
        rev = foreign.ForeignRevision((b"a", b"foreign", b"revid"),
 
411
                                      mapp, b"roundtripped-revid")
 
412
        self.assertEqual(b"", rev.inventory_sha1)
 
413
        self.assertEqual((b"a", b"foreign", b"revid"), rev.foreign_revid)
 
414
        self.assertEqual(mapp, rev.mapping)
 
415
 
 
416
 
 
417
class DummyForeignVcsTests(tests.TestCaseWithTransport):
 
418
    """Very basic test for DummyForeignVcs."""
 
419
 
 
420
    def setUp(self):
 
421
        super(DummyForeignVcsTests, self).setUp()
 
422
        register_dummy_foreign_for_test(self)
 
423
 
 
424
    def test_create(self):
 
425
        """Test we can create dummies."""
 
426
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
 
427
        dir = controldir.ControlDir.open("d")
 
428
        self.assertEqual(b"A Dummy VCS Dir", dir._format.get_format_string())
 
429
        dir.open_repository()
 
430
        dir.open_branch()
 
431
        dir.open_workingtree()
 
432
 
 
433
    def test_sprout(self):
 
434
        """Test we can clone dummies and that the format is not preserved."""
 
435
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
 
436
        dir = controldir.ControlDir.open("d")
 
437
        newdir = dir.sprout("e")
 
438
        self.assertNotEqual(b"A Dummy VCS Dir",
 
439
                            newdir._format.get_format_string())
 
440
 
 
441
    def test_push_not_supported(self):
 
442
        source_tree = self.make_branch_and_tree("source")
 
443
        target_tree = self.make_branch_and_tree(
 
444
            "target", format=DummyForeignVcsDirFormat())
 
445
        self.assertRaises(errors.NoRoundtrippingSupport,
 
446
                          source_tree.branch.push, target_tree.branch)
 
447
 
 
448
    def test_lossy_push_empty(self):
 
449
        source_tree = self.make_branch_and_tree("source")
 
450
        target_tree = self.make_branch_and_tree(
 
451
            "target", format=DummyForeignVcsDirFormat())
 
452
        pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
 
453
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
 
454
        self.assertEqual(revision.NULL_REVISION, pushresult.new_revid)
 
455
        self.assertEqual({}, pushresult.revidmap)
 
456
 
 
457
    def test_lossy_push_simple(self):
 
458
        source_tree = self.make_branch_and_tree("source")
 
459
        self.build_tree(['source/a', 'source/b'])
 
460
        source_tree.add(['a', 'b'])
 
461
        revid1 = source_tree.commit("msg")
 
462
        target_tree = self.make_branch_and_tree(
 
463
            "target", format=DummyForeignVcsDirFormat())
 
464
        target_tree.branch.lock_write()
 
465
        try:
 
466
            pushresult = source_tree.branch.push(
 
467
                target_tree.branch, lossy=True)
 
468
        finally:
 
469
            target_tree.branch.unlock()
 
470
        self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
 
471
        self.assertEqual({revid1: target_tree.branch.last_revision()},
 
472
                         pushresult.revidmap)
 
473
        self.assertEqual(pushresult.revidmap[revid1], pushresult.new_revid)