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