/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

mention the requirement to install Dulwich.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
 
18
17
"""Tests for interfacing with a Git Branch"""
19
18
 
20
 
 
21
 
import dulwich
22
 
from dulwich.repo import (
23
 
    Repo as GitRepo,
24
 
    )
25
 
 
26
 
import os
27
 
 
28
 
from bzrlib import (
29
 
    errors,
30
 
    revision,
31
 
    )
32
 
from bzrlib.branch import (
33
 
    Branch,
34
 
    InterBranch,
35
 
    )
36
 
from bzrlib.bzrdir import (
37
 
    BzrDir,
38
 
    )
39
 
from bzrlib.repository import (
40
 
    Repository,
41
 
    )
 
19
from bzrlib import revision
 
20
from bzrlib.branch import Branch
42
21
 
43
22
from bzrlib.plugins.git import (
44
 
    LocalGitBzrDirFormat,
45
23
    branch,
46
24
    tests,
47
25
    )
48
 
from bzrlib.plugins.git.mapping import (
49
 
    default_mapping,
50
 
    )
 
26
from bzrlib.plugins.git.mapping import default_mapping
 
27
 
 
28
import dulwich as git
51
29
 
52
30
 
53
31
class TestGitBranch(tests.TestCaseInTempDir):
55
33
    _test_needs_features = [tests.GitCommandFeature]
56
34
 
57
35
    def test_open_existing(self):
58
 
        GitRepo.init('.')
59
 
        d = BzrDir.open('.')
60
 
        thebranch = d.create_branch()
 
36
        tests.run_git('init')
 
37
 
 
38
        thebranch = Branch.open('.')
61
39
        self.assertIsInstance(thebranch, branch.GitBranch)
62
40
 
63
 
    def test_repr(self):
64
 
        GitRepo.init('.')
65
 
        d = BzrDir.open('.')
66
 
        thebranch = d.create_branch()
67
 
        self.assertEquals("<LocalGitBranch('file://%s/', 'refs/heads/master')>" % self.test_dir, repr(thebranch))
68
 
 
69
41
    def test_last_revision_is_null(self):
70
 
        GitRepo.init('.')
71
 
        thedir = BzrDir.open('.')
72
 
        thebranch = thedir.create_branch()
 
42
        tests.run_git('init')
 
43
 
 
44
        thebranch = Branch.open('.')
73
45
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
74
46
        self.assertEqual((0, revision.NULL_REVISION),
75
47
                         thebranch.last_revision_info())
76
48
 
77
49
    def simple_commit_a(self):
78
 
        GitRepo.init('.')
 
50
        tests.run_git('init')
79
51
        self.build_tree(['a'])
80
52
        tests.run_git('add', 'a')
81
53
        tests.run_git('commit', '-m', 'a')
83
55
    def test_last_revision_is_valid(self):
84
56
        self.simple_commit_a()
85
57
        head = tests.run_git('rev-parse', 'HEAD').strip()
 
58
 
86
59
        thebranch = Branch.open('.')
87
60
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
88
61
                         thebranch.last_revision())
99
72
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
100
73
                         thebranch.revision_history())
101
74
 
102
 
    def test_tag_annotated(self):
 
75
    def test_tags(self):
103
76
        self.simple_commit_a()
104
77
        reva = tests.run_git('rev-parse', 'HEAD').strip()
 
78
        
105
79
        tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
106
 
        thebranch = Branch.open('.')
107
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
108
 
                          thebranch.tags.get_tag_dict())
109
 
 
110
 
    def test_tag(self):
111
 
        self.simple_commit_a()
112
 
        reva = tests.run_git('rev-parse', 'HEAD').strip()
113
 
        tests.run_git('tag', '-m', 'add tag', 'foo')
114
 
        thebranch = Branch.open('.')
115
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
116
 
                          thebranch.tags.get_tag_dict())
117
 
 
 
80
        
 
81
        thebranch = Branch.open('.')
 
82
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
83
                          thebranch.tags.get_tag_dict())
118
84
        
119
85
 
120
86
class TestWithGitBranch(tests.TestCaseWithTransport):
121
87
 
122
88
    def setUp(self):
123
89
        tests.TestCaseWithTransport.setUp(self)
124
 
        dulwich.repo.Repo.create(self.test_dir)
125
 
        d = BzrDir.open(self.test_dir)
126
 
        self.git_branch = d.create_branch()
 
90
        git.repo.Repo.create(self.test_dir)
 
91
        self.git_branch = Branch.open(self.test_dir)
127
92
 
128
93
    def test_get_parent(self):
129
94
        self.assertIs(None, self.git_branch.get_parent())
130
95
 
131
96
    def test_get_stacked_on_url(self):
132
 
        self.assertRaises(errors.UnstackableBranchFormat, 
133
 
            self.git_branch.get_stacked_on_url)
 
97
        self.assertIs(None, self.git_branch.get_stacked_on_url())
134
98
 
135
99
    def test_get_physical_lock_status(self):
136
100
        self.assertFalse(self.git_branch.get_physical_lock_status())
144
108
 
145
109
    def test_get_format_description(self):
146
110
        self.assertEquals("Git Branch", self.format.get_format_description())
147
 
 
148
 
 
149
 
 
150
 
class BranchTests(tests.TestCaseInTempDir):
151
 
 
152
 
    def make_onerev_branch(self):
153
 
        os.mkdir("d")
154
 
        os.chdir("d")
155
 
        GitRepo.init('.')
156
 
        bb = tests.GitBranchBuilder()
157
 
        bb.set_file("foobar", "foo\nbar\n", False)
158
 
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
159
 
        gitsha = bb.finish()[mark]
160
 
        os.chdir("..")
161
 
        return "d", gitsha
162
 
 
163
 
    def make_tworev_branch(self):
164
 
        os.mkdir("d")
165
 
        os.chdir("d")
166
 
        GitRepo.init('.')
167
 
        bb = tests.GitBranchBuilder()
168
 
        bb.set_file("foobar", "foo\nbar\n", False)
169
 
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
170
 
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
171
 
        marks = bb.finish()
172
 
        os.chdir("..")
173
 
        return "d", (marks[mark1], marks[mark2])
174
 
 
175
 
    def clone_git_branch(self, from_url, to_url):
176
 
        from_dir = BzrDir.open(from_url)
177
 
        to_dir = from_dir.sprout(to_url)
178
 
        return to_dir.open_branch()
179
 
 
180
 
    def test_single_rev(self):
181
 
        path, gitsha = self.make_onerev_branch()
182
 
        oldrepo = Repository.open(path)
183
 
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
184
 
        newbranch = self.clone_git_branch(path, "f")
185
 
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
186
 
 
187
 
    def test_sprouted_tags(self):
188
 
        path, gitsha = self.make_onerev_branch()
189
 
        os.chdir(path)
190
 
        tests.run_git("tag", "lala")
191
 
        os.chdir(self.test_dir)
192
 
        oldrepo = Repository.open(path)
193
 
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
194
 
        newbranch = self.clone_git_branch(path, "f")
195
 
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
196
 
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
197
 
 
198
 
    def test_interbranch_pull(self):
199
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
200
 
        oldrepo = Repository.open(path)
201
 
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
202
 
        newbranch = self.make_branch('g')
203
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
204
 
        inter_branch.pull()
205
 
        self.assertEquals(revid2, newbranch.last_revision())
206
 
 
207
 
    def test_interbranch_pull_noop(self):
208
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
209
 
        oldrepo = Repository.open(path)
210
 
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
211
 
        newbranch = self.make_branch('g')
212
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
213
 
        inter_branch.pull()
214
 
        # This is basically "assertNotRaises"
215
 
        inter_branch.pull()
216
 
        self.assertEquals(revid2, newbranch.last_revision())
217
 
 
218
 
    def test_interbranch_pull_stop_revision(self):
219
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
220
 
        oldrepo = Repository.open(path)
221
 
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
222
 
        newbranch = self.make_branch('g')
223
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
224
 
        inter_branch.pull(stop_revision=revid1)
225
 
        self.assertEquals(revid1, newbranch.last_revision())
226
 
 
227
 
    def test_interbranch_limited_pull(self):
228
 
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
229
 
        oldrepo = Repository.open(path)
230
 
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
231
 
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
232
 
        newbranch = self.make_branch('g')
233
 
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
234
 
        inter_branch.pull(limit=1)
235
 
        self.assertEquals(revid1, newbranch.last_revision())
236
 
        inter_branch.pull(limit=1)
237
 
        self.assertEquals(revid2, newbranch.last_revision())
238
 
 
239
 
 
240
 
class ForeignTestsBranchFactory(object):
241
 
 
242
 
    def make_empty_branch(self, transport):
243
 
        d = LocalGitBzrDirFormat().initialize_on_transport(transport)
244
 
        return d.create_branch()
245
 
 
246
 
    make_branch = make_empty_branch
247
 
 
248
 
 
249