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.script import TestCaseWithTransportAndScript
39
from ...tests.features import PluginLoadedFeature
42
class TestGitBlackBox(ExternalBase):
44
def simple_commit(self):
45
# Create a git repository with a revision.
46
repo = GitRepo.init(self.test_dir)
47
builder = tests.GitBranchBuilder()
48
builder.set_file('a', b'text for a\n', False)
49
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
50
return repo, builder.finish()[r1]
53
r = GitRepo.init(self.test_dir)
54
dir = ControlDir.open(self.test_dir)
56
output, error = self.run_bzr(['nick'])
57
self.assertEqual("master\n", output)
59
def test_branches(self):
61
output, error = self.run_bzr(['branches'])
62
self.assertEqual("* master\n", output)
66
output, error = self.run_bzr(['info'])
67
self.assertEqual(error, '')
70
'Standalone tree (format: git)\n'
72
' light checkout root: .\n'
73
' checkout of co-located branch: master\n')
75
def test_ignore(self):
77
output, error = self.run_bzr(['ignore', 'foo'])
78
self.assertEqual(error, '')
79
self.assertEqual(output, '')
80
self.assertFileEqual("foo\n", ".gitignore")
82
def test_cat_revision(self):
84
output, error = self.run_bzr(['cat-revision', '-r-1'], retcode=3)
85
self.assertContainsRe(
87
'brz: ERROR: Repository .* does not support access to raw '
89
self.assertEqual(output, '')
91
def test_branch(self):
93
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
95
builder = tests.GitBranchBuilder()
96
builder.set_file(b'a', b'text for a\n', False)
97
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
101
output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
103
(error == 'Branched 1 revision(s).\n') or
104
(error == 'Branched 1 revision.\n'),
107
def test_checkout(self):
108
os.mkdir("gitbranch")
109
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
110
os.chdir('gitbranch')
111
builder = tests.GitBranchBuilder()
112
builder.set_file(b'a', b'text for a\n', False)
113
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
117
output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
118
self.assertEqual(error,
119
'Fetching from Git to Bazaar repository. '
120
'For better performance, fetch into a Git repository.\n')
121
self.assertEqual(output, '')
123
def test_branch_ls(self):
125
output, error = self.run_bzr(['ls', '-r-1'])
126
self.assertEqual(error, '')
127
self.assertEqual(output, "a\n")
130
self.run_bzr("init --format=git repo")
132
def test_info_verbose(self):
135
output, error = self.run_bzr(['info', '-v'])
136
self.assertEqual(error, '')
137
self.assertTrue("Standalone tree (format: git)" in output)
138
self.assertTrue("control: Local Git Repository" in output)
139
self.assertTrue("branch: Local Git Branch" in output)
140
self.assertTrue("repository: Git Repository" in output)
142
def test_push_roundtripping(self):
143
self.knownFailure("roundtripping is not yet supported")
144
self.with_roundtripping()
146
GitRepo.init(os.path.join(self.test_dir, "bla"))
147
self.run_bzr(['init', 'foo'])
148
self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
149
# when roundtripping is supported
150
output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
151
self.assertEqual(b"", output)
152
self.assertTrue(error.endswith(b"Created new branch.\n"))
155
# Smoke test for "bzr log" in a git repository.
158
# Check that bzr log does not fail and includes the revision.
159
output, error = self.run_bzr(['log'])
160
self.assertEqual(error, '')
162
'<The commit message>' in output,
163
"Commit message was not found in output:\n%s" % (output,))
165
def test_log_verbose(self):
166
# Smoke test for "bzr log -v" in a git repository.
169
# Check that bzr log does not fail and includes the revision.
170
output, error = self.run_bzr(['log', '-v'])
172
def test_log_file(self):
173
# Smoke test for "bzr log" in a git repository.
174
repo = GitRepo.init(self.test_dir)
175
builder = tests.GitBranchBuilder()
176
builder.set_file('a', b'text for a\n', False)
177
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'First')
178
builder.set_file('a', b'text 3a for a\n', False)
179
r2a = builder.commit(b'Joe Foo <joe@foo.com>', u'Second a', base=r1)
180
builder.set_file('a', b'text 3b for a\n', False)
181
r2b = builder.commit(b'Joe Foo <joe@foo.com>', u'Second b', base=r1)
182
builder.set_file('a', b'text 4 for a\n', False)
183
builder.commit(b'Joe Foo <joe@foo.com>', u'Third', merge=[r2a], base=r2b)
186
# Check that bzr log does not fail and includes the revision.
187
output, error = self.run_bzr(['log', '-n2', 'a'])
188
self.assertEqual(error, '')
189
self.assertIn('Second a', output)
190
self.assertIn('Second b', output)
191
self.assertIn('First', output)
192
self.assertIn('Third', output)
195
git_repo, commit_sha1 = self.simple_commit()
196
git_repo.refs[b"refs/tags/foo"] = commit_sha1
198
output, error = self.run_bzr(['tags'])
199
self.assertEqual(error, '')
200
self.assertEqual(output, "foo 1\n")
205
output, error = self.run_bzr(["tag", "bar"])
207
# bzr <= 2.2 emits this message in the output stream
208
# bzr => 2.3 emits this message in the error stream
209
self.assertEqual(error + output, 'Created tag bar.\n')
211
def test_init_repo(self):
212
output, error = self.run_bzr(["init", "--format=git", "bla.git"])
213
self.assertEqual(error, '')
214
self.assertEqual(output, 'Created a standalone tree (format: git)\n')
216
def test_diff_format(self):
217
tree = self.make_branch_and_tree('.')
218
self.build_tree(['a'])
220
output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
221
self.assertEqual(error, '')
222
# Some older versions of Dulwich (< 0.19.12) formatted diffs slightly
224
from dulwich import __version__ as dulwich_version
225
if dulwich_version < (0, 19, 12):
226
self.assertEqual(output,
227
'diff --git /dev/null b/a\n'
230
'index 0000000..c197bd8 100644\n'
236
self.assertEqual(output,
237
'diff --git a/a b/a\n'
239
'new file mode 100644\n'
240
'index 0000000..c197bd8 100644\n'
246
def test_git_import_uncolocated(self):
247
r = GitRepo.init("a", mkdir=True)
248
self.build_tree(["a/file"])
250
r.do_commit(ref=b"refs/heads/abranch",
251
committer=b"Joe <joe@example.com>", message=b"Dummy")
252
r.do_commit(ref=b"refs/heads/bbranch",
253
committer=b"Joe <joe@example.com>", message=b"Dummy")
254
self.run_bzr(["git-import", "a", "b"])
256
set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
258
def test_git_import(self):
259
r = GitRepo.init("a", mkdir=True)
260
self.build_tree(["a/file"])
262
r.do_commit(ref=b"refs/heads/abranch",
263
committer=b"Joe <joe@example.com>", message=b"Dummy")
264
r.do_commit(ref=b"refs/heads/bbranch",
265
committer=b"Joe <joe@example.com>", message=b"Dummy")
266
self.run_bzr(["git-import", "--colocated", "a", "b"])
267
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
268
self.assertEqual(set(["abranch", "bbranch"]),
269
set(ControlDir.open("b").get_branches().keys()))
271
def test_git_import_incremental(self):
272
r = GitRepo.init("a", mkdir=True)
273
self.build_tree(["a/file"])
275
r.do_commit(ref=b"refs/heads/abranch",
276
committer=b"Joe <joe@example.com>", message=b"Dummy")
277
self.run_bzr(["git-import", "--colocated", "a", "b"])
278
self.run_bzr(["git-import", "--colocated", "a", "b"])
279
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
280
b = ControlDir.open("b")
281
self.assertEqual(["abranch"], list(b.get_branches().keys()))
283
def test_git_import_tags(self):
284
r = GitRepo.init("a", mkdir=True)
285
self.build_tree(["a/file"])
287
cid = r.do_commit(ref=b"refs/heads/abranch",
288
committer=b"Joe <joe@example.com>", message=b"Dummy")
289
r[b"refs/tags/atag"] = cid
290
self.run_bzr(["git-import", "--colocated", "a", "b"])
291
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
292
b = ControlDir.open("b")
293
self.assertEqual(["abranch"], list(b.get_branches().keys()))
294
self.assertEqual(["atag"],
295
list(b.open_branch("abranch").tags.get_tag_dict().keys()))
297
def test_git_import_colo(self):
298
r = GitRepo.init("a", mkdir=True)
299
self.build_tree(["a/file"])
301
r.do_commit(ref=b"refs/heads/abranch",
302
committer=b"Joe <joe@example.com>", message=b"Dummy")
303
r.do_commit(ref=b"refs/heads/bbranch",
304
committer=b"Joe <joe@example.com>", message=b"Dummy")
305
self.make_controldir("b", format="development-colo")
306
self.run_bzr(["git-import", "--colocated", "a", "b"])
308
set([b.name for b in ControlDir.open("b").list_branches()]),
309
set(["abranch", "bbranch"]))
311
def test_git_refs_from_git(self):
312
r = GitRepo.init("a", mkdir=True)
313
self.build_tree(["a/file"])
315
cid = r.do_commit(ref=b"refs/heads/abranch",
316
committer=b"Joe <joe@example.com>", message=b"Dummy")
317
r[b"refs/tags/atag"] = cid
318
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
319
self.assertEqual(stderr, "")
320
self.assertEqual(stdout,
321
'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
322
'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
324
def test_git_refs_from_bzr(self):
325
tree = self.make_branch_and_tree('a')
326
self.build_tree(["a/file"])
329
committer=b"Joe <joe@example.com>", message=b"Dummy")
330
tree.branch.tags.set_tag("atag", revid)
331
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
332
self.assertEqual(stderr, "")
333
self.assertTrue("refs/tags/atag -> " in stdout)
334
self.assertTrue("HEAD -> " in stdout)
336
def test_check(self):
337
r = GitRepo.init("gitr", mkdir=True)
338
self.build_tree_contents([("gitr/foo", b"hello from git")])
340
r.do_commit(b"message", committer=b"Somebody <user@example.com>")
341
out, err = self.run_bzr(["check", "gitr"])
343
self.assertEqual(out, '')
344
self.assertTrue(err.endswith, '3 objects\n')
347
class ShallowTests(ExternalBase):
350
super(ShallowTests, self).setUp()
351
# Smoke test for "bzr log" in a git repository with shallow depth.
352
self.repo = GitRepo.init('gitr', mkdir=True)
353
self.build_tree_contents([("gitr/foo", b"hello from git")])
354
self.repo.stage("foo")
356
b"message", committer=b"Somebody <user@example.com>",
357
author=b"Somebody <user@example.com>",
358
commit_timestamp=1526330165, commit_timezone=0,
359
author_timestamp=1526330165, author_timezone=0,
360
merge_heads=[b'aa' * 20])
362
def test_log_shallow(self):
363
# Check that bzr log does not fail and includes the revision.
364
output, error = self.run_bzr(['log', 'gitr'], retcode=3)
366
error, 'brz: ERROR: Further revision history missing.\n')
367
self.assertEqual(output,
368
'------------------------------------------------------------\n'
369
'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
370
'git commit: ' + self.repo.head().decode('ascii') + '\n'
371
'committer: Somebody <user@example.com>\n'
372
'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
376
def test_version_info_rio(self):
377
output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
378
self.assertEqual(error, '')
379
self.assertNotIn('revno:', output)
381
def test_version_info_python(self):
382
output, error = self.run_bzr(['version-info', '--python', 'gitr'])
383
self.assertEqual(error, '')
384
self.assertNotIn('revno:', output)
386
def test_version_info_custom_with_revno(self):
387
output, error = self.run_bzr(
388
['version-info', '--custom',
389
'--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
391
error, 'brz: ERROR: Variable {revno} is not available.\n')
392
self.assertEqual(output, 'VERSION_INFO r')
394
def test_version_info_custom_without_revno(self):
395
output, error = self.run_bzr(
396
['version-info', '--custom', '--template=VERSION_INFO \n',
398
self.assertEqual(error, '')
399
self.assertEqual(output, 'VERSION_INFO \n')
402
class SwitchTests(ExternalBase):
404
def test_switch_branch(self):
405
# Create a git repository with a revision.
406
repo = GitRepo.init(self.test_dir)
407
builder = tests.GitBranchBuilder()
408
builder.set_branch(b'refs/heads/oldbranch')
409
builder.set_file('a', b'text for a\n', False)
410
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
411
builder.set_branch(b'refs/heads/newbranch')
413
builder.set_file('a', b'text for new a\n', False)
414
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
417
repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
421
output, error = self.run_bzr('switch oldbranch')
422
self.assertEqual(output, '')
423
self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
425
self.assertFileEqual("text for a\n", 'a')
426
tree = WorkingTree.open('.')
427
with tree.lock_read():
428
basis_tree = tree.basis_tree()
429
with basis_tree.lock_read():
430
self.assertEqual([], list(tree.iter_changes(basis_tree)))
433
class SwitchScriptTests(TestCaseWithTransportAndScript):
435
def test_switch_preserves(self):
436
# See https://bugs.launchpad.net/brz/+bug/1820606
439
Created a standalone tree (format: git)
441
$ echo original > file.txt
444
$ brz ci -q -m "Initial"
445
$ echo "entered on master branch" > file.txt
449
$ brz switch -b other
450
2>Tree is up to date at revision 1.
451
2>Switched to branch other
453
entered on master branch
457
class GrepTests(ExternalBase):
459
def test_simple_grep(self):
460
tree = self.make_branch_and_tree('.', format='git')
461
self.build_tree_contents([('a', 'text for a\n')])
463
output, error = self.run_bzr('grep text')
464
self.assertEqual(output, 'a:text for a\n')
465
self.assertEqual(error, '')
468
class ReconcileTests(ExternalBase):
470
def test_simple_reconcile(self):
471
tree = self.make_branch_and_tree('.', format='git')
472
self.build_tree_contents([('a', 'text for a\n')])
474
output, error = self.run_bzr('reconcile')
475
self.assertContainsRe(
477
'Reconciling branch file://.*\n'
478
'Reconciling repository file://.*\n'
479
'Reconciliation complete.\n')
480
self.assertEqual(error, '')
483
class StatusTests(ExternalBase):
485
def test_empty_dir(self):
486
tree = self.make_branch_and_tree('.', format='git')
487
self.build_tree(['a/', 'a/foo'])
488
self.build_tree_contents([('.gitignore', 'foo\n')])
489
tree.add(['.gitignore'])
490
tree.commit('add ignore')
491
output, error = self.run_bzr('st')
492
self.assertEqual(output, '')
493
self.assertEqual(error, '')
496
class StatsTests(ExternalBase):
498
def test_simple_stats(self):
499
self.requireFeature(PluginLoadedFeature('stats'))
500
tree = self.make_branch_and_tree('.', format='git')
501
self.build_tree_contents([('a', 'text for a\n')])
503
tree.commit('a commit', committer='Somebody <somebody@example.com>')
504
output, error = self.run_bzr('stats')
505
self.assertEqual(output, ' 1 Somebody <somebody@example.com>\n')
508
class GitObjectsTests(ExternalBase):
510
def run_simple(self, format):
511
tree = self.make_branch_and_tree('.', format=format)
512
self.build_tree(['a/', 'a/foo'])
515
output, error = self.run_bzr('git-objects')
516
shas = list(output.splitlines())
517
self.assertEqual([40, 40], [len(s) for s in shas])
518
self.assertEqual(error, '')
520
output, error = self.run_bzr('git-object %s' % shas[0])
521
self.assertEqual('', error)
523
def test_in_native(self):
524
self.run_simple(format='git')
526
def test_in_bzr(self):
527
self.run_simple(format='2a')
530
class GitApplyTests(ExternalBase):
532
def test_apply(self):
533
b = self.make_branch_and_tree('.')
535
with open('foo.patch', 'w') as f:
537
From bdefb25fab801e6af0a70e965f60cb48f2b759fa Mon Sep 17 00:00:00 2001
538
From: Dmitry Bogatov <KAction@debian.org>
539
Date: Fri, 8 Feb 2019 23:28:30 +0000
540
Subject: [PATCH] Add fixed for out-of-date-standards-version
544
1 files changed, 14 insertions(+)
545
create mode 100644 message
547
diff --git a/message b/message
549
index 0000000..05ec0b1
553
+Update standards version, no changes needed.
555
+Fixed-Lintian-Tags: out-of-date-standards-version
557
output, error = self.run_bzr('git-apply foo.patch')
558
self.assertContainsRe(
560
'Committing to: .*\n'
561
'Committed revision 1.\n')