/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: Jelmer Vernooij
  • Date: 2011-12-05 17:31:43 UTC
  • mto: This revision was merged to the branch mainline in revision 6348.
  • Revision ID: jelmer@samba.org-20111205173143-jwj2xtdwpgpn4ic0
Fix test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 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
21
21
from bzrlib import (
22
22
    branch,
23
23
    bzrdir,
 
24
    controldir,
24
25
    errors,
25
26
    foreign,
26
27
    lockable_files,
27
28
    lockdir,
 
29
    repository,
28
30
    revision,
29
31
    tests,
30
32
    trace,
 
33
    vf_repository,
31
34
    )
32
35
 
 
36
from bzrlib.repofmt import groupcompress_repo
 
37
 
33
38
# This is the dummy foreign revision control system, used 
34
39
# mainly here in the testsuite to test the foreign VCS infrastructure.
35
40
# It is basically standard Bazaar with some minor modifications to 
86
91
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
87
92
    """A Dummy VCS Branch."""
88
93
 
 
94
    @property
 
95
    def user_transport(self):
 
96
        return self.bzrdir.user_transport
 
97
 
89
98
    def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
90
99
        self._format = _format
91
100
        self._base = a_bzrdir.transport.base
92
101
        self._ignore_fallbacks = False
93
102
        self.bzrdir = a_bzrdir
94
 
        foreign.ForeignBranch.__init__(self, 
 
103
        foreign.ForeignBranch.__init__(self,
95
104
            DummyForeignVcsMapping(DummyForeignVcs()))
96
 
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir, 
 
105
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
97
106
            *args, **kwargs)
98
107
 
99
 
 
100
 
class InterToDummyVcsBranch(branch.GenericInterBranch,
101
 
                            foreign.InterToForeignBranch):
 
108
    def _get_checkout_format(self, lightweight=False):
 
109
        """Return the most suitable metadir for a checkout of this branch.
 
110
        Weaves are used if this branch's repository uses weaves.
 
111
        """
 
112
        return self.bzrdir.checkout_metadir()
 
113
 
 
114
    def import_last_revision_info_and_tags(self, source, revno, revid,
 
115
                                           lossy=False):
 
116
        interbranch = InterToDummyVcsBranch(source, self)
 
117
        result = interbranch.push(stop_revision=revid, lossy=True)
 
118
        if lossy:
 
119
            revid = result.revidmap[revid]
 
120
        return (revno, revid)
 
121
 
 
122
 
 
123
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
 
124
 
 
125
    def _generate_revision_if_needed(self):
 
126
        mapping = DummyForeignVcsMapping(DummyForeignVcs())
 
127
        if self._lossy:
 
128
            self._new_revision_id = mapping.revision_id_foreign_to_bzr(
 
129
                (str(self._timestamp), str(self._timezone), "UNKNOWN"))
 
130
            self.random_revid = False
 
131
        elif self._new_revision_id is not None:
 
132
            self.random_revid = False
 
133
        else:
 
134
            self._new_revision_id = self._gen_revision_id()
 
135
            self.random_revid = True
 
136
 
 
137
 
 
138
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
 
139
    foreign.ForeignRepository):
 
140
    """Dummy foreign vcs repository."""
 
141
 
 
142
 
 
143
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
 
144
 
 
145
    repository_class = DummyForeignVcsRepository
 
146
    _commit_builder_class = DummyForeignCommitBuilder
 
147
 
 
148
    def get_format_string(self):
 
149
        return "Dummy Foreign Vcs Repository"
 
150
 
 
151
    def get_format_description(self):
 
152
        return "Dummy Foreign Vcs Repository"
 
153
 
 
154
 
 
155
def branch_history(graph, revid):
 
156
    ret = list(graph.iter_lefthand_ancestry(revid,
 
157
        (revision.NULL_REVISION,)))
 
158
    ret.reverse()
 
159
    return ret
 
160
 
 
161
 
 
162
class InterToDummyVcsBranch(branch.GenericInterBranch):
102
163
 
103
164
    @staticmethod
104
165
    def is_compatible(source, target):
105
166
        return isinstance(target, DummyForeignVcsBranch)
106
167
 
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):
 
168
    def push(self, overwrite=False, stop_revision=None, lossy=False):
 
169
        if not lossy:
 
170
            raise errors.NoRoundtrippingSupport(self.source, self.target)
111
171
        result = branch.BranchPushResult()
112
172
        result.source_branch = self.source
113
173
        result.target_branch = self.target
114
174
        result.old_revno, result.old_revid = self.target.last_revision_info()
115
175
        self.source.lock_read()
116
176
        try:
 
177
            graph = self.source.repository.get_graph()
117
178
            # 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()
 
179
            my_history = branch_history(self.target.repository.get_graph(),
 
180
                result.old_revid)
 
181
            if stop_revision is None:
 
182
                stop_revision = self.source.last_revision()
 
183
            their_history = branch_history(graph, stop_revision)
120
184
            if their_history[:min(len(my_history), len(their_history))] != my_history:
121
185
                raise errors.DivergedBranches(self.target, self.source)
122
186
            todo = their_history[len(my_history):]
128
192
                    return (tree.get_file(file_id), None)
129
193
                tree.get_file_with_stat = get_file_with_stat
130
194
                new_revid = self.target.mapping.revision_id_foreign_to_bzr(
131
 
                    (str(rev.timestamp), str(rev.timezone), 
 
195
                    (str(rev.timestamp), str(rev.timezone),
132
196
                        str(self.target.revno())))
133
197
                parent_revno, parent_revid= self.target.last_revision_info()
134
198
                if parent_revid == revision.NULL_REVISION:
168
232
    def get_format_string(self):
169
233
        return "Branch for Testing"
170
234
 
171
 
    def __init__(self):
172
 
        super(DummyForeignVcsBranchFormat, self).__init__()
173
 
        self._matchingbzrdir = DummyForeignVcsDirFormat()
 
235
    @property
 
236
    def _matchingbzrdir(self):
 
237
        return DummyForeignVcsDirFormat()
174
238
 
175
 
    def open(self, a_bzrdir, name=None, _found=False):
 
239
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
240
            found_repository=None):
176
241
        if not _found:
177
242
            raise NotImplementedError
178
243
        try:
179
244
            transport = a_bzrdir.get_branch_transport(None, name=name)
180
245
            control_files = lockable_files.LockableFiles(transport, 'lock',
181
246
                                                         lockdir.LockDir)
 
247
            if found_repository is None:
 
248
                found_repository = a_bzrdir.find_repository()
182
249
            return DummyForeignVcsBranch(_format=self,
183
250
                              _control_files=control_files,
184
251
                              a_bzrdir=a_bzrdir,
185
 
                              _repository=a_bzrdir.find_repository())
 
252
                              _repository=found_repository)
186
253
        except errors.NoSuchFile:
187
254
            raise errors.NotBranchError(path=transport.base)
188
255
 
205
272
    def get_branch_format(self):
206
273
        return DummyForeignVcsBranchFormat()
207
274
 
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()
 
275
    @property
 
276
    def repository_format(self):
 
277
        return DummyForeignVcsRepositoryFormat()
214
278
 
215
279
    def initialize_on_transport(self, transport):
216
280
        """Initialize a new bzrdir in the base directory of a Transport."""
244
308
        self._control_files = lockable_files.LockableFiles(self.transport,
245
309
            "lock", lockable_files.TransportLock)
246
310
 
247
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
 
311
    def create_workingtree(self):
 
312
        # dirstate requires a ".bzr" entry to exist
 
313
        self.root_transport.put_bytes(".bzr", "foo")
 
314
        return super(DummyForeignVcsDir, self).create_workingtree()
 
315
 
 
316
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
 
317
            possible_transports=None):
248
318
        if name is not None:
249
319
            raise errors.NoColocatedBranchSupport(self)
250
320
        return self._format.get_branch_format().open(self, _found=True)
253
323
        """Produce a metadir suitable for cloning with."""
254
324
        return bzrdir.format_registry.make_bzrdir("default")
255
325
 
 
326
    def checkout_metadir(self):
 
327
        return self.cloning_metadir()
 
328
 
256
329
    def sprout(self, url, revision_id=None, force_new_repo=False,
257
330
               recurse='down', possible_transports=None,
258
331
               accelerator_tree=None, hardlink=False, stacked=False,
266
339
 
267
340
 
268
341
def register_dummy_foreign_for_test(testcase):
269
 
    bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
 
    testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
 
                        DummyForeignVcsDirFormat)
 
342
    controldir.ControlDirFormat.register_prober(DummyForeignProber)
 
343
    testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
 
344
        DummyForeignProber)
 
345
    repository.format_registry.register(DummyForeignVcsRepositoryFormat())
 
346
    testcase.addCleanup(repository.format_registry.remove,
 
347
            DummyForeignVcsRepositoryFormat())
 
348
    branch.format_registry.register(DummyForeignVcsBranchFormat())
 
349
    testcase.addCleanup(branch.format_registry.remove,
 
350
            DummyForeignVcsBranchFormat())
272
351
    # We need to register the optimiser to make the dummy appears really
273
352
    # different from a regular bzr repository.
274
353
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
276
355
                        InterToDummyVcsBranch)
277
356
 
278
357
 
 
358
class DummyForeignProber(controldir.Prober):
 
359
 
 
360
    @classmethod
 
361
    def probe_transport(klass, transport):
 
362
        """Return the .bzrdir style format present in a directory."""
 
363
        if not transport.has('.dummy'):
 
364
            raise errors.NotBranchError(path=transport.base)
 
365
        return DummyForeignVcsDirFormat()
 
366
 
 
367
    @classmethod
 
368
    def known_formats(cls):
 
369
        return set([DummyForeignVcsDirFormat()])
 
370
 
 
371
 
279
372
class ForeignVcsRegistryTests(tests.TestCase):
280
373
    """Tests for the ForeignVcsRegistry class."""
281
374
 
293
386
        reg = foreign.ForeignVcsRegistry()
294
387
        vcs = DummyForeignVcs()
295
388
        reg.register("dummy", vcs, "Dummy VCS")
296
 
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
297
 
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
 
389
        self.assertEquals((
 
390
            ("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
 
391
            reg.parse_revision_id("dummy-v1:some-foreign-revid"))
298
392
 
299
393
 
300
394
class ForeignRevisionTests(tests.TestCase):
368
462
        source_tree = self.make_branch_and_tree("source")
369
463
        target_tree = self.make_branch_and_tree("target", 
370
464
            format=DummyForeignVcsDirFormat())
371
 
        pushresult = source_tree.branch.lossy_push(target_tree.branch)
 
465
        pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
372
466
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
373
467
        self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
374
468
        self.assertEquals({}, pushresult.revidmap)
382
476
            format=DummyForeignVcsDirFormat())
383
477
        target_tree.branch.lock_write()
384
478
        try:
385
 
            pushresult = source_tree.branch.lossy_push(target_tree.branch)
 
479
            pushresult = source_tree.branch.push(
 
480
                target_tree.branch, lossy=True)
386
481
        finally:
387
482
            target_tree.branch.unlock()
388
483
        self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)