/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
1
# Copyright (C) 2008 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
16
17
18
"""Tests for foreign VCS utility code."""
19
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
20
from bzrlib import (
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
21
    branch,
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
22
    errors,
23
    foreign,
24
    lockable_files,
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
25
    lockdir,
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
26
    )
27
from bzrlib.bzrdir import (
28
    BzrDir,
29
    BzrDirFormat,
30
    BzrDirMeta1,
31
    BzrDirMetaFormat1,
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
32
    format_registry,
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
33
    )
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
34
from bzrlib.inventory import Inventory
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
35
from bzrlib.revision import Revision
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
36
from bzrlib.tests import TestCase, TestCaseWithTransport
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
37
3920.2.7 by Jelmer Vernooij
Add comments about dummy vcs.
38
# This is the dummy foreign revision control system, used 
39
# mainly here in the testsuite to test the foreign VCS infrastructure.
40
# It is basically standard Bazaar with some minor modifications to 
41
# make it "foreign". 
42
# 
43
# It has the following differences to "regular" Bazaar:
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
44
# - The control directory is named ".dummy", not ".bzr".
3920.2.7 by Jelmer Vernooij
Add comments about dummy vcs.
45
# - The revision ids are tuples, not strings.
3920.2.20 by Jelmer Vernooij
Fix dpush tests.
46
# - Doesn't support more than one parent natively
3920.2.7 by Jelmer Vernooij
Add comments about dummy vcs.
47
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
48
49
class DummyForeignVcsMapping(foreign.VcsMapping):
50
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
51
52
    def __eq__(self, other):
53
        return type(self) == type(other)
54
55
    def revision_id_bzr_to_foreign(self, bzr_revid):
56
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
57
58
    def revision_id_foreign_to_bzr(self, foreign_revid):
59
        return "dummy-v1:%s-%s-%s" % foreign_revid
60
61
62
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
63
64
    def revision_id_bzr_to_foreign(self, revid):
65
        if not revid.startswith("dummy-"):
66
            raise errors.InvalidRevisionId(revid, None)
67
        mapping_version = revid[len("dummy-"):len("dummy-vx")]
68
        mapping = self.get(mapping_version)
69
        return mapping.revision_id_bzr_to_foreign(revid)
70
71
72
class DummyForeignVcs(foreign.ForeignVcs):
73
    """A dummy Foreign VCS, for use with testing.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
74
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
75
    It has revision ids that are a tuple with three strings.
76
    """
77
78
    def __init__(self):
79
        self.mapping_registry = DummyForeignVcsMappingRegistry()
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
80
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
81
                                       "Version 1")
82
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
83
    def show_foreign_revid(self, foreign_revid):
84
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
85
86
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
87
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
88
    """A Dummy VCS Branch."""
89
90
    def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
91
        self._format = _format
92
        self._base = a_bzrdir.transport.base
3920.2.29 by Jelmer Vernooij
Fix dpush tests.
93
        self._ignore_fallbacks = False
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
94
        foreign.ForeignBranch.__init__(self, DummyForeignVcsMapping(DummyForeignVcs()))
95
        branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir, *args, **kwargs)
96
97
    def dpull(self, source, stop_revision=None):
3920.2.20 by Jelmer Vernooij
Fix dpush tests.
98
        # This just handles simple cases, but that's good enough for tests
99
        my_history = self.revision_history()
100
        their_history = source.revision_history()
101
        if their_history[:min(len(my_history), len(their_history))] != my_history:
102
            raise errors.DivergedBranches(self, source)
103
        todo = their_history[len(my_history):]
104
        revidmap = {}
105
        for revid in todo:
106
            rev = source.repository.get_revision(revid)
107
            tree = source.repository.revision_tree(revid)
108
            builder = self.get_commit_builder([self.last_revision()], 
109
                    self.get_config(), rev.timestamp,
110
                    rev.timezone, rev.committer, rev.properties)
111
            for path, ie in tree.inventory.iter_entries():
112
                builder.record_entry_contents(ie.copy(), 
113
                    [self.repository.get_inventory(self.last_revision())],
114
                    path, tree, None)
115
            builder.finish_inventory()
116
            revidmap[revid] = builder.commit(rev.message)
117
        return revidmap
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
118
119
120
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
121
122
    def get_format_string(self):
123
        return "Branch for Testing"
124
125
    def __init__(self):
126
        super(DummyForeignVcsBranchFormat, self).__init__()
127
        self._matchingbzrdir = DummyForeignVcsDirFormat()
128
129
    def open(self, a_bzrdir, _found=False):
130
        if not _found:
131
            raise NotImplementedError
132
        try:
133
            transport = a_bzrdir.get_branch_transport(None)
134
            control_files = lockable_files.LockableFiles(transport, 'lock',
135
                                                         lockdir.LockDir)
136
            return DummyForeignVcsBranch(_format=self,
137
                              _control_files=control_files,
138
                              a_bzrdir=a_bzrdir,
139
                              _repository=a_bzrdir.find_repository())
140
        except errors.NoSuchFile:
141
            raise errors.NotBranchError(path=transport.base)
142
143
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
144
class DummyForeignVcsDirFormat(BzrDirMetaFormat1):
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
145
    """BzrDirFormat for the dummy foreign VCS."""
146
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
147
    @classmethod
148
    def get_format_string(cls):
149
        return "A Dummy VCS Dir"
150
151
    @classmethod
152
    def get_format_description(cls):
153
        return "A Dummy VCS Dir"
154
155
    @classmethod
156
    def is_supported(cls):
157
        return True
158
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
159
    def get_branch_format(self):
160
        return DummyForeignVcsBranchFormat()
161
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
162
    @classmethod
163
    def probe_transport(klass, transport):
164
        """Return the .bzrdir style format present in a directory."""
165
        if not transport.has('.dummy'):
166
            raise errors.NotBranchError(path=transport.base)
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
167
        return klass()
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
168
169
    def initialize_on_transport(self, transport):
170
        """Initialize a new bzrdir in the base directory of a Transport."""
171
        # Since we don't have a .bzr directory, inherit the
172
        # mode from the root directory
173
        temp_control = lockable_files.LockableFiles(transport,
174
                            '', lockable_files.TransportLock)
175
        temp_control._transport.mkdir('.dummy',
176
                                      # FIXME: RBC 20060121 don't peek under
177
                                      # the covers
178
                                      mode=temp_control._dir_mode)
179
        del temp_control
180
        bzrdir_transport = transport.clone('.dummy')
181
        # NB: no need to escape relative paths that are url safe.
182
        control_files = lockable_files.LockableFiles(bzrdir_transport,
183
            self._lock_file_name, self._lock_class)
184
        control_files.create_lock()
185
        return self.open(transport, _found=True)
186
187
    def _open(self, transport):
188
        return DummyForeignVcsDir(transport, self)
189
190
191
class DummyForeignVcsDir(BzrDirMeta1):
192
193
    def __init__(self, _transport, _format):
194
        self._format = _format
195
        self.transport = _transport.clone('.dummy')
196
        self.root_transport = _transport
197
        self._mode_check_done = False
3920.2.19 by Jelmer Vernooij
Get everything except dpull itself working.
198
        self._control_files = lockable_files.LockableFiles(self.transport,
199
            "lock", lockable_files.TransportLock)
200
201
    def open_branch(self):
202
        return self._format.get_branch_format().open(self, _found=True)
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
203
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
204
    def cloning_metadir(self, stacked=False):
205
        """Produce a metadir suitable for cloning with."""
206
        return format_registry.make_bzrdir("default")
207
208
    def sprout(self, url, revision_id=None, force_new_repo=False,
209
               recurse='down', possible_transports=None,
210
               accelerator_tree=None, hardlink=False, stacked=False,
211
               source_branch=None):
212
        # dirstate doesn't cope with accelerator_trees well 
213
        # that have a different control dir
214
        return super(DummyForeignVcsDir, self).sprout(url=url, 
215
                revision_id=revision_id, force_new_repo=force_new_repo, 
216
                recurse=recurse, possible_transports=possible_transports, 
217
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
218
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
219
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
220
class ForeignVcsRegistryTests(TestCase):
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
221
    """Tests for the ForeignVcsRegistry class."""
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
222
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
223
    def test_parse_revision_id_no_dash(self):
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
224
        reg = foreign.ForeignVcsRegistry()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
225
        self.assertRaises(errors.InvalidRevisionId,
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
226
                          reg.parse_revision_id, "invalid")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
227
228
    def test_parse_revision_id_unknown_mapping(self):
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
229
        reg = foreign.ForeignVcsRegistry()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
230
        self.assertRaises(errors.InvalidRevisionId,
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
231
                          reg.parse_revision_id, "unknown-foreignrevid")
232
233
    def test_parse_revision_id(self):
234
        reg = foreign.ForeignVcsRegistry()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
235
        vcs = DummyForeignVcs()
236
        reg.register("dummy", vcs, "Dummy VCS")
237
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
238
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
239
240
241
class ForeignRevisionTests(TestCase):
242
    """Tests for the ForeignRevision class."""
243
244
    def test_create(self):
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
245
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
246
        rev = foreign.ForeignRevision(("a", "foreign", "revid"),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
247
                                      mapp, "roundtripped-revid")
248
        self.assertEquals("", rev.inventory_sha1)
249
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
250
        self.assertEquals(mapp, rev.mapping)
251
252
253
class ShowForeignPropertiesTests(TestCase):
254
    """Tests for the show_foreign_properties() function."""
255
256
    def setUp(self):
257
        super(ShowForeignPropertiesTests, self).setUp()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
258
        self.vcs = DummyForeignVcs()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
259
        foreign.foreign_vcs_registry.register("dummy",
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
260
            self.vcs, "Dummy VCS")
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
261
262
    def tearDown(self):
263
        super(ShowForeignPropertiesTests, self).tearDown()
264
        foreign.foreign_vcs_registry.remove("dummy")
265
266
    def test_show_non_foreign(self):
267
        """Test use with a native (non-foreign) bzr revision."""
268
        self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
269
270
    def test_show_imported(self):
271
        rev = Revision("dummy-v1:my-foreign-revid")
272
        self.assertEquals({ "dummy ding": "my/foreign\\revid" },
273
                          foreign.show_foreign_properties(rev))
274
275
    def test_show_direct(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
276
        rev = foreign.ForeignRevision(("some", "foreign", "revid"),
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
277
                                      DummyForeignVcsMapping(self.vcs),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
278
                                      "roundtrip-revid")
279
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
280
                          foreign.show_foreign_properties(rev))
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
281
282
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
283
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
3920.2.30 by Jelmer Vernooij
Review from John.
284
    """Tests for _determine_fileid_renames()."""
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
285
286
    def test_det_renames_same(self):
287
        a = Inventory()
288
        a.add_path("bla", "directory", "bla-a")
289
        b = Inventory()
290
        b.add_path("bla", "directory", "bla-a")
3920.2.29 by Jelmer Vernooij
Fix dpush tests.
291
        self.assertEquals({
292
            '': ('TREE_ROOT', 'TREE_ROOT'), 
293
            'bla': ('bla-a', 'bla-a')},
3920.2.30 by Jelmer Vernooij
Review from John.
294
            foreign._determine_fileid_renames(a, b))
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
295
296
    def test_det_renames_simple(self):
297
        a = Inventory()
298
        a.add_path("bla", "directory", "bla-a")
299
        b = Inventory()
300
        b.add_path("bla", "directory", "bla-b")
3920.2.29 by Jelmer Vernooij
Fix dpush tests.
301
        self.assertEquals({
302
            '': ('TREE_ROOT', 'TREE_ROOT'), 
303
            'bla': ('bla-a', 'bla-b'),
3920.2.30 by Jelmer Vernooij
Review from John.
304
            }, foreign._determine_fileid_renames(a, b))
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
305
306
    def test_det_renames_root(self):
307
        a = Inventory()
308
        a.add_path("", "directory", "bla-a")
309
        b = Inventory()
310
        b.add_path("", "directory", "bla-b")
311
        self.assertEquals(
312
                {"": ("bla-a", "bla-b")},
3920.2.30 by Jelmer Vernooij
Review from John.
313
                foreign._determine_fileid_renames(a, b))
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
314
315
    def test_update_workinginv(self):
316
        a = Inventory()
317
        a.add_path("bla", "directory", "bla-a")
318
        b = Inventory()
319
        b.add_path("bla", "directory", "bla-b")
320
        wt = self.make_branch_and_tree('br1')
321
        self.build_tree_contents([('br1/bla', 'original contents\n')])
322
        wt.add('bla', 'bla-a')
323
        foreign.update_workinginv_fileids(wt, a, b)
324
        wt.lock_read()
325
        try:
326
            self.assertEquals(["TREE_ROOT", "bla-b"], list(wt.inventory))
327
        finally:
328
            wt.unlock()
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
329
330
331
class DummyForeignVcsTests(TestCaseWithTransport):
332
    """Very basic test for DummyForeignVcs."""
333
334
    def setUp(self):
335
        BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
336
        self.addCleanup(self.unregister)
337
        super(DummyForeignVcsTests, self).setUp()
338
339
    def unregister(self):
340
        try:
341
            BzrDirFormat.unregister_control_format(DummyForeignVcsDirFormat)
342
        except ValueError:
343
            pass
344
345
    def test_create(self):
346
        """Test we can create dummies."""
347
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
348
        dir = BzrDir.open("d")
349
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
350
        dir.open_repository()
351
        dir.open_branch()
352
        dir.open_workingtree()
353
354
    def test_sprout(self):
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
355
        """Test we can clone dummies and that the format is not preserved."""
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
356
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
357
        dir = BzrDir.open("d")
358
        newdir = dir.sprout("e")
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
359
        self.assertNotEquals("A Dummy VCS Dir", newdir._format.get_format_string())