/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_branch.py

  • Committer: Martin Pool
  • Date: 2005-07-06 04:09:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050706040902-5b98f739e5ee1841
- avoid copying string lists when handling unmatched regions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 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 interfacing with a Git Branch"""
19
 
 
20
 
 
21
 
import dulwich
22
 
from dulwich.objects import (
23
 
    Commit,
24
 
    Tag,
25
 
    )
26
 
from dulwich.repo import (
27
 
    Repo as GitRepo,
28
 
    )
29
 
 
30
 
import os
31
 
 
32
 
from bzrlib import (
33
 
    errors,
34
 
    revision,
35
 
    )
36
 
from bzrlib.branch import (
37
 
    Branch,
38
 
    InterBranch,
39
 
    )
40
 
try:
41
 
    from bzrlib.controldir import (
42
 
        ControlDir,
43
 
        )
44
 
except ImportError:
45
 
    # bzr < 2.3
46
 
    from bzrlib.bzrdir import (
47
 
        BzrDir,
48
 
        )
49
 
    ControlDir = BzrDir
50
 
from bzrlib.repository import (
51
 
    Repository,
52
 
    )
53
 
 
54
 
from bzrlib.plugins.git import (
55
 
    LocalGitControlDirFormat,
56
 
    branch,
57
 
    tests,
58
 
    )
59
 
from bzrlib.plugins.git.mapping import (
60
 
    default_mapping,
61
 
    )
62
 
 
63
 
 
64
 
class TestGitBranch(tests.TestCaseInTempDir):
65
 
 
66
 
    def test_open_existing(self):
67
 
        GitRepo.init('.')
68
 
        d = ControlDir.open('.')
69
 
        thebranch = d.create_branch()
70
 
        self.assertIsInstance(thebranch, branch.GitBranch)
71
 
 
72
 
    def test_repr(self):
73
 
        GitRepo.init('.')
74
 
        d = ControlDir.open('.')
75
 
        thebranch = d.create_branch()
76
 
        self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
77
 
 
78
 
    def test_last_revision_is_null(self):
79
 
        GitRepo.init('.')
80
 
        thedir = ControlDir.open('.')
81
 
        thebranch = thedir.create_branch()
82
 
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
83
 
        self.assertEqual((0, revision.NULL_REVISION),
84
 
                         thebranch.last_revision_info())
85
 
 
86
 
    def simple_commit_a(self):
87
 
        r = GitRepo.init('.')
88
 
        self.build_tree(['a'])
89
 
        r.stage(["a"])
90
 
        return r.do_commit("a", committer="Somebody <foo@example.com>")
91
 
 
92
 
    def test_last_revision_is_valid(self):
93
 
        head = self.simple_commit_a()
94
 
        thebranch = Branch.open('.')
95
 
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
96
 
                         thebranch.last_revision())
97
 
 
98
 
    def test_revision_history(self):
99
 
        reva = self.simple_commit_a()
100
 
        self.build_tree(['b'])
101
 
        r = GitRepo(".")
102
 
        r.stage("b")
103
 
        revb = r.do_commit("b", committer="Somebody <foo@example.com>")
104
 
 
105
 
        thebranch = Branch.open('.')
106
 
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
107
 
                         thebranch.revision_history())
108
 
 
109
 
    def test_tag_annotated(self):
110
 
        reva = self.simple_commit_a()
111
 
        o = Tag()
112
 
        o.name = "foo"
113
 
        o.tagger = "Jelmer <foo@example.com>"
114
 
        o.message = "add tag"
115
 
        o.object = (Commit, reva)
116
 
        o.tag_timezone = 0
117
 
        o.tag_time = 42
118
 
        r = GitRepo(".")
119
 
        r.object_store.add_object(o)
120
 
        r['refs/tags/foo'] = o.id
121
 
        thebranch = Branch.open('.')
122
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
123
 
                          thebranch.tags.get_tag_dict())
124
 
 
125
 
    def test_tag(self):
126
 
        reva = self.simple_commit_a()
127
 
        r = GitRepo(".")
128
 
        r.refs["refs/tags/foo"] = reva
129
 
        thebranch = Branch.open('.')
130
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
131
 
                          thebranch.tags.get_tag_dict())
132
 
 
133
 
 
134
 
 
135
 
class TestWithGitBranch(tests.TestCaseWithTransport):
136
 
 
137
 
    def setUp(self):
138
 
        tests.TestCaseWithTransport.setUp(self)
139
 
        dulwich.repo.Repo.create(self.test_dir)
140
 
        d = ControlDir.open(self.test_dir)
141
 
        self.git_branch = d.create_branch()
142
 
 
143
 
    def test_get_parent(self):
144
 
        self.assertIs(None, self.git_branch.get_parent())
145
 
 
146
 
    def test_get_stacked_on_url(self):
147
 
        self.assertRaises(errors.UnstackableBranchFormat,
148
 
            self.git_branch.get_stacked_on_url)
149
 
 
150
 
    def test_get_physical_lock_status(self):
151
 
        self.assertFalse(self.git_branch.get_physical_lock_status())
152
 
 
153
 
 
154
 
class TestGitBranchFormat(tests.TestCase):
155
 
 
156
 
    def setUp(self):
157
 
        super(TestGitBranchFormat, self).setUp()
158
 
        self.format = branch.GitBranchFormat()
159
 
 
160
 
    def test_get_format_description(self):
161
 
        self.assertEquals("Git Branch", self.format.get_format_description())
162
 
 
163
 
    def test_get_network_name(self):
164
 
        self.assertEquals("git", self.format.network_name())
165
 
 
166
 
    def test_supports_tags(self):
167
 
        self.assertTrue(self.format.supports_tags())
168
 
 
169
 
 
170
 
class BranchTests(tests.TestCaseInTempDir):
171
 
 
172
 
    def make_onerev_branch(self):
173
 
        os.mkdir("d")
174
 
        os.chdir("d")
175
 
        GitRepo.init('.')
176
 
        bb = tests.GitBranchBuilder()
177
 
        bb.set_file("foobar", "foo\nbar\n", False)
178
 
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
179
 
        gitsha = bb.finish()[mark]
180
 
        os.chdir("..")
181
 
        return "d", gitsha
182
 
 
183
 
    def make_tworev_branch(self):
184
 
        os.mkdir("d")
185
 
        os.chdir("d")
186
 
        GitRepo.init('.')
187
 
        bb = tests.GitBranchBuilder()
188
 
        bb.set_file("foobar", "foo\nbar\n", False)
189
 
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
190
 
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
191
 
        marks = bb.finish()
192
 
        os.chdir("..")
193
 
        return "d", (marks[mark1], marks[mark2])
194
 
 
195
 
    def clone_git_branch(self, from_url, to_url):
196
 
        from_dir = ControlDir.open(from_url)
197
 
        to_dir = from_dir.sprout(to_url)
198
 
        return to_dir.open_branch()
199
 
 
200
 
    def test_single_rev(self):
201
 
        path, gitsha = self.make_onerev_branch()
202
 
        oldrepo = Repository.open(path)
203
 
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
204
 
        newbranch = self.clone_git_branch(path, "f")
205
 
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
206
 
 
207
 
    def test_sprouted_tags(self):
208
 
        path, gitsha = self.make_onerev_branch()
209
 
        r = GitRepo(path)
210
 
        r.refs["refs/tags/lala"] = r.head()
211
 
        oldrepo = Repository.open(path)
212
 
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
213
 
        newbranch = self.clone_git_branch(path, "f")
214
 
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
215
 
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
216
 
 
217
 
    def test_interbranch_pull(self):
218
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
219
 
        oldrepo = Repository.open(path)
220
 
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
221
 
        newbranch = self.make_branch('g')
222
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
223
 
        inter_branch.pull()
224
 
        self.assertEquals(revid2, newbranch.last_revision())
225
 
 
226
 
    def test_interbranch_pull_noop(self):
227
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
228
 
        oldrepo = Repository.open(path)
229
 
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
230
 
        newbranch = self.make_branch('g')
231
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
232
 
        inter_branch.pull()
233
 
        # This is basically "assertNotRaises"
234
 
        inter_branch.pull()
235
 
        self.assertEquals(revid2, newbranch.last_revision())
236
 
 
237
 
    def test_interbranch_pull_stop_revision(self):
238
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
239
 
        oldrepo = Repository.open(path)
240
 
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
241
 
        newbranch = self.make_branch('g')
242
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
243
 
        inter_branch.pull(stop_revision=revid1)
244
 
        self.assertEquals(revid1, newbranch.last_revision())
245
 
 
246
 
    def test_interbranch_limited_pull(self):
247
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
248
 
        oldrepo = Repository.open(path)
249
 
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
250
 
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
251
 
        newbranch = self.make_branch('g')
252
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
253
 
        inter_branch.pull(limit=1)
254
 
        self.assertEquals(revid1, newbranch.last_revision())
255
 
        inter_branch.pull(limit=1)
256
 
        self.assertEquals(revid2, newbranch.last_revision())
257
 
 
258
 
 
259
 
class ForeignTestsBranchFactory(object):
260
 
 
261
 
    def make_empty_branch(self, transport):
262
 
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
263
 
        return d.create_branch()
264
 
 
265
 
    make_branch = make_empty_branch
266
 
 
267
 
 
268