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_ignore(self):
70
output, error = self.run_bzr(['ignore', 'foo'])
71
self.assertEqual(error, '')
72
self.assertEqual(output, '')
73
self.assertFileEqual("foo\n", ".gitignore")
75
def test_branch(self):
77
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
79
builder = tests.GitBranchBuilder()
80
builder.set_file(b'a', b'text for a\n', False)
81
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
85
output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
87
(error == 'Branched 1 revision(s).\n') or
88
(error == 'Branched 1 revision.\n'),
91
def test_checkout(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(['checkout', 'gitbranch', 'bzrbranch'])
102
self.assertEqual(error,
103
'Fetching from Git to Bazaar repository. '
104
'For better performance, fetch into a Git repository.\n')
105
self.assertEqual(output, '')
107
def test_branch_ls(self):
109
output, error = self.run_bzr(['ls', '-r-1'])
110
self.assertEqual(error, '')
111
self.assertEqual(output, "a\n")
114
self.run_bzr("init --format=git repo")
116
def test_info_verbose(self):
119
output, error = self.run_bzr(['info', '-v'])
120
self.assertEqual(error, '')
121
self.assertTrue("Standalone tree (format: git)" in output)
122
self.assertTrue("control: Local Git Repository" in output)
123
self.assertTrue("branch: Local Git Branch" in output)
124
self.assertTrue("repository: Git Repository" in output)
126
def test_push_roundtripping(self):
127
self.knownFailure("roundtripping is not yet supported")
128
self.with_roundtripping()
130
GitRepo.init(os.path.join(self.test_dir, "bla"))
131
self.run_bzr(['init', 'foo'])
132
self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
133
# when roundtripping is supported
134
output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
135
self.assertEqual(b"", output)
136
self.assertTrue(error.endswith(b"Created new branch.\n"))
139
# Smoke test for "bzr log" in a git repository.
142
# Check that bzr log does not fail and includes the revision.
143
output, error = self.run_bzr(['log'])
144
self.assertEqual(error, '')
146
'<The commit message>' in output,
147
"Commit message was not found in output:\n%s" % (output,))
149
def test_log_verbose(self):
150
# Smoke test for "bzr log -v" in a git repository.
153
# Check that bzr log does not fail and includes the revision.
154
output, error = self.run_bzr(['log', '-v'])
157
git_repo, commit_sha1 = self.simple_commit()
158
git_repo.refs[b"refs/tags/foo"] = commit_sha1
160
output, error = self.run_bzr(['tags'])
161
self.assertEqual(error, '')
162
self.assertEqual(output, "foo 1\n")
167
output, error = self.run_bzr(["tag", "bar"])
169
# bzr <= 2.2 emits this message in the output stream
170
# bzr => 2.3 emits this message in the error stream
171
self.assertEqual(error + output, 'Created tag bar.\n')
173
def test_init_repo(self):
174
output, error = self.run_bzr(["init", "--format=git", "bla.git"])
175
self.assertEqual(error, '')
176
self.assertEqual(output, 'Created a standalone tree (format: git)\n')
178
def test_diff_format(self):
179
tree = self.make_branch_and_tree('.')
180
self.build_tree(['a'])
182
output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
183
self.assertEqual(error, '')
184
self.assertEqual(output,
185
'diff --git /dev/null b/a\n'
188
'index 0000000..c197bd8 100644\n'
194
def test_git_import_uncolocated(self):
195
r = GitRepo.init("a", mkdir=True)
196
self.build_tree(["a/file"])
198
r.do_commit(ref=b"refs/heads/abranch",
199
committer=b"Joe <joe@example.com>", message=b"Dummy")
200
r.do_commit(ref=b"refs/heads/bbranch",
201
committer=b"Joe <joe@example.com>", message=b"Dummy")
202
self.run_bzr(["git-import", "a", "b"])
204
set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
206
def test_git_import(self):
207
r = GitRepo.init("a", mkdir=True)
208
self.build_tree(["a/file"])
210
r.do_commit(ref=b"refs/heads/abranch",
211
committer=b"Joe <joe@example.com>", message=b"Dummy")
212
r.do_commit(ref=b"refs/heads/bbranch",
213
committer=b"Joe <joe@example.com>", message=b"Dummy")
214
self.run_bzr(["git-import", "--colocated", "a", "b"])
215
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
216
self.assertEqual(set(["abranch", "bbranch"]),
217
set(ControlDir.open("b").get_branches().keys()))
219
def test_git_import_incremental(self):
220
r = GitRepo.init("a", mkdir=True)
221
self.build_tree(["a/file"])
223
r.do_commit(ref=b"refs/heads/abranch",
224
committer=b"Joe <joe@example.com>", message=b"Dummy")
225
self.run_bzr(["git-import", "--colocated", "a", "b"])
226
self.run_bzr(["git-import", "--colocated", "a", "b"])
227
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
228
b = ControlDir.open("b")
229
self.assertEqual(["abranch"], list(b.get_branches().keys()))
231
def test_git_import_tags(self):
232
r = GitRepo.init("a", mkdir=True)
233
self.build_tree(["a/file"])
235
cid = r.do_commit(ref=b"refs/heads/abranch",
236
committer=b"Joe <joe@example.com>", message=b"Dummy")
237
r[b"refs/tags/atag"] = cid
238
self.run_bzr(["git-import", "--colocated", "a", "b"])
239
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
240
b = ControlDir.open("b")
241
self.assertEqual(["abranch"], list(b.get_branches().keys()))
242
self.assertEqual(["atag"],
243
list(b.open_branch("abranch").tags.get_tag_dict().keys()))
245
def test_git_import_colo(self):
246
r = GitRepo.init("a", mkdir=True)
247
self.build_tree(["a/file"])
249
r.do_commit(ref=b"refs/heads/abranch",
250
committer=b"Joe <joe@example.com>", message=b"Dummy")
251
r.do_commit(ref=b"refs/heads/bbranch",
252
committer=b"Joe <joe@example.com>", message=b"Dummy")
253
self.make_controldir("b", format="development-colo")
254
self.run_bzr(["git-import", "--colocated", "a", "b"])
256
set([b.name for b in ControlDir.open("b").list_branches()]),
257
set(["abranch", "bbranch"]))
259
def test_git_refs_from_git(self):
260
r = GitRepo.init("a", mkdir=True)
261
self.build_tree(["a/file"])
263
cid = r.do_commit(ref=b"refs/heads/abranch",
264
committer=b"Joe <joe@example.com>", message=b"Dummy")
265
r[b"refs/tags/atag"] = cid
266
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
267
self.assertEqual(stderr, "")
268
self.assertEqual(stdout,
269
'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
270
'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
272
def test_git_refs_from_bzr(self):
273
tree = self.make_branch_and_tree('a')
274
self.build_tree(["a/file"])
277
committer=b"Joe <joe@example.com>", message=b"Dummy")
278
tree.branch.tags.set_tag("atag", revid)
279
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
280
self.assertEqual(stderr, "")
281
self.assertTrue("refs/tags/atag -> " in stdout)
282
self.assertTrue("HEAD -> " in stdout)
284
def test_check(self):
285
r = GitRepo.init("gitr", mkdir=True)
286
self.build_tree_contents([("gitr/foo", b"hello from git")])
288
r.do_commit(b"message", committer=b"Somebody <user@example.com>")
289
out, err = self.run_bzr(["check", "gitr"])
291
self.assertEqual(out, '')
292
self.assertTrue(err.endswith, '3 objects\n')
295
class ShallowTests(ExternalBase):
298
super(ShallowTests, self).setUp()
299
# Smoke test for "bzr log" in a git repository with shallow depth.
300
self.repo = GitRepo.init('gitr', mkdir=True)
301
self.build_tree_contents([("gitr/foo", b"hello from git")])
302
self.repo.stage("foo")
304
b"message", committer=b"Somebody <user@example.com>",
305
commit_timestamp=1526330165, commit_timezone=0,
306
author_timestamp=1526330165, author_timezone=0,
307
merge_heads=[b'aa' * 20])
309
def test_log_shallow(self):
310
# Check that bzr log does not fail and includes the revision.
311
output, error = self.run_bzr(['log', 'gitr'], retcode=3)
313
error, 'brz: ERROR: Further revision history missing.\n')
314
self.assertEqual(output,
315
'------------------------------------------------------------\n'
316
'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
317
'git commit: ' + self.repo.head().decode('ascii') + '\n'
318
'committer: Somebody <user@example.com>\n'
319
'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
323
def test_version_info_rio(self):
324
output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
325
self.assertEqual(error, '')
326
self.assertNotIn('revno:', output)
328
def test_version_info_python(self):
329
output, error = self.run_bzr(['version-info', '--python', 'gitr'])
330
self.assertEqual(error, '')
331
self.assertNotIn('revno:', output)
333
def test_version_info_custom_with_revno(self):
334
output, error = self.run_bzr(
335
['version-info', '--custom',
336
'--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
338
error, 'brz: ERROR: Variable {revno} is not available.\n')
339
self.assertEqual(output, 'VERSION_INFO r')
341
def test_version_info_custom_without_revno(self):
342
output, error = self.run_bzr(
343
['version-info', '--custom', '--template=VERSION_INFO \n',
345
self.assertEqual(error, '')
346
self.assertEqual(output, 'VERSION_INFO \n')
349
class SwitchTests(ExternalBase):
351
def test_switch_branch(self):
352
# Create a git repository with a revision.
353
repo = GitRepo.init(self.test_dir)
354
builder = tests.GitBranchBuilder()
355
builder.set_branch(b'refs/heads/oldbranch')
356
builder.set_file('a', b'text for a\n', False)
357
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
358
builder.set_branch(b'refs/heads/newbranch')
360
builder.set_file('a', b'text for new a\n', False)
361
builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
364
repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
368
output, error = self.run_bzr('switch oldbranch')
369
self.assertEqual(output, '')
370
self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
372
self.assertFileEqual("text for a\n", 'a')
373
tree = WorkingTree.open('.')
374
with tree.lock_read():
375
basis_tree = tree.basis_tree()
376
with basis_tree.lock_read():
377
self.assertEqual([], list(tree.iter_changes(basis_tree)))
380
class GrepTests(ExternalBase):
382
def test_simple_grep(self):
383
tree = self.make_branch_and_tree('.', format='git')
384
self.build_tree_contents([('a', 'text for a\n')])
386
output, error = self.run_bzr('grep text')
387
self.assertEqual(output, 'a:text for a\n')
388
self.assertEqual(error, '')