/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: Ian Clatworthy
  • Date: 2009-05-18 07:48:02 UTC
  • mto: (4456.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4457.
  • Revision ID: ian.clatworthy@canonical.com-20090518074802-dbx8ats78vl134y6
refactor ls command to use new APIs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 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
20
20
 
21
21
from bzrlib import (
22
22
    branch,
23
 
    bzrdir,
24
23
    errors,
25
24
    foreign,
26
25
    lockable_files,
27
26
    lockdir,
28
 
    revision,
29
 
    tests,
30
27
    trace,
31
28
    )
 
29
from bzrlib.bzrdir import (
 
30
    BzrDir,
 
31
    BzrDirFormat,
 
32
    BzrDirMeta1,
 
33
    BzrDirMetaFormat1,
 
34
    format_registry,
 
35
    )
 
36
from bzrlib.inventory import Inventory
 
37
from bzrlib.revision import Revision
 
38
from bzrlib.tests import (
 
39
    TestCase,
 
40
    TestCaseWithTransport,
 
41
    )
32
42
 
33
43
# This is the dummy foreign revision control system, used 
34
44
# mainly here in the testsuite to test the foreign VCS infrastructure.
74
84
        self.mapping_registry = DummyForeignVcsMappingRegistry()
75
85
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
76
86
                                       "Version 1")
77
 
        self.abbreviation = "dummy"
78
87
 
79
88
    def show_foreign_revid(self, foreign_revid):
80
89
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
81
90
 
82
 
    def serialize_foreign_revid(self, foreign_revid):
83
 
        return "%s|%s|%s" % foreign_revid
84
 
 
85
91
 
86
92
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
87
93
    """A Dummy VCS Branch."""
90
96
        self._format = _format
91
97
        self._base = a_bzrdir.transport.base
92
98
        self._ignore_fallbacks = False
93
 
        self.bzrdir = a_bzrdir
94
99
        foreign.ForeignBranch.__init__(self, 
95
100
            DummyForeignVcsMapping(DummyForeignVcs()))
96
101
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir, 
104
109
    def is_compatible(source, target):
105
110
        return isinstance(target, DummyForeignVcsBranch)
106
111
 
107
 
    def push(self, overwrite=False, stop_revision=None):
108
 
        raise errors.NoRoundtrippingSupport(self.source, self.target)
109
 
 
110
112
    def lossy_push(self, stop_revision=None):
111
 
        result = branch.BranchPushResult()
112
 
        result.source_branch = self.source
113
 
        result.target_branch = self.target
114
 
        result.old_revno, result.old_revid = self.target.last_revision_info()
115
113
        self.source.lock_read()
116
114
        try:
117
115
            # This just handles simple cases, but that's good enough for tests
131
129
                    (str(rev.timestamp), str(rev.timezone), 
132
130
                        str(self.target.revno())))
133
131
                parent_revno, parent_revid= self.target.last_revision_info()
134
 
                if parent_revid == revision.NULL_REVISION:
135
 
                    parent_revids = []
136
 
                else:
137
 
                    parent_revids = [parent_revid]
138
 
                builder = self.target.get_commit_builder(parent_revids, 
 
132
                builder = self.target.get_commit_builder([parent_revid], 
139
133
                        self.target.get_config(), rev.timestamp,
140
134
                        rev.timezone, rev.committer, rev.properties,
141
135
                        new_revid)
144
138
                        new_ie = ie.copy()
145
139
                        new_ie.revision = None
146
140
                        builder.record_entry_contents(new_ie, 
147
 
                            [self.target.repository.revision_tree(parent_revid).inventory],
 
141
                            [self.target.repository.get_inventory(parent_revid)],
148
142
                            path, tree, 
149
143
                            (ie.kind, ie.text_size, ie.executable, ie.text_sha1))
150
144
                    builder.finish_inventory()
158
152
                    revid, revidmap[revid])
159
153
        finally:
160
154
            self.source.unlock()
161
 
        result.new_revno, result.new_revid = self.target.last_revision_info()
162
 
        result.revidmap = revidmap
163
 
        return result
 
155
        return revidmap
164
156
 
165
157
 
166
158
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
172
164
        super(DummyForeignVcsBranchFormat, self).__init__()
173
165
        self._matchingbzrdir = DummyForeignVcsDirFormat()
174
166
 
175
 
    def open(self, a_bzrdir, name=None, _found=False):
 
167
    def open(self, a_bzrdir, _found=False):
176
168
        if not _found:
177
169
            raise NotImplementedError
178
170
        try:
179
 
            transport = a_bzrdir.get_branch_transport(None, name=name)
 
171
            transport = a_bzrdir.get_branch_transport(None)
180
172
            control_files = lockable_files.LockableFiles(transport, 'lock',
181
173
                                                         lockdir.LockDir)
182
174
            return DummyForeignVcsBranch(_format=self,
187
179
            raise errors.NotBranchError(path=transport.base)
188
180
 
189
181
 
190
 
class DummyForeignVcsDirFormat(bzrdir.BzrDirMetaFormat1):
 
182
class DummyForeignVcsDirFormat(BzrDirMetaFormat1):
191
183
    """BzrDirFormat for the dummy foreign VCS."""
192
184
 
193
185
    @classmethod
234
226
        return DummyForeignVcsDir(transport, self)
235
227
 
236
228
 
237
 
class DummyForeignVcsDir(bzrdir.BzrDirMeta1):
 
229
class DummyForeignVcsDir(BzrDirMeta1):
238
230
 
239
231
    def __init__(self, _transport, _format):
240
232
        self._format = _format
244
236
        self._control_files = lockable_files.LockableFiles(self.transport,
245
237
            "lock", lockable_files.TransportLock)
246
238
 
247
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
248
 
        if name is not None:
249
 
            raise errors.NoColocatedBranchSupport(self)
 
239
    def open_branch(self, ignore_fallbacks=True):
250
240
        return self._format.get_branch_format().open(self, _found=True)
251
241
 
252
242
    def cloning_metadir(self, stacked=False):
253
243
        """Produce a metadir suitable for cloning with."""
254
 
        return bzrdir.format_registry.make_bzrdir("default")
 
244
        return format_registry.make_bzrdir("default")
255
245
 
256
246
    def sprout(self, url, revision_id=None, force_new_repo=False,
257
247
               recurse='down', possible_transports=None,
265
255
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
266
256
 
267
257
 
268
 
def register_dummy_foreign_for_test(testcase):
269
 
    bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
 
    testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
 
                        DummyForeignVcsDirFormat)
272
 
    # We need to register the optimiser to make the dummy appears really
273
 
    # different from a regular bzr repository.
274
 
    branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
275
 
    testcase.addCleanup(branch.InterBranch.unregister_optimiser,
276
 
                        InterToDummyVcsBranch)
277
 
 
278
 
 
279
 
class ForeignVcsRegistryTests(tests.TestCase):
 
258
class ForeignVcsRegistryTests(TestCase):
280
259
    """Tests for the ForeignVcsRegistry class."""
281
260
 
282
261
    def test_parse_revision_id_no_dash(self):
297
276
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
298
277
 
299
278
 
300
 
class ForeignRevisionTests(tests.TestCase):
 
279
class ForeignRevisionTests(TestCase):
301
280
    """Tests for the ForeignRevision class."""
302
281
 
303
282
    def test_create(self):
309
288
        self.assertEquals(mapp, rev.mapping)
310
289
 
311
290
 
312
 
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
 
291
class ShowForeignPropertiesTests(TestCase):
 
292
    """Tests for the show_foreign_properties() function."""
 
293
 
 
294
    def setUp(self):
 
295
        super(ShowForeignPropertiesTests, self).setUp()
 
296
        self.vcs = DummyForeignVcs()
 
297
        foreign.foreign_vcs_registry.register("dummy",
 
298
            self.vcs, "Dummy VCS")
 
299
 
 
300
    def tearDown(self):
 
301
        super(ShowForeignPropertiesTests, self).tearDown()
 
302
        foreign.foreign_vcs_registry.remove("dummy")
 
303
 
 
304
    def test_show_non_foreign(self):
 
305
        """Test use with a native (non-foreign) bzr revision."""
 
306
        self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
 
307
 
 
308
    def test_show_imported(self):
 
309
        rev = Revision("dummy-v1:my-foreign-revid")
 
310
        self.assertEquals({ "dummy ding": "my/foreign\\revid" },
 
311
                          foreign.show_foreign_properties(rev))
 
312
 
 
313
    def test_show_direct(self):
 
314
        rev = foreign.ForeignRevision(("some", "foreign", "revid"),
 
315
                                      DummyForeignVcsMapping(self.vcs),
 
316
                                      "roundtrip-revid")
 
317
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
 
318
                          foreign.show_foreign_properties(rev))
 
319
 
 
320
 
 
321
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
313
322
    """Tests for update_workingtree_fileids()."""
314
323
 
315
324
    def test_update_workingtree(self):
317
326
        self.build_tree_contents([('br1/bla', 'original contents\n')])
318
327
        wt.add('bla', 'bla-a')
319
328
        wt.commit('bla-a')
320
 
        root_id = wt.get_root_id()
321
329
        target = wt.bzrdir.sprout('br2').open_workingtree()
322
330
        target.unversion(['bla-a'])
323
331
        target.add('bla', 'bla-b')
328
336
        foreign.update_workingtree_fileids(wt, target_basis)
329
337
        wt.lock_read()
330
338
        try:
331
 
            self.assertEquals(set([root_id, "bla-b"]), set(wt.inventory))
 
339
            self.assertEquals(["TREE_ROOT", "bla-b"], list(wt.inventory))
332
340
        finally:
333
341
            wt.unlock()
334
342
 
335
343
 
336
 
class DummyForeignVcsTests(tests.TestCaseWithTransport):
 
344
class DummyForeignVcsTests(TestCaseWithTransport):
337
345
    """Very basic test for DummyForeignVcs."""
338
346
 
339
347
    def setUp(self):
 
348
        BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
 
349
        branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
 
350
        self.addCleanup(self.unregister)
340
351
        super(DummyForeignVcsTests, self).setUp()
341
 
        register_dummy_foreign_for_test(self)
 
352
 
 
353
    def unregister(self):
 
354
        try:
 
355
            BzrDirFormat.unregister_control_format(DummyForeignVcsDirFormat)
 
356
        except ValueError:
 
357
            pass
 
358
        branch.InterBranch.unregister_optimiser(InterToDummyVcsBranch)
342
359
 
343
360
    def test_create(self):
344
361
        """Test we can create dummies."""
345
362
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
346
 
        dir = bzrdir.BzrDir.open("d")
 
363
        dir = BzrDir.open("d")
347
364
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
348
365
        dir.open_repository()
349
366
        dir.open_branch()
352
369
    def test_sprout(self):
353
370
        """Test we can clone dummies and that the format is not preserved."""
354
371
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
355
 
        dir = bzrdir.BzrDir.open("d")
 
372
        dir = BzrDir.open("d")
356
373
        newdir = dir.sprout("e")
357
 
        self.assertNotEquals("A Dummy VCS Dir",
358
 
                             newdir._format.get_format_string())
359
 
 
360
 
    def test_push_not_supported(self):
361
 
        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)
366
 
 
367
 
    def test_lossy_push_empty(self):
368
 
        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)
375
 
 
376
 
    def test_lossy_push_simple(self):
377
 
        source_tree = self.make_branch_and_tree("source")
378
 
        self.build_tree(['source/a', 'source/b'])
379
 
        source_tree.add(['a', 'b'])
380
 
        revid1 = source_tree.commit("msg")
381
 
        target_tree = self.make_branch_and_tree("target", 
382
 
            format=DummyForeignVcsDirFormat())
383
 
        target_tree.branch.lock_write()
384
 
        try:
385
 
            pushresult = source_tree.branch.lossy_push(target_tree.branch)
386
 
        finally:
387
 
            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)
 
374
        self.assertNotEquals("A Dummy VCS Dir", newdir._format.get_format_string())