/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:26:22 UTC
  • mfrom: (7167.1.4 run-flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181116182622-qw3gan3hz78a2imw
Add a flake8 test.

Merged from https://code.launchpad.net/~jelmer/brz/run-flake8/+merge/358902

Show diffs side-by-side

added added

removed removed

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