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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 revision_id_bzr_to_foreign(self, bzr_revid):
32
return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
34
def revision_id_foreign_to_bzr(self, foreign_revid):
35
return "dummy-v1:%s-%s-%s" % foreign_revid
38
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
40
def revision_id_bzr_to_foreign(self, revid):
41
if not revid.startswith("dummy-"):
42
raise errors.InvalidRevisionId(revid, None)
43
mapping_version = revid[len("dummy-"):len("dummy-vx")]
44
mapping = self.get(mapping_version)
45
return mapping.revision_id_bzr_to_foreign(revid)
48
class DummyForeignVcs(foreign.ForeignVcs):
49
"""A dummy Foreign VCS, for use with testing.
51
It has revision ids that are a tuple with three strings.
55
self.mapping_registry = DummyForeignVcsMappingRegistry()
56
self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
59
def show_foreign_revid(self, foreign_revid):
60
return { "dummy ding": "%s/%s\\%s" % foreign_revid }
64
class ForeignVcsRegistryTests(TestCase):
66
def test_parse_revision_id_no_dash(self):
67
reg = foreign.ForeignVcsRegistry()
68
self.assertRaises(errors.InvalidRevisionId,
69
reg.parse_revision_id, "invalid")
71
def test_parse_revision_id_unknown_mapping(self):
72
reg = foreign.ForeignVcsRegistry()
73
self.assertRaises(errors.InvalidRevisionId,
74
reg.parse_revision_id, "unknown-foreignrevid")
76
def test_parse_revision_id(self):
77
reg = foreign.ForeignVcsRegistry()
78
vcs = DummyForeignVcs()
79
reg.register("dummy", vcs, "Dummy VCS")
80
self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
81
reg.parse_revision_id("dummy-v1:some-foreign-revid"))
84
class ForeignRevisionTests(TestCase):
85
"""Tests for the ForeignRevision class."""
87
def test_create(self):
88
mapp = DummyForeignVcsMapping(DummyForeignVcs())
89
rev = foreign.ForeignRevision(("a", "foreign", "revid"),
90
mapp, "roundtripped-revid")
91
self.assertEquals("", rev.inventory_sha1)
92
self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
93
self.assertEquals(mapp, rev.mapping)
96
class ShowForeignPropertiesTests(TestCase):
97
"""Tests for the show_foreign_properties() function."""
100
super(ShowForeignPropertiesTests, self).setUp()
101
self.vcs = DummyForeignVcs()
102
foreign.foreign_vcs_registry.register("dummy",
103
self.vcs, "Dummy VCS")
106
super(ShowForeignPropertiesTests, self).tearDown()
107
foreign.foreign_vcs_registry.remove("dummy")
109
def test_show_non_foreign(self):
110
"""Test use with a native (non-foreign) bzr revision."""
111
self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
113
def test_show_imported(self):
114
rev = Revision("dummy-v1:my-foreign-revid")
115
self.assertEquals({ "dummy ding": "my/foreign\\revid" },
116
foreign.show_foreign_properties(rev))
118
def test_show_direct(self):
119
rev = foreign.ForeignRevision(("some", "foreign", "revid"),
120
DummyForeignVcsMapping(self.vcs),
122
self.assertEquals({ "dummy ding": "some/foreign\\revid" },
123
foreign.show_foreign_properties(rev))