/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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