/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
20
from bzrlib import errors, foreign
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
21
from bzrlib.inventory import Inventory
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
22
from bzrlib.revision import Revision
23
from bzrlib.tests import TestCase
24
25
26
class DummyForeignVcsMapping(foreign.VcsMapping):
27
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
28
29
    def __eq__(self, other):
30
        return type(self) == type(other)
31
32
    def show_foreign_revid(self, foreign_revid):
33
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
34
35
    def revision_id_bzr_to_foreign(self, bzr_revid):
36
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
37
38
    def revision_id_foreign_to_bzr(self, foreign_revid):
39
        return "dummy-v1:%s-%s-%s" % foreign_revid
40
41
42
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
43
44
    def revision_id_bzr_to_foreign(self, revid):
45
        if not revid.startswith("dummy-"):
46
            raise errors.InvalidRevisionId(revid, None)
47
        mapping_version = revid[len("dummy-"):len("dummy-vx")]
48
        mapping = self.get(mapping_version)
49
        return mapping.revision_id_bzr_to_foreign(revid)
50
51
52
class DummyForeignVcs(foreign.ForeignVcs):
53
    """A dummy Foreign VCS, for use with testing.
54
    
55
    It has revision ids that are a tuple with three strings.
56
    """
57
58
    def __init__(self):
59
        self.mapping_registry = DummyForeignVcsMappingRegistry()
60
        self.mapping_registry.register("v1", DummyForeignVcsMapping(), 
61
                                       "Version 1")
62
63
64
class ForeignVcsRegistryTests(TestCase):
65
66
    def test_parse_revision_id_no_dash(self):       
67
        reg = foreign.ForeignVcsRegistry()
68
        self.assertRaises(errors.InvalidRevisionId, 
69
                          reg.parse_revision_id, "invalid")
70
        
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")
75
76
    def test_parse_revision_id(self):
77
        reg = foreign.ForeignVcsRegistry()
78
        reg.register("dummy", DummyForeignVcs(), "Dummy VCS")
79
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping()),
80
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
81
82
83
class ForeignRevisionTests(TestCase):
84
    """Tests for the ForeignRevision class."""
85
86
    def test_create(self):
87
        mapp = DummyForeignVcsMapping()
88
        rev = foreign.ForeignRevision(("a", "foreign", "revid"), 
89
                                      mapp, "roundtripped-revid")
90
        self.assertEquals("", rev.inventory_sha1)
91
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
92
        self.assertEquals(mapp, rev.mapping)
93
94
95
class ShowForeignPropertiesTests(TestCase):
96
    """Tests for the show_foreign_properties() function."""
97
98
    def setUp(self):
99
        super(ShowForeignPropertiesTests, self).setUp()
100
        foreign.foreign_vcs_registry.register("dummy", 
101
            DummyForeignVcs(), "Dummy VCS")
102
103
    def tearDown(self):
104
        super(ShowForeignPropertiesTests, self).tearDown()
105
        foreign.foreign_vcs_registry.remove("dummy")
106
107
    def test_show_non_foreign(self):
108
        """Test use with a native (non-foreign) bzr revision."""
109
        self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
110
111
    def test_show_imported(self):
112
        rev = Revision("dummy-v1:my-foreign-revid")
113
        self.assertEquals({ "dummy ding": "my/foreign\\revid" },
114
                          foreign.show_foreign_properties(rev))
115
116
    def test_show_direct(self):
117
        rev = foreign.ForeignRevision(("some", "foreign", "revid"), 
118
                                      DummyForeignVcsMapping(), 
119
                                      "roundtrip-revid")
120
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
121
                          foreign.show_foreign_properties(rev))
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
122
123
124
class WorkingTreeFileIdUpdateTests(TestCase):
125
    """Tests for determine_fileid_renames()."""
126
127
    def test_det_renames_same(self):
128
        a = Inventory()
129
        a.add_path("bla", "directory", "bla-a")
130
        b = Inventory()
131
        b.add_path("bla", "directory", "bla-a")
132
        self.assertEquals( {}, foreign.determine_fileid_renames(a, b))
133
134
    def test_det_renames_simple(self):
135
        a = Inventory()
136
        a.add_path("bla", "directory", "bla-a")
137
        b = Inventory()
138
        b.add_path("bla", "directory", "bla-b")
139
        self.assertEquals(
140
                {"bla": ("bla-a", "bla-b")},
141
                foreign.determine_fileid_renames(a, b))
142
143
    def test_det_renames_root(self):
144
        a = Inventory()
145
        a.add_path("", "directory", "bla-a")
146
        b = Inventory()
147
        b.add_path("", "directory", "bla-b")
148
        self.assertEquals(
149
                {"": ("bla-a", "bla-b")},
150
                foreign.determine_fileid_renames(a, b))