/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
20
from bzrlib import errors, foreign
21
from bzrlib.revision import Revision
22
from bzrlib.tests import TestCase
23
24
25
class DummyForeignVcsMapping(foreign.VcsMapping):
26
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
27
28
    def __eq__(self, other):
29
        return type(self) == type(other)
30
31
    def revision_id_bzr_to_foreign(self, bzr_revid):
32
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
33
34
    def revision_id_foreign_to_bzr(self, foreign_revid):
35
        return "dummy-v1:%s-%s-%s" % foreign_revid
36
37
38
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
39
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)
46
47
48
class DummyForeignVcs(foreign.ForeignVcs):
49
    """A dummy Foreign VCS, for use with testing.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
50
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
51
    It has revision ids that are a tuple with three strings.
52
    """
53
54
    def __init__(self):
55
        self.mapping_registry = DummyForeignVcsMappingRegistry()
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
56
        self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
57
                                       "Version 1")
58
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
59
    def show_foreign_revid(self, foreign_revid):
60
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
61
62
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
63
64
class ForeignVcsRegistryTests(TestCase):
65
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
66
    def test_parse_revision_id_no_dash(self):
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
67
        reg = foreign.ForeignVcsRegistry()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
68
        self.assertRaises(errors.InvalidRevisionId,
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
69
                          reg.parse_revision_id, "invalid")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
70
71
    def test_parse_revision_id_unknown_mapping(self):
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
72
        reg = foreign.ForeignVcsRegistry()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
73
        self.assertRaises(errors.InvalidRevisionId,
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
74
                          reg.parse_revision_id, "unknown-foreignrevid")
75
76
    def test_parse_revision_id(self):
77
        reg = foreign.ForeignVcsRegistry()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
78
        vcs = DummyForeignVcs()
79
        reg.register("dummy", vcs, "Dummy VCS")
80
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping(vcs)),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
81
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
82
83
84
class ForeignRevisionTests(TestCase):
85
    """Tests for the ForeignRevision class."""
86
87
    def test_create(self):
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
88
        mapp = DummyForeignVcsMapping(DummyForeignVcs())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
89
        rev = foreign.ForeignRevision(("a", "foreign", "revid"),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
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)
94
95
96
class ShowForeignPropertiesTests(TestCase):
97
    """Tests for the show_foreign_properties() function."""
98
99
    def setUp(self):
100
        super(ShowForeignPropertiesTests, self).setUp()
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
101
        self.vcs = DummyForeignVcs()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
102
        foreign.foreign_vcs_registry.register("dummy",
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
103
            self.vcs, "Dummy VCS")
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
104
105
    def tearDown(self):
106
        super(ShowForeignPropertiesTests, self).tearDown()
107
        foreign.foreign_vcs_registry.remove("dummy")
108
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")))
112
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))
117
118
    def test_show_direct(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
119
        rev = foreign.ForeignRevision(("some", "foreign", "revid"),
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
120
                                      DummyForeignVcsMapping(self.vcs),
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
121
                                      "roundtrip-revid")
122
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
123
                          foreign.show_foreign_properties(rev))