/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 breezy/git/tests/test_branch.py

  • Committer: Jelmer Vernooij
  • Date: 2018-08-14 01:15:02 UTC
  • mto: This revision was merged to the branch mainline in revision 7078.
  • Revision ID: jelmer@jelmer.uk-20180814011502-5zaydaq02vc2qxo1
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
# Copyright (C) 2007 Canonical Ltd
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
 
19
"""Tests for interfacing with a Git Branch"""
 
20
 
 
21
from __future__ import absolute_import
 
22
 
 
23
import dulwich
 
24
from dulwich.objects import (
 
25
    Commit,
 
26
    Tag,
 
27
    )
 
28
from dulwich.repo import (
 
29
    Repo as GitRepo,
 
30
    )
 
31
 
 
32
import os
 
33
import urllib
 
34
 
 
35
from ... import (
 
36
    errors,
 
37
    revision,
 
38
    urlutils,
 
39
    )
 
40
from ...branch import (
 
41
    Branch,
 
42
    InterBranch,
 
43
    UnstackableBranchFormat,
 
44
    )
 
45
from ...controldir import (
 
46
    ControlDir,
 
47
    )
 
48
from ...repository import (
 
49
    Repository,
 
50
    )
 
51
 
 
52
from .. import (
 
53
    branch,
 
54
    tests,
 
55
    )
 
56
from ..dir import (
 
57
    LocalGitControlDirFormat,
 
58
    )
 
59
from ..mapping import (
 
60
    default_mapping,
 
61
    )
 
62
 
 
63
 
 
64
class TestGitBranch(tests.TestCaseInTempDir):
 
65
 
 
66
    def test_open_by_ref(self):
 
67
        GitRepo.init('.')
 
68
        url = "%s,ref=%s" % (
 
69
            urlutils.local_path_to_url(self.test_dir),
 
70
            urlutils.quote("refs/remotes/origin/unstable", safe='')
 
71
            )
 
72
        d = ControlDir.open(url)
 
73
        b = d.create_branch()
 
74
        self.assertEqual(b.ref, b"refs/remotes/origin/unstable")
 
75
 
 
76
    def test_open_existing(self):
 
77
        r = GitRepo.init('.')
 
78
        d = ControlDir.open('.')
 
79
        thebranch = d.create_branch()
 
80
        self.assertIsInstance(thebranch, branch.GitBranch)
 
81
 
 
82
    def test_repr(self):
 
83
        r = GitRepo.init('.')
 
84
        d = ControlDir.open('.')
 
85
        thebranch = d.create_branch()
 
86
        self.assertEqual(
 
87
            "<LocalGitBranch('%s/', %r)>" % (
 
88
                urlutils.local_path_to_url(self.test_dir),
 
89
                u'master'),
 
90
            repr(thebranch))
 
91
 
 
92
    def test_last_revision_is_null(self):
 
93
        r = GitRepo.init('.')
 
94
        thedir = ControlDir.open('.')
 
95
        thebranch = thedir.create_branch()
 
96
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
 
97
        self.assertEqual((0, revision.NULL_REVISION),
 
98
                         thebranch.last_revision_info())
 
99
 
 
100
    def simple_commit_a(self):
 
101
        r = GitRepo.init('.')
 
102
        self.build_tree(['a'])
 
103
        r.stage(["a"])
 
104
        return r.do_commit(b"a", committer=b"Somebody <foo@example.com>")
 
105
 
 
106
    def test_last_revision_is_valid(self):
 
107
        head = self.simple_commit_a()
 
108
        thebranch = Branch.open('.')
 
109
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
 
110
                         thebranch.last_revision())
 
111
 
 
112
    def test_last_revision_info(self):
 
113
        reva = self.simple_commit_a()
 
114
        self.build_tree(['b'])
 
115
        r = GitRepo(".")
 
116
        r.stage("b")
 
117
        revb = r.do_commit(b"b", committer=b"Somebody <foo@example.com>")
 
118
 
 
119
        thebranch = Branch.open('.')
 
120
        self.assertEqual((2, default_mapping.revision_id_foreign_to_bzr(revb)), thebranch.last_revision_info())
 
121
 
 
122
    def test_tag_annotated(self):
 
123
        reva = self.simple_commit_a()
 
124
        o = Tag()
 
125
        o.name = b"foo"
 
126
        o.tagger = b"Jelmer <foo@example.com>"
 
127
        o.message = b"add tag"
 
128
        o.object = (Commit, reva)
 
129
        o.tag_timezone = 0
 
130
        o.tag_time = 42
 
131
        r = GitRepo(".")
 
132
        r.object_store.add_object(o)
 
133
        r[b'refs/tags/foo'] = o.id
 
134
        thebranch = Branch.open('.')
 
135
        self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
136
                          thebranch.tags.get_tag_dict())
 
137
 
 
138
    def test_tag(self):
 
139
        reva = self.simple_commit_a()
 
140
        r = GitRepo(".")
 
141
        r.refs[b"refs/tags/foo"] = reva
 
142
        thebranch = Branch.open('.')
 
143
        self.assertEqual({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
144
                          thebranch.tags.get_tag_dict())
 
145
 
 
146
 
 
147
 
 
148
class TestWithGitBranch(tests.TestCaseWithTransport):
 
149
 
 
150
    def setUp(self):
 
151
        tests.TestCaseWithTransport.setUp(self)
 
152
        r = dulwich.repo.Repo.create(self.test_dir)
 
153
        d = ControlDir.open(self.test_dir)
 
154
        self.git_branch = d.create_branch()
 
155
 
 
156
    def test_get_parent(self):
 
157
        self.assertIs(None, self.git_branch.get_parent())
 
158
 
 
159
    def test_get_stacked_on_url(self):
 
160
        self.assertRaises(UnstackableBranchFormat,
 
161
            self.git_branch.get_stacked_on_url)
 
162
 
 
163
    def test_get_physical_lock_status(self):
 
164
        self.assertFalse(self.git_branch.get_physical_lock_status())
 
165
 
 
166
 
 
167
class TestLocalGitBranchFormat(tests.TestCase):
 
168
 
 
169
    def setUp(self):
 
170
        super(TestLocalGitBranchFormat, self).setUp()
 
171
        self.format = branch.LocalGitBranchFormat()
 
172
 
 
173
    def test_get_format_description(self):
 
174
        self.assertEqual("Local Git Branch", self.format.get_format_description())
 
175
 
 
176
    def test_get_network_name(self):
 
177
        self.assertEqual(b"git", self.format.network_name())
 
178
 
 
179
    def test_supports_tags(self):
 
180
        self.assertTrue(self.format.supports_tags())
 
181
 
 
182
 
 
183
class BranchTests(tests.TestCaseInTempDir):
 
184
 
 
185
    def make_onerev_branch(self):
 
186
        os.mkdir("d")
 
187
        os.chdir("d")
 
188
        GitRepo.init('.')
 
189
        bb = tests.GitBranchBuilder()
 
190
        bb.set_file("foobar", b"foo\nbar\n", False)
 
191
        mark = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
192
        gitsha = bb.finish()[mark]
 
193
        os.chdir("..")
 
194
        return os.path.abspath("d"), gitsha
 
195
 
 
196
    def make_tworev_branch(self):
 
197
        os.mkdir("d")
 
198
        os.chdir("d")
 
199
        GitRepo.init('.')
 
200
        bb = tests.GitBranchBuilder()
 
201
        bb.set_file("foobar", b"foo\nbar\n", False)
 
202
        mark1 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
203
        mark2 = bb.commit(b"Somebody <somebody@someorg.org>", b"mymsg")
 
204
        marks = bb.finish()
 
205
        os.chdir("..")
 
206
        return "d", (marks[mark1], marks[mark2])
 
207
 
 
208
    def clone_git_branch(self, from_url, to_url):
 
209
        from_dir = ControlDir.open(from_url)
 
210
        to_dir = from_dir.sprout(to_url)
 
211
        return to_dir.open_branch()
 
212
 
 
213
    def test_single_rev(self):
 
214
        path, gitsha = self.make_onerev_branch()
 
215
        oldrepo = Repository.open(path)
 
216
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
217
        self.assertEqual(gitsha, oldrepo._git.get_refs()[b"refs/heads/master"])
 
218
        newbranch = self.clone_git_branch(path, "f")
 
219
        self.assertEqual([revid], newbranch.repository.all_revision_ids())
 
220
 
 
221
    def test_sprouted_tags(self):
 
222
        path, gitsha = self.make_onerev_branch()
 
223
        r = GitRepo(path)
 
224
        r.refs[b"refs/tags/lala"] = r.head()
 
225
        oldrepo = Repository.open(path)
 
226
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
227
        newbranch = self.clone_git_branch(path, "f")
 
228
        self.assertEqual({"lala": revid}, newbranch.tags.get_tag_dict())
 
229
        self.assertEqual([revid], newbranch.repository.all_revision_ids())
 
230
 
 
231
    def test_interbranch_pull(self):
 
232
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
233
        oldrepo = Repository.open(path)
 
234
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
235
        newbranch = self.make_branch('g')
 
236
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
237
        inter_branch.pull()
 
238
        self.assertEqual(revid2, newbranch.last_revision())
 
239
 
 
240
    def test_interbranch_pull_noop(self):
 
241
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
242
        oldrepo = Repository.open(path)
 
243
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
244
        newbranch = self.make_branch('g')
 
245
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
246
        inter_branch.pull()
 
247
        # This is basically "assertNotRaises"
 
248
        inter_branch.pull()
 
249
        self.assertEqual(revid2, newbranch.last_revision())
 
250
 
 
251
    def test_interbranch_pull_stop_revision(self):
 
252
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
253
        oldrepo = Repository.open(path)
 
254
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
255
        newbranch = self.make_branch('g')
 
256
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
257
        inter_branch.pull(stop_revision=revid1)
 
258
        self.assertEqual(revid1, newbranch.last_revision())
 
259
 
 
260
    def test_interbranch_pull_with_tags(self):
 
261
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
262
        gitrepo = GitRepo(path)
 
263
        gitrepo.refs[b"refs/tags/sometag"] = gitsha2
 
264
        oldrepo = Repository.open(path)
 
265
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
266
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
267
        newbranch = self.make_branch('g')
 
268
        source_branch = Branch.open(path)
 
269
        source_branch.get_config().set_user_option("branch.fetch_tags", True)
 
270
        inter_branch = InterBranch.get(source_branch, newbranch)
 
271
        inter_branch.pull(stop_revision=revid1)
 
272
        self.assertEqual(revid1, newbranch.last_revision())
 
273
        self.assertTrue(newbranch.repository.has_revision(revid2))
 
274
 
 
275
 
 
276
class ForeignTestsBranchFactory(object):
 
277
 
 
278
    def make_empty_branch(self, transport):
 
279
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
 
280
        return d.create_branch()
 
281
 
 
282
    make_branch = make_empty_branch