/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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Tests for foreign VCS utility code."""
19
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
20
from bzrlib import (
21
    errors,
22
    foreign,
23
    lockable_files,
24
    )
25
from bzrlib.bzrdir import (
26
    BzrDir,
27
    BzrDirFormat,
28
    BzrDirMeta1,
29
    BzrDirMetaFormat1,
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
30
    format_registry,
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
31
    )
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
32
from bzrlib.inventory import Inventory
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
33
from bzrlib.revision import Revision
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
34
from bzrlib.tests import TestCase, TestCaseWithTransport
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
35
3920.2.7 by Jelmer Vernooij
Add comments about dummy vcs.
36
# This is the dummy foreign revision control system, used 
37
# mainly here in the testsuite to test the foreign VCS infrastructure.
38
# It is basically standard Bazaar with some minor modifications to 
39
# make it "foreign". 
40
# 
41
# It has the following differences to "regular" Bazaar:
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
42
# - The control directory is named ".dummy", not ".bzr".
3920.2.7 by Jelmer Vernooij
Add comments about dummy vcs.
43
# - The revision ids are tuples, not strings.
44
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
45
46
class DummyForeignVcsMapping(foreign.VcsMapping):
47
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
48
49
    def __eq__(self, other):
50
        return type(self) == type(other)
51
52
    def revision_id_bzr_to_foreign(self, bzr_revid):
53
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
54
55
    def revision_id_foreign_to_bzr(self, foreign_revid):
56
        return "dummy-v1:%s-%s-%s" % foreign_revid
57
58
59
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
60
61
    def revision_id_bzr_to_foreign(self, revid):
62
        if not revid.startswith("dummy-"):
63
            raise errors.InvalidRevisionId(revid, None)
64
        mapping_version = revid[len("dummy-"):len("dummy-vx")]
65
        mapping = self.get(mapping_version)
66
        return mapping.revision_id_bzr_to_foreign(revid)
67
68
69
class DummyForeignVcs(foreign.ForeignVcs):
70
    """A dummy Foreign VCS, for use with testing.
71
    
72
    It has revision ids that are a tuple with three strings.
73
    """
74
75
    def __init__(self):
76
        self.mapping_registry = DummyForeignVcsMappingRegistry()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
77
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self), 
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
78
                                       "Version 1")
79
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
80
    def show_foreign_revid(self, foreign_revid):
81
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
82
83
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
84
class DummyForeignVcsDirFormat(BzrDirMetaFormat1):
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
85
    """BzrDirFormat for the dummy foreign VCS."""
86
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
87
    @classmethod
88
    def get_format_string(cls):
89
        return "A Dummy VCS Dir"
90
91
    @classmethod
92
    def get_format_description(cls):
93
        return "A Dummy VCS Dir"
94
95
    @classmethod
96
    def is_supported(cls):
97
        return True
98
99
    @classmethod
100
    def probe_transport(klass, transport):
101
        """Return the .bzrdir style format present in a directory."""
102
        if not transport.has('.dummy'):
103
            raise errors.NotBranchError(path=transport.base)
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
104
        return klass()
3920.2.15 by Jelmer Vernooij
Add a DummyForeignVcsDir class.
105
106
    def initialize_on_transport(self, transport):
107
        """Initialize a new bzrdir in the base directory of a Transport."""
108
        # Since we don't have a .bzr directory, inherit the
109
        # mode from the root directory
110
        temp_control = lockable_files.LockableFiles(transport,
111
                            '', lockable_files.TransportLock)
112
        temp_control._transport.mkdir('.dummy',
113
                                      # FIXME: RBC 20060121 don't peek under
114
                                      # the covers
115
                                      mode=temp_control._dir_mode)
116
        del temp_control
117
        bzrdir_transport = transport.clone('.dummy')
118
        # NB: no need to escape relative paths that are url safe.
119
        control_files = lockable_files.LockableFiles(bzrdir_transport,
120
            self._lock_file_name, self._lock_class)
121
        control_files.create_lock()
122
        return self.open(transport, _found=True)
123
124
    def _open(self, transport):
125
        return DummyForeignVcsDir(transport, self)
126
127
128
class DummyForeignVcsDir(BzrDirMeta1):
129
130
    def __init__(self, _transport, _format):
131
        self._format = _format
132
        self.transport = _transport.clone('.dummy')
133
        self.root_transport = _transport
134
        self._mode_check_done = False
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
135
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
136
    def cloning_metadir(self, stacked=False):
137
        """Produce a metadir suitable for cloning with."""
138
        return format_registry.make_bzrdir("default")
139
140
    def sprout(self, url, revision_id=None, force_new_repo=False,
141
               recurse='down', possible_transports=None,
142
               accelerator_tree=None, hardlink=False, stacked=False,
143
               source_branch=None):
144
        # dirstate doesn't cope with accelerator_trees well 
145
        # that have a different control dir
146
        return super(DummyForeignVcsDir, self).sprout(url=url, 
147
                revision_id=revision_id, force_new_repo=force_new_repo, 
148
                recurse=recurse, possible_transports=possible_transports, 
149
                hardlink=hardlink, stacked=stacked, source_branch=source_branch)
150
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
151
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
152
class ForeignVcsRegistryTests(TestCase):
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
153
    """Tests for the ForeignVcsRegistry class."""
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
154
155
    def test_parse_revision_id_no_dash(self):       
156
        reg = foreign.ForeignVcsRegistry()
157
        self.assertRaises(errors.InvalidRevisionId, 
158
                          reg.parse_revision_id, "invalid")
159
        
160
    def test_parse_revision_id_unknown_mapping(self):       
161
        reg = foreign.ForeignVcsRegistry()
162
        self.assertRaises(errors.InvalidRevisionId, 
163
                          reg.parse_revision_id, "unknown-foreignrevid")
164
165
    def test_parse_revision_id(self):
166
        reg = foreign.ForeignVcsRegistry()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
167
        vcs = DummyForeignVcs()
168
        reg.register("dummy", vcs, "Dummy VCS")
169
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
170
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
171
172
173
class ForeignRevisionTests(TestCase):
174
    """Tests for the ForeignRevision class."""
175
176
    def test_create(self):
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
177
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
178
        rev = foreign.ForeignRevision(("a", "foreign", "revid"), 
179
                                      mapp, "roundtripped-revid")
180
        self.assertEquals("", rev.inventory_sha1)
181
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
182
        self.assertEquals(mapp, rev.mapping)
183
184
185
class ShowForeignPropertiesTests(TestCase):
186
    """Tests for the show_foreign_properties() function."""
187
188
    def setUp(self):
189
        super(ShowForeignPropertiesTests, self).setUp()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
190
        self.vcs = DummyForeignVcs()
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
191
        foreign.foreign_vcs_registry.register("dummy", 
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
192
            self.vcs, "Dummy VCS")
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
193
194
    def tearDown(self):
195
        super(ShowForeignPropertiesTests, self).tearDown()
196
        foreign.foreign_vcs_registry.remove("dummy")
197
198
    def test_show_non_foreign(self):
199
        """Test use with a native (non-foreign) bzr revision."""
200
        self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
201
202
    def test_show_imported(self):
203
        rev = Revision("dummy-v1:my-foreign-revid")
204
        self.assertEquals({ "dummy ding": "my/foreign\\revid" },
205
                          foreign.show_foreign_properties(rev))
206
207
    def test_show_direct(self):
208
        rev = foreign.ForeignRevision(("some", "foreign", "revid"), 
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
209
                                      DummyForeignVcsMapping(self.vcs), 
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
210
                                      "roundtrip-revid")
211
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
212
                          foreign.show_foreign_properties(rev))
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
213
214
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
215
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
216
    """Tests for determine_fileid_renames()."""
217
218
    def test_det_renames_same(self):
219
        a = Inventory()
220
        a.add_path("bla", "directory", "bla-a")
221
        b = Inventory()
222
        b.add_path("bla", "directory", "bla-a")
223
        self.assertEquals( {}, foreign.determine_fileid_renames(a, b))
224
225
    def test_det_renames_simple(self):
226
        a = Inventory()
227
        a.add_path("bla", "directory", "bla-a")
228
        b = Inventory()
229
        b.add_path("bla", "directory", "bla-b")
230
        self.assertEquals(
231
                {"bla": ("bla-a", "bla-b")},
232
                foreign.determine_fileid_renames(a, b))
233
234
    def test_det_renames_root(self):
235
        a = Inventory()
236
        a.add_path("", "directory", "bla-a")
237
        b = Inventory()
238
        b.add_path("", "directory", "bla-b")
239
        self.assertEquals(
240
                {"": ("bla-a", "bla-b")},
241
                foreign.determine_fileid_renames(a, b))
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
242
243
    def test_update_workinginv(self):
244
        a = Inventory()
245
        a.add_path("bla", "directory", "bla-a")
246
        b = Inventory()
247
        b.add_path("bla", "directory", "bla-b")
248
        wt = self.make_branch_and_tree('br1')
249
        self.build_tree_contents([('br1/bla', 'original contents\n')])
250
        wt.add('bla', 'bla-a')
251
        foreign.update_workinginv_fileids(wt, a, b)
252
        wt.lock_read()
253
        try:
254
            self.assertEquals(["TREE_ROOT", "bla-b"], list(wt.inventory))
255
        finally:
256
            wt.unlock()
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
257
258
259
class DummyForeignVcsTests(TestCaseWithTransport):
260
    """Very basic test for DummyForeignVcs."""
261
262
    def setUp(self):
263
        BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
264
        self.addCleanup(self.unregister)
265
        super(DummyForeignVcsTests, self).setUp()
266
267
    def unregister(self):
268
        try:
269
            BzrDirFormat.unregister_control_format(DummyForeignVcsDirFormat)
270
        except ValueError:
271
            pass
272
273
    def test_create(self):
274
        """Test we can create dummies."""
275
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
276
        dir = BzrDir.open("d")
277
        self.assertEquals("A Dummy VCS Dir", dir._format.get_format_string())
278
        dir.open_repository()
279
        dir.open_branch()
280
        dir.open_workingtree()
281
282
    def test_sprout(self):
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
283
        """Test we can clone dummies and that the format is not preserved."""
3920.2.16 by Jelmer Vernooij
Add tests for DummyForeignVcs.
284
        self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
285
        dir = BzrDir.open("d")
286
        newdir = dir.sprout("e")
3920.2.17 by Jelmer Vernooij
Override BzrDir.sprout() to avoid accelerator_tree's from being used.
287
        self.assertNotEquals("A Dummy VCS Dir", newdir._format.get_format_string())