1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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
18
"""Tests for foreign VCS utility code."""
20
from bzrlib import errors, foreign
21
from bzrlib.revision import Revision
22
from bzrlib.tests import TestCase
25
class DummyForeignVcsMapping(foreign.VcsMapping):
26
"""A simple mapping for the dummy Foreign VCS, for use with testing."""
28
def __eq__(self, other):
29
return type(self) == type(other)
31
def show_foreign_revid(self, foreign_revid):
32
return { "dummy ding": "%s/%s\\%s" % foreign_revid }
34
def revision_id_bzr_to_foreign(self, bzr_revid):
35
return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
37
def revision_id_foreign_to_bzr(self, foreign_revid):
38
return "dummy-v1:%s-%s-%s" % foreign_revid
41
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
43
def revision_id_bzr_to_foreign(self, revid):
44
if not revid.startswith("dummy-"):
45
raise errors.InvalidRevisionId(revid, None)
46
mapping_version = revid[len("dummy-"):len("dummy-vx")]
47
mapping = self.get(mapping_version)
48
return mapping.revision_id_bzr_to_foreign(revid)
51
class DummyForeignVcs(foreign.ForeignVcs):
52
"""A dummy Foreign VCS, for use with testing.
54
It has revision ids that are a tuple with three strings.
58
self.mapping_registry = DummyForeignVcsMappingRegistry()
59
self.mapping_registry.register("v1", DummyForeignVcsMapping(),
63
class ForeignVcsRegistryTests(TestCase):
65
def test_parse_revision_id_no_dash(self):
66
reg = foreign.ForeignVcsRegistry()
67
self.assertRaises(errors.InvalidRevisionId,
68
reg.parse_revision_id, "invalid")
70
def test_parse_revision_id_unknown_mapping(self):
71
reg = foreign.ForeignVcsRegistry()
72
self.assertRaises(errors.InvalidRevisionId,
73
reg.parse_revision_id, "unknown-foreignrevid")
75
def test_parse_revision_id(self):
76
reg = foreign.ForeignVcsRegistry()
77
reg.register("dummy", DummyForeignVcs(), "Dummy VCS")
78
self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping()),
79
reg.parse_revision_id("dummy-v1:some-foreign-revid"))
82
class ForeignRevisionTests(TestCase):
83
"""Tests for the ForeignRevision class."""
85
def test_create(self):
86
mapp = DummyForeignVcsMapping()
87
rev = foreign.ForeignRevision(("a", "foreign", "revid"),
88
mapp, "roundtripped-revid")
89
self.assertEquals("", rev.inventory_sha1)
90
self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
91
self.assertEquals(mapp, rev.mapping)
94
class ShowForeignPropertiesTests(TestCase):
95
"""Tests for the show_foreign_properties() function."""
98
super(ShowForeignPropertiesTests, self).setUp()
99
foreign.foreign_vcs_registry.register("dummy",
100
DummyForeignVcs(), "Dummy VCS")
103
super(ShowForeignPropertiesTests, self).tearDown()
104
foreign.foreign_vcs_registry.remove("dummy")
106
def test_show_non_foreign(self):
107
"""Test use with a native (non-foreign) bzr revision."""
108
self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
110
def test_show_imported(self):
111
rev = Revision("dummy-v1:my-foreign-revid")
112
self.assertEquals({ "dummy ding": "my/foreign\\revid" },
113
foreign.show_foreign_properties(rev))
115
def test_show_direct(self):
116
rev = foreign.ForeignRevision(("some", "foreign", "revid"),
117
DummyForeignVcsMapping(),
119
self.assertEquals({ "dummy ding": "some/foreign\\revid" },
120
foreign.show_foreign_properties(rev))