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
38
from ...tests.features import PluginLoadedFeature
41
class TestGitBlackBox(ExternalBase):
43
def simple_commit(self):
44
# Create a git repository with a revision.
45
repo = GitRepo.init(self.test_dir)
46
builder = tests.GitBranchBuilder()
47
builder.set_file('a', b'text for a\n', False)
48
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
49
return repo, builder.finish()[r1]
52
r = GitRepo.init(self.test_dir)
53
dir = ControlDir.open(self.test_dir)
55
output, error = self.run_bzr(['nick'])
56
self.assertEqual("master\n", output)
58
def test_branches(self):
60
output, error = self.run_bzr(['branches'])
61
self.assertEqual("* master\n", output)
65
output, error = self.run_bzr(['info'])
66
self.assertEqual(error, '')
69
'Standalone tree (format: git)\n'
71
' light checkout root: .\n'
72
' checkout of co-located branch: master\n')
74
def test_ignore(self):
76
output, error = self.run_bzr(['ignore', 'foo'])
77
self.assertEqual(error, '')
78
self.assertEqual(output, '')
79
self.assertFileEqual("foo\n", ".gitignore")
81
def test_branch(self):
83
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
85
builder = tests.GitBranchBuilder()
86
builder.set_file(b'a', b'text for a\n', False)
87
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
91
output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
93
(error == 'Branched 1 revision(s).\n') or
94
(error == 'Branched 1 revision.\n'),
97
def test_checkout(self):
99
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
100
os.chdir('gitbranch')
101
builder = tests.GitBranchBuilder()
102
builder.set_file(b'a', b'text for a\n', False)
103
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
107
output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
108
self.assertEqual(error,
109
'Fetching from Git to Bazaar repository. '
110
'For better performance, fetch into a Git repository.\n')
111
self.assertEqual(output, '')
113
def test_branch_ls(self):
115
output, error = self.run_bzr(['ls', '-r-1'])
116
self.assertEqual(error, '')
117
self.assertEqual(output, "a\n")
120
self.run_bzr("init --format=git repo")
122
def test_info_verbose(self):
125
output, error = self.run_bzr(['info', '-v'])
126
self.assertEqual(error, '')
127
self.assertTrue("Standalone tree (format: git)" in output)
128
self.assertTrue("control: Local Git Repository" in output)
129
self.assertTrue("branch: Local Git Branch" in output)
130
self.assertTrue("repository: Git Repository" in output)
132
def test_push_roundtripping(self):
133
self.knownFailure("roundtripping is not yet supported")
134
self.with_roundtripping()
136
GitRepo.init(os.path.join(self.test_dir, "bla"))
137
self.run_bzr(['init', 'foo'])
138
self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
139
# when roundtripping is supported
140
output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
141
self.assertEqual(b"", output)
142
self.assertTrue(error.endswith(b"Created new branch.\n"))
145
# Smoke test for "bzr log" in a git repository.
148
# Check that bzr log does not fail and includes the revision.
149
output, error = self.run_bzr(['log'])
150
self.assertEqual(error, '')
152
'<The commit message>' in output,
153
"Commit message was not found in output:\n%s" % (output,))
155
def test_log_verbose(self):
156
# Smoke test for "bzr log -v" in a git repository.
159
# Check that bzr log does not fail and includes the revision.
160
output, error = self.run_bzr(['log', '-v'])
163
git_repo, commit_sha1 = self.simple_commit()
164
git_repo.refs[b"refs/tags/foo"] = commit_sha1
166
output, error = self.run_bzr(['tags'])
167
self.assertEqual(error, '')
168
self.assertEqual(output, "foo 1\n")
173
output, error = self.run_bzr(["tag", "bar"])
175
# bzr <= 2.2 emits this message in the output stream
176
# bzr => 2.3 emits this message in the error stream
177
self.assertEqual(error + output, 'Created tag bar.\n')
179
def test_init_repo(self):
180
output, error = self.run_bzr(["init", "--format=git", "bla.git"])
181
self.assertEqual(error, '')
182
self.assertEqual(output, 'Created a standalone tree (format: git)\n')
184
def test_diff_format(self):
185
tree = self.make_branch_and_tree('.')
186
self.build_tree(['a'])
188
output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
189
self.assertEqual(error, '')
190
self.assertEqual(output,
191
'diff --git /dev/null b/a\n'
194
'index 0000000..c197bd8 100644\n'
200
def test_git_import_uncolocated(self):
201
r = GitRepo.init("a", mkdir=True)
202
self.build_tree(["a/file"])
204
r.do_commit(ref=b"refs/heads/abranch",
205
committer=b"Joe <joe@example.com>", message=b"Dummy")
206
r.do_commit(ref=b"refs/heads/bbranch",
207
committer=b"Joe <joe@example.com>", message=b"Dummy")
208
self.run_bzr(["git-import", "a", "b"])
210
set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
212
def test_git_import(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
r.do_commit(ref=b"refs/heads/bbranch",
219
committer=b"Joe <joe@example.com>", message=b"Dummy")
220
self.run_bzr(["git-import", "--colocated", "a", "b"])
221
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
222
self.assertEqual(set(["abranch", "bbranch"]),
223
set(ControlDir.open("b").get_branches().keys()))
225
def test_git_import_incremental(self):
226
r = GitRepo.init("a", mkdir=True)
227
self.build_tree(["a/file"])
229
r.do_commit(ref=b"refs/heads/abranch",
230
committer=b"Joe <joe@example.com>", message=b"Dummy")
231
self.run_bzr(["git-import", "--colocated", "a", "b"])
232
self.run_bzr(["git-import", "--colocated", "a", "b"])
233
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
234
b = ControlDir.open("b")
235
self.assertEqual(["abranch"], list(b.get_branches().keys()))
237
def test_git_import_tags(self):
238
r = GitRepo.init("a", mkdir=True)
239
self.build_tree(["a/file"])
241
cid = r.do_commit(ref=b"refs/heads/abranch",
242
committer=b"Joe <joe@example.com>", message=b"Dummy")
243
r[b"refs/tags/atag"] = cid
244
self.run_bzr(["git-import", "--colocated", "a", "b"])
245
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
246
b = ControlDir.open("b")
247
self.assertEqual(["abranch"], list(b.get_branches().keys()))
248
self.assertEqual(["atag"],
249
list(b.open_branch("abranch").tags.get_tag_dict().keys()))
251
def test_git_import_colo(self):
252
r = GitRepo.init("a", mkdir=True)
253
self.build_tree(["a/file"])
255
r.do_commit(ref=b"refs/heads/abranch",
256
committer=b"Joe <joe@example.com>", message=b"Dummy")
257
r.do_commit(ref=b"refs/heads/bbranch",
258
committer=b"Joe <joe@example.com>", message=b"Dummy")
259
self.make_controldir("b", format="development-colo")
260
self.run_bzr(["git-import", "--colocated", "a", "b"])
262
set([b.name for b in ControlDir.open("b").list_branches()]),
263
set(["abranch", "bbranch"]))
265
def test_git_refs_from_git(self):
266
r = GitRepo.init("a", mkdir=True)
267
self.build_tree(["a/file"])
269
cid = r.do_commit(ref=b"refs/heads/abranch",
270
committer=b"Joe <joe@example.com>", message=b"Dummy")
271
r[b"refs/tags/atag"] = cid
272
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
273
self.assertEqual(stderr, "")
274
self.assertEqual(stdout,
275
'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
276
'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
278
def test_git_refs_from_bzr(self):
279
tree = self.make_branch_and_tree('a')
280
self.build_tree(["a/file"])
283
committer=b"Joe <joe@example.com>", message=b"Dummy")
284
tree.branch.tags.set_tag("atag", revid)
285
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
286
self.assertEqual(stderr, "")
287
self.assertTrue("refs/tags/atag -> " in stdout)
288
self.assertTrue("HEAD -> " in stdout)
290
def test_check(self):
291
r = GitRepo.init("gitr", mkdir=True)
292
self.build_tree_contents([("gitr/foo", b"hello from git")])
294
r.do_commit(b"message", committer=b"Somebody <user@example.com>")
295
out, err = self.run_bzr(["check", "gitr"])
297
self.assertEqual(out, '')
298
self.assertTrue(err.endswith, '3 objects\n')
301
class ShallowTests(ExternalBase):
304
super(ShallowTests, self).setUp()
305
# Smoke test for "bzr log" in a git repository with shallow depth.
306
self.repo = GitRepo.init('gitr', mkdir=True)
307
self.build_tree_contents([("gitr/foo", b"hello from git")])
308
self.repo.stage("foo")
310
b"message", committer=b"Somebody <user@example.com>",
311
commit_timestamp=1526330165, commit_timezone=0,
312
author_timestamp=1526330165, author_timezone=0,
313
merge_heads=[b'aa' * 20])
315
def test_log_shallow(self):
316
# Check that bzr log does not fail and includes the revision.
317
output, error = self.run_bzr(['log', 'gitr'], retcode=3)
319
error, 'brz: ERROR: Further revision history missing.\n')
320
self.assertEqual(output,
321
'------------------------------------------------------------\n'
322
'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
323
'git commit: ' + self.repo.head().decode('ascii') + '\n'
324
'committer: Somebody <user@example.com>\n'
325
'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
329
def test_version_info_rio(self):
330
output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
331
self.assertEqual(error, '')
332
self.assertNotIn('revno:', output)
334
def test_version_info_python(self):
335
output, error = self.run_bzr(['version-info', '--python', 'gitr'])
336
self.assertEqual(error, '')
337
self.assertNotIn('revno:', output)
339
def test_version_info_custom_with_revno(self):
340
output, error = self.run_bzr(
341
['version-info', '--custom',
342
'--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
344
error, 'brz: ERROR: Variable {revno} is not available.\n')
345
self.assertEqual(output, 'VERSION_INFO r')
347
def test_version_info_custom_without_revno(self):
348
output, error = self.run_bzr(
349
['version-info', '--custom', '--template=VERSION_INFO \n',
351
self.assertEqual(error, '')
352
self.assertEqual(output, 'VERSION_INFO \n')
355
class SwitchTests(ExternalBase):
357
def test_switch_branch(self):
358
# Create a git repository with a revision.
359
repo = GitRepo.init(self.test_dir)
360
builder = tests.GitBranchBuilder()
361
builder.set_branch(b'refs/heads/oldbranch')
362
builder.set_file('a', b'text for a\n', False)
363
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
364
builder.set_branch(b'refs/heads/newbranch')
366
builder.set_file('a', b'text for new a\n', False)
367
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
370
repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
374
output, error = self.run_bzr('switch oldbranch')
375
self.assertEqual(output, '')
376
self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
378
self.assertFileEqual("text for a\n", 'a')
379
tree = WorkingTree.open('.')
380
with tree.lock_read():
381
basis_tree = tree.basis_tree()
382
with basis_tree.lock_read():
383
self.assertEqual([], list(tree.iter_changes(basis_tree)))
386
class GrepTests(ExternalBase):
388
def test_simple_grep(self):
389
self.requireFeature(PluginLoadedFeature('grep'))
390
tree = self.make_branch_and_tree('.', format='git')
391
self.build_tree_contents([('a', 'text for a\n')])
393
output, error = self.run_bzr('grep text')
394
self.assertEqual(output, 'a:text for a\n')
395
self.assertEqual(error, '')
398
class ReconcileTests(ExternalBase):
400
def test_simple_reconcile(self):
401
tree = self.make_branch_and_tree('.', format='git')
402
self.build_tree_contents([('a', 'text for a\n')])
404
output, error = self.run_bzr('reconcile')
405
self.assertContainsRe(
407
'Reconciling branch file://.*\n'
408
'Reconciling repository file://.*\n'
409
'Reconciliation complete.\n')
410
self.assertEqual(error, '')
413
class StatusTests(ExternalBase):
415
def test_empty_dir(self):
416
tree = self.make_branch_and_tree('.', format='git')
417
self.build_tree(['a/', 'a/foo'])
418
self.build_tree_contents([('.gitignore', 'foo\n')])
419
tree.add(['.gitignore'])
420
tree.commit('add ignore')
421
output, error = self.run_bzr('st')
422
self.assertEqual(output, '')
423
self.assertEqual(error, '')
426
class StatsTests(ExternalBase):
428
def test_simple_stats(self):
429
self.requireFeature(PluginLoadedFeature('stats'))
430
tree = self.make_branch_and_tree('.', format='git')
431
self.build_tree_contents([('a', 'text for a\n')])
433
tree.commit('a commit', committer='Somebody <somebody@example.com>')
434
output, error = self.run_bzr('stats')
435
self.assertEqual(output, ' 1 Somebody <somebody@example.com>\n')
438
class GitObjectsTests(ExternalBase):
440
def run_simple(self, format):
441
tree = self.make_branch_and_tree('.', format=format)
442
self.build_tree(['a/', 'a/foo'])
445
output, error = self.run_bzr('git-objects')
446
shas = list(output.splitlines())
447
self.assertEqual([40, 40], [len(s) for s in shas])
448
self.assertEqual(error, '')
450
output, error = self.run_bzr('git-object %s' % shas[0])
451
self.assertEqual('', error)
453
def test_in_native(self):
454
self.run_simple(format='git')
456
def test_in_bzr(self):
457
self.run_simple(format='2a')