1
# Copyright (C) 2007 David Allouche <ddaa@ddaa.net>
2
# Copyright (C) 2007-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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
18
"""Black-box tests for bzr-git."""
20
from __future__ import absolute_import
22
from dulwich.repo import (
28
from ...controldir import (
32
from ...tests.blackbox import ExternalBase
33
from ...workingtree import WorkingTree
40
class TestGitBlackBox(ExternalBase):
42
def simple_commit(self):
43
# Create a git repository with a revision.
44
repo = GitRepo.init(self.test_dir)
45
builder = tests.GitBranchBuilder()
46
builder.set_file('a', b'text for a\n', False)
47
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
48
return repo, builder.finish()[r1]
51
r = GitRepo.init(self.test_dir)
52
dir = ControlDir.open(self.test_dir)
54
output, error = self.run_bzr(['nick'])
55
self.assertEqual("master\n", output)
57
def test_branches(self):
59
output, error = self.run_bzr(['branches'])
60
self.assertEqual("* master\n", output)
64
output, error = self.run_bzr(['info'])
65
self.assertEqual(error, '')
66
self.assertTrue("Standalone tree (format: git)" in output)
68
def test_branch(self):
70
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
72
builder = tests.GitBranchBuilder()
73
builder.set_file(b'a', b'text for a\n', False)
74
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
78
output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
80
(error == 'Branched 1 revision(s).\n') or
81
(error == 'Branched 1 revision.\n'),
84
def test_checkout(self):
86
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
88
builder = tests.GitBranchBuilder()
89
builder.set_file(b'a', b'text for a\n', False)
90
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
94
output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
95
self.assertEqual(error,
96
'Fetching from Git to Bazaar repository. '
97
'For better performance, fetch into a Git repository.\n')
98
self.assertEqual(output, '')
100
def test_branch_ls(self):
102
output, error = self.run_bzr(['ls', '-r-1'])
103
self.assertEqual(error, '')
104
self.assertEqual(output, "a\n")
107
self.run_bzr("init --format=git repo")
109
def test_info_verbose(self):
112
output, error = self.run_bzr(['info', '-v'])
113
self.assertEqual(error, '')
114
self.assertTrue("Standalone tree (format: git)" in output)
115
self.assertTrue("control: Local Git Repository" in output)
116
self.assertTrue("branch: Local Git Branch" in output)
117
self.assertTrue("repository: Git Repository" in output)
119
def test_push_roundtripping(self):
120
self.knownFailure("roundtripping is not yet supported")
121
self.with_roundtripping()
123
GitRepo.init(os.path.join(self.test_dir, "bla"))
124
self.run_bzr(['init', 'foo'])
125
self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
126
# when roundtripping is supported
127
output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
128
self.assertEqual(b"", output)
129
self.assertTrue(error.endswith(b"Created new branch.\n"))
132
# Smoke test for "bzr log" in a git repository.
135
# Check that bzr log does not fail and includes the revision.
136
output, error = self.run_bzr(['log'])
137
self.assertEqual(error, '')
139
'<The commit message>' in output,
140
"Commit message was not found in output:\n%s" % (output,))
142
def test_log_verbose(self):
143
# Smoke test for "bzr log -v" in a git repository.
146
# Check that bzr log does not fail and includes the revision.
147
output, error = self.run_bzr(['log', '-v'])
150
git_repo, commit_sha1 = self.simple_commit()
151
git_repo.refs[b"refs/tags/foo"] = commit_sha1
153
output, error = self.run_bzr(['tags'])
154
self.assertEqual(error, '')
155
self.assertEqual(output, "foo 1\n")
160
output, error = self.run_bzr(["tag", "bar"])
162
# bzr <= 2.2 emits this message in the output stream
163
# bzr => 2.3 emits this message in the error stream
164
self.assertEqual(error + output, 'Created tag bar.\n')
166
def test_init_repo(self):
167
output, error = self.run_bzr(["init", "--format=git", "bla.git"])
168
self.assertEqual(error, '')
169
self.assertEqual(output, 'Created a standalone tree (format: git)\n')
171
def test_diff_format(self):
172
tree = self.make_branch_and_tree('.')
173
self.build_tree(['a'])
175
output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
176
self.assertEqual(error, '')
177
self.assertEqual(output,
178
'diff --git /dev/null b/a\n'
181
'index 0000000..c197bd8 100644\n'
187
def test_git_import_uncolocated(self):
188
r = GitRepo.init("a", mkdir=True)
189
self.build_tree(["a/file"])
191
r.do_commit(ref=b"refs/heads/abranch",
192
committer=b"Joe <joe@example.com>", message=b"Dummy")
193
r.do_commit(ref=b"refs/heads/bbranch",
194
committer=b"Joe <joe@example.com>", message=b"Dummy")
195
self.run_bzr(["git-import", "a", "b"])
197
set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
199
def test_git_import(self):
200
r = GitRepo.init("a", mkdir=True)
201
self.build_tree(["a/file"])
203
r.do_commit(ref=b"refs/heads/abranch",
204
committer=b"Joe <joe@example.com>", message=b"Dummy")
205
r.do_commit(ref=b"refs/heads/bbranch",
206
committer=b"Joe <joe@example.com>", message=b"Dummy")
207
self.run_bzr(["git-import", "--colocated", "a", "b"])
208
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
209
self.assertEqual(set(["abranch", "bbranch"]),
210
set(ControlDir.open("b").get_branches().keys()))
212
def test_git_import_incremental(self):
213
r = GitRepo.init("a", mkdir=True)
214
self.build_tree(["a/file"])
216
r.do_commit(ref=b"refs/heads/abranch",
217
committer=b"Joe <joe@example.com>", message=b"Dummy")
218
self.run_bzr(["git-import", "--colocated", "a", "b"])
219
self.run_bzr(["git-import", "--colocated", "a", "b"])
220
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
221
b = ControlDir.open("b")
222
self.assertEqual(["abranch"], list(b.get_branches().keys()))
224
def test_git_import_tags(self):
225
r = GitRepo.init("a", mkdir=True)
226
self.build_tree(["a/file"])
228
cid = r.do_commit(ref=b"refs/heads/abranch",
229
committer=b"Joe <joe@example.com>", message=b"Dummy")
230
r[b"refs/tags/atag"] = cid
231
self.run_bzr(["git-import", "--colocated", "a", "b"])
232
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
233
b = ControlDir.open("b")
234
self.assertEqual(["abranch"], list(b.get_branches().keys()))
235
self.assertEqual(["atag"],
236
list(b.open_branch("abranch").tags.get_tag_dict().keys()))
238
def test_git_import_colo(self):
239
r = GitRepo.init("a", mkdir=True)
240
self.build_tree(["a/file"])
242
r.do_commit(ref=b"refs/heads/abranch",
243
committer=b"Joe <joe@example.com>", message=b"Dummy")
244
r.do_commit(ref=b"refs/heads/bbranch",
245
committer=b"Joe <joe@example.com>", message=b"Dummy")
246
self.make_controldir("b", format="development-colo")
247
self.run_bzr(["git-import", "--colocated", "a", "b"])
249
set([b.name for b in ControlDir.open("b").list_branches()]),
250
set(["abranch", "bbranch"]))
252
def test_git_refs_from_git(self):
253
r = GitRepo.init("a", mkdir=True)
254
self.build_tree(["a/file"])
256
cid = r.do_commit(ref=b"refs/heads/abranch",
257
committer=b"Joe <joe@example.com>", message=b"Dummy")
258
r[b"refs/tags/atag"] = cid
259
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
260
self.assertEqual(stderr, "")
261
self.assertEqual(stdout,
262
'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
263
'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
265
def test_git_refs_from_bzr(self):
266
tree = self.make_branch_and_tree('a')
267
self.build_tree(["a/file"])
270
committer=b"Joe <joe@example.com>", message=b"Dummy")
271
tree.branch.tags.set_tag("atag", revid)
272
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
273
self.assertEqual(stderr, "")
274
self.assertTrue("refs/tags/atag -> " in stdout)
275
self.assertTrue("HEAD -> " in stdout)
277
def test_check(self):
278
r = GitRepo.init("gitr", mkdir=True)
279
self.build_tree_contents([("gitr/foo", b"hello from git")])
281
r.do_commit(b"message", committer=b"Somebody <user@example.com>")
282
out, err = self.run_bzr(["check", "gitr"])
284
self.assertEqual(out, '')
285
self.assertTrue(err.endswith, '3 objects\n')
288
class ShallowTests(ExternalBase):
291
super(ShallowTests, self).setUp()
292
# Smoke test for "bzr log" in a git repository with shallow depth.
293
self.repo = GitRepo.init('gitr', mkdir=True)
294
self.build_tree_contents([("gitr/foo", b"hello from git")])
295
self.repo.stage("foo")
297
b"message", committer=b"Somebody <user@example.com>",
298
commit_timestamp=1526330165, commit_timezone=0,
299
author_timestamp=1526330165, author_timezone=0,
300
merge_heads=[b'aa' * 20])
302
def test_log_shallow(self):
303
# Check that bzr log does not fail and includes the revision.
304
output, error = self.run_bzr(['log', 'gitr'], retcode=3)
306
error, 'brz: ERROR: Further revision history missing.\n')
307
self.assertEqual(output,
308
'------------------------------------------------------------\n'
309
'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
310
'git commit: ' + self.repo.head().decode('ascii') + '\n'
311
'committer: Somebody <user@example.com>\n'
312
'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
316
def test_version_info_rio(self):
317
output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
318
self.assertEqual(error, '')
319
self.assertNotIn('revno:', output)
321
def test_version_info_python(self):
322
output, error = self.run_bzr(['version-info', '--python', 'gitr'])
323
self.assertEqual(error, '')
324
self.assertNotIn('revno:', output)
326
def test_version_info_custom_with_revno(self):
327
output, error = self.run_bzr(
328
['version-info', '--custom',
329
'--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
331
error, 'brz: ERROR: Variable {revno} is not available.\n')
332
self.assertEqual(output, 'VERSION_INFO r')
334
def test_version_info_custom_without_revno(self):
335
output, error = self.run_bzr(
336
['version-info', '--custom', '--template=VERSION_INFO \n',
338
self.assertEqual(error, '')
339
self.assertEqual(output, 'VERSION_INFO \n')
342
class SwitchTests(ExternalBase):
344
def test_switch_branch(self):
345
# Create a git repository with a revision.
346
repo = GitRepo.init(self.test_dir)
347
builder = tests.GitBranchBuilder()
348
builder.set_branch(b'refs/heads/oldbranch')
349
builder.set_file('a', b'text for a\n', False)
350
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
351
builder.set_branch(b'refs/heads/newbranch')
353
builder.set_file('a', b'text for new a\n', False)
354
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
357
repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
361
output, error = self.run_bzr('switch oldbranch')
362
self.assertEqual(output, '')
363
self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
365
self.assertFileEqual("text for a\n", 'a')
366
tree = WorkingTree.open('.')
367
with tree.lock_read():
368
basis_tree = tree.basis_tree()
369
with basis_tree.lock_read():
370
self.assertEqual([], list(tree.iter_changes(basis_tree)))
373
class GrepTests(ExternalBase):
375
def test_simple_grep(self):
376
tree = self.make_branch_and_tree('.', format='git')
377
self.build_tree_contents([('a', 'text for a\n')])
379
output, error = self.run_bzr('grep text')
380
self.assertEqual(output, 'a:text for a\n')
381
self.assertEqual(error, '')