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