/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
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
23
from bzrlib.tests import TestCase, TestCaseWithTransport
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
24
3920.2.7 by Jelmer Vernooij
Add comments about dummy vcs.
25
# This is the dummy foreign revision control system, used 
26
# mainly here in the testsuite to test the foreign VCS infrastructure.
27
# It is basically standard Bazaar with some minor modifications to 
28
# make it "foreign". 
29
# 
30
# It has the following differences to "regular" Bazaar:
31
# - The revision ids are tuples, not strings.
32
3830.4.5 by Jelmer Vernooij
add tests for VCS infrastructure classes.
33
34
class DummyForeignVcsMapping(foreign.VcsMapping):
35
    """A simple mapping for the dummy Foreign VCS, for use with testing."""
36
37
    def __eq__(self, other):
38
        return type(self) == type(other)
39
40
    def show_foreign_revid(self, foreign_revid):
41
        return { "dummy ding": "%s/%s\\%s" % foreign_revid }
42
43
    def revision_id_bzr_to_foreign(self, bzr_revid):
44
        return tuple(bzr_revid[len("dummy-v1:"):].split("-")), self
45
46
    def revision_id_foreign_to_bzr(self, foreign_revid):
47
        return "dummy-v1:%s-%s-%s" % foreign_revid
48
49
50
class DummyForeignVcsMappingRegistry(foreign.VcsMappingRegistry):
51
52
    def revision_id_bzr_to_foreign(self, revid):
53
        if not revid.startswith("dummy-"):
54
            raise errors.InvalidRevisionId(revid, None)
55
        mapping_version = revid[len("dummy-"):len("dummy-vx")]
56
        mapping = self.get(mapping_version)
57
        return mapping.revision_id_bzr_to_foreign(revid)
58
59
60
class DummyForeignVcs(foreign.ForeignVcs):
61
    """A dummy Foreign VCS, for use with testing.
62
    
63
    It has revision ids that are a tuple with three strings.
64
    """
65
66
    def __init__(self):
67
        self.mapping_registry = DummyForeignVcsMappingRegistry()
68
        self.mapping_registry.register("v1", DummyForeignVcsMapping(), 
69
                                       "Version 1")
70
71
72
class ForeignVcsRegistryTests(TestCase):
73
74
    def test_parse_revision_id_no_dash(self):       
75
        reg = foreign.ForeignVcsRegistry()
76
        self.assertRaises(errors.InvalidRevisionId, 
77
                          reg.parse_revision_id, "invalid")
78
        
79
    def test_parse_revision_id_unknown_mapping(self):       
80
        reg = foreign.ForeignVcsRegistry()
81
        self.assertRaises(errors.InvalidRevisionId, 
82
                          reg.parse_revision_id, "unknown-foreignrevid")
83
84
    def test_parse_revision_id(self):
85
        reg = foreign.ForeignVcsRegistry()
86
        reg.register("dummy", DummyForeignVcs(), "Dummy VCS")
87
        self.assertEquals((("some", "foreign", "revid"), DummyForeignVcsMapping()),
88
                          reg.parse_revision_id("dummy-v1:some-foreign-revid"))
89
90
91
class ForeignRevisionTests(TestCase):
92
    """Tests for the ForeignRevision class."""
93
94
    def test_create(self):
95
        mapp = DummyForeignVcsMapping()
96
        rev = foreign.ForeignRevision(("a", "foreign", "revid"), 
97
                                      mapp, "roundtripped-revid")
98
        self.assertEquals("", rev.inventory_sha1)
99
        self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
100
        self.assertEquals(mapp, rev.mapping)
101
102
103
class ShowForeignPropertiesTests(TestCase):
104
    """Tests for the show_foreign_properties() function."""
105
106
    def setUp(self):
107
        super(ShowForeignPropertiesTests, self).setUp()
108
        foreign.foreign_vcs_registry.register("dummy", 
109
            DummyForeignVcs(), "Dummy VCS")
110
111
    def tearDown(self):
112
        super(ShowForeignPropertiesTests, self).tearDown()
113
        foreign.foreign_vcs_registry.remove("dummy")
114
115
    def test_show_non_foreign(self):
116
        """Test use with a native (non-foreign) bzr revision."""
117
        self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
118
119
    def test_show_imported(self):
120
        rev = Revision("dummy-v1:my-foreign-revid")
121
        self.assertEquals({ "dummy ding": "my/foreign\\revid" },
122
                          foreign.show_foreign_properties(rev))
123
124
    def test_show_direct(self):
125
        rev = foreign.ForeignRevision(("some", "foreign", "revid"), 
126
                                      DummyForeignVcsMapping(), 
127
                                      "roundtrip-revid")
128
        self.assertEquals({ "dummy ding": "some/foreign\\revid" },
129
                          foreign.show_foreign_properties(rev))
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
130
131
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
132
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
133
    """Tests for determine_fileid_renames()."""
134
135
    def test_det_renames_same(self):
136
        a = Inventory()
137
        a.add_path("bla", "directory", "bla-a")
138
        b = Inventory()
139
        b.add_path("bla", "directory", "bla-a")
140
        self.assertEquals( {}, foreign.determine_fileid_renames(a, b))
141
142
    def test_det_renames_simple(self):
143
        a = Inventory()
144
        a.add_path("bla", "directory", "bla-a")
145
        b = Inventory()
146
        b.add_path("bla", "directory", "bla-b")
147
        self.assertEquals(
148
                {"bla": ("bla-a", "bla-b")},
149
                foreign.determine_fileid_renames(a, b))
150
151
    def test_det_renames_root(self):
152
        a = Inventory()
153
        a.add_path("", "directory", "bla-a")
154
        b = Inventory()
155
        b.add_path("", "directory", "bla-b")
156
        self.assertEquals(
157
                {"": ("bla-a", "bla-b")},
158
                foreign.determine_fileid_renames(a, b))
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
159
160
    def test_update_workinginv(self):
161
        a = Inventory()
162
        a.add_path("bla", "directory", "bla-a")
163
        b = Inventory()
164
        b.add_path("bla", "directory", "bla-b")
165
        wt = self.make_branch_and_tree('br1')
166
        self.build_tree_contents([('br1/bla', 'original contents\n')])
167
        wt.add('bla', 'bla-a')
168
        foreign.update_workinginv_fileids(wt, a, b)
169
        wt.lock_read()
170
        try:
171
            self.assertEquals(["TREE_ROOT", "bla-b"], list(wt.inventory))
172
        finally:
173
            wt.unlock()