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
39
class TestGitBlackBox(ExternalBase):
41
def simple_commit(self):
42
# Create a git repository with a revision.
43
repo = GitRepo.init(self.test_dir)
44
builder = tests.GitBranchBuilder()
45
builder.set_file('a', b'text for a\n', False)
46
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
47
return repo, builder.finish()[r1]
50
r = GitRepo.init(self.test_dir)
51
dir = ControlDir.open(self.test_dir)
53
output, error = self.run_bzr(['nick'])
54
self.assertEqual("master\n", output)
56
def test_branches(self):
58
output, error = self.run_bzr(['branches'])
59
self.assertEqual("* master\n", output)
63
output, error = self.run_bzr(['info'])
64
self.assertEqual(error, '')
65
self.assertTrue("Standalone tree (format: git)" in output)
67
def test_branch(self):
69
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
71
builder = tests.GitBranchBuilder()
72
builder.set_file(b'a', b'text for a\n', False)
73
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
77
output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
79
(error == 'Branched 1 revision(s).\n') or
80
(error == 'Branched 1 revision.\n'),
83
def test_checkout(self):
85
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
87
builder = tests.GitBranchBuilder()
88
builder.set_file(b'a', b'text for a\n', False)
89
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
93
output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
94
self.assertEqual(error,
95
'Fetching from Git to Bazaar repository. '
96
'For better performance, fetch into a Git repository.\n')
97
self.assertEqual(output, '')
99
def test_branch_ls(self):
101
output, error = self.run_bzr(['ls', '-r-1'])
102
self.assertEqual(error, '')
103
self.assertEqual(output, "a\n")
106
self.run_bzr("init --format=git repo")
108
def test_info_verbose(self):
111
output, error = self.run_bzr(['info', '-v'])
112
self.assertEqual(error, '')
113
self.assertTrue("Standalone tree (format: git)" in output)
114
self.assertTrue("control: Local Git Repository" in output)
115
self.assertTrue("branch: Local Git Branch" in output)
116
self.assertTrue("repository: Git Repository" in output)
118
def test_push_roundtripping(self):
119
self.knownFailure("roundtripping is not yet supported")
120
self.with_roundtripping()
122
GitRepo.init(os.path.join(self.test_dir, "bla"))
123
self.run_bzr(['init', 'foo'])
124
self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
125
# when roundtripping is supported
126
output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
127
self.assertEqual(b"", output)
128
self.assertTrue(error.endswith(b"Created new branch.\n"))
131
# Smoke test for "bzr log" in a git repository.
134
# Check that bzr log does not fail and includes the revision.
135
output, error = self.run_bzr(['log'])
136
self.assertEqual(error, '')
138
'<The commit message>' in output,
139
"Commit message was not found in output:\n%s" % (output,))
141
def test_log_verbose(self):
142
# Smoke test for "bzr log -v" in a git repository.
145
# Check that bzr log does not fail and includes the revision.
146
output, error = self.run_bzr(['log', '-v'])
149
git_repo, commit_sha1 = self.simple_commit()
150
git_repo.refs[b"refs/tags/foo"] = commit_sha1
152
output, error = self.run_bzr(['tags'])
153
self.assertEqual(error, '')
154
self.assertEqual(output, "foo 1\n")
159
output, error = self.run_bzr(["tag", "bar"])
161
# bzr <= 2.2 emits this message in the output stream
162
# bzr => 2.3 emits this message in the error stream
163
self.assertEqual(error + output, 'Created tag bar.\n')
165
def test_init_repo(self):
166
output, error = self.run_bzr(["init", "--format=git", "bla.git"])
167
self.assertEqual(error, '')
168
self.assertEqual(output, 'Created a standalone tree (format: git)\n')
170
def test_diff_format(self):
171
tree = self.make_branch_and_tree('.')
172
self.build_tree(['a'])
174
output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
175
self.assertEqual(error, '')
176
self.assertEqual(output,
177
'diff --git /dev/null b/a\n'
180
'index 0000000..c197bd8 100644\n'
186
def test_git_import_uncolocated(self):
187
r = GitRepo.init("a", mkdir=True)
188
self.build_tree(["a/file"])
190
r.do_commit(ref=b"refs/heads/abranch",
191
committer=b"Joe <joe@example.com>", message=b"Dummy")
192
r.do_commit(ref=b"refs/heads/bbranch",
193
committer=b"Joe <joe@example.com>", message=b"Dummy")
194
self.run_bzr(["git-import", "a", "b"])
196
set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
198
def test_git_import(self):
199
r = GitRepo.init("a", mkdir=True)
200
self.build_tree(["a/file"])
202
r.do_commit(ref=b"refs/heads/abranch",
203
committer=b"Joe <joe@example.com>", message=b"Dummy")
204
r.do_commit(ref=b"refs/heads/bbranch",
205
committer=b"Joe <joe@example.com>", message=b"Dummy")
206
self.run_bzr(["git-import", "--colocated", "a", "b"])
207
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
208
self.assertEqual(set(["abranch", "bbranch"]),
209
set(ControlDir.open("b").get_branches().keys()))
211
def test_git_import_incremental(self):
212
r = GitRepo.init("a", mkdir=True)
213
self.build_tree(["a/file"])
215
r.do_commit(ref=b"refs/heads/abranch",
216
committer=b"Joe <joe@example.com>", message=b"Dummy")
217
self.run_bzr(["git-import", "--colocated", "a", "b"])
218
self.run_bzr(["git-import", "--colocated", "a", "b"])
219
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
220
b = ControlDir.open("b")
221
self.assertEqual(["abranch"], list(b.get_branches().keys()))
223
def test_git_import_tags(self):
224
r = GitRepo.init("a", mkdir=True)
225
self.build_tree(["a/file"])
227
cid = r.do_commit(ref=b"refs/heads/abranch",
228
committer=b"Joe <joe@example.com>", message=b"Dummy")
229
r[b"refs/tags/atag"] = cid
230
self.run_bzr(["git-import", "--colocated", "a", "b"])
231
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
232
b = ControlDir.open("b")
233
self.assertEqual(["abranch"], list(b.get_branches().keys()))
234
self.assertEqual(["atag"],
235
list(b.open_branch("abranch").tags.get_tag_dict().keys()))
237
def test_git_import_colo(self):
238
r = GitRepo.init("a", mkdir=True)
239
self.build_tree(["a/file"])
241
r.do_commit(ref=b"refs/heads/abranch",
242
committer=b"Joe <joe@example.com>", message=b"Dummy")
243
r.do_commit(ref=b"refs/heads/bbranch",
244
committer=b"Joe <joe@example.com>", message=b"Dummy")
245
self.make_controldir("b", format="development-colo")
246
self.run_bzr(["git-import", "--colocated", "a", "b"])
248
set([b.name for b in ControlDir.open("b").list_branches()]),
249
set(["abranch", "bbranch"]))
251
def test_git_refs_from_git(self):
252
r = GitRepo.init("a", mkdir=True)
253
self.build_tree(["a/file"])
255
cid = r.do_commit(ref=b"refs/heads/abranch",
256
committer=b"Joe <joe@example.com>", message=b"Dummy")
257
r[b"refs/tags/atag"] = cid
258
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
259
self.assertEqual(stderr, "")
260
self.assertEqual(stdout,
261
'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
262
'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
264
def test_git_refs_from_bzr(self):
265
tree = self.make_branch_and_tree('a')
266
self.build_tree(["a/file"])
269
committer=b"Joe <joe@example.com>", message=b"Dummy")
270
tree.branch.tags.set_tag("atag", revid)
271
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
272
self.assertEqual(stderr, "")
273
self.assertTrue("refs/tags/atag -> " in stdout)
274
self.assertTrue("HEAD -> " in stdout)
276
def test_check(self):
277
r = GitRepo.init("gitr", mkdir=True)
278
self.build_tree_contents([("gitr/foo", b"hello from git")])
280
r.do_commit(b"message", committer=b"Somebody <user@example.com>")
281
out, err = self.run_bzr(["check", "gitr"])
283
self.assertEqual(out, '')
284
self.assertTrue(err.endswith, '3 objects\n')
287
class ShallowTests(ExternalBase):
290
super(ShallowTests, self).setUp()
291
# Smoke test for "bzr log" in a git repository with shallow depth.
292
self.repo = GitRepo.init('gitr', mkdir=True)
293
self.build_tree_contents([("gitr/foo", b"hello from git")])
294
self.repo.stage("foo")
296
b"message", committer=b"Somebody <user@example.com>",
297
commit_timestamp=1526330165, commit_timezone=0,
298
author_timestamp=1526330165, author_timezone=0,
299
merge_heads=[b'aa' * 20])
301
def test_log_shallow(self):
302
# Check that bzr log does not fail and includes the revision.
303
output, error = self.run_bzr(['log', 'gitr'], retcode=3)
305
error, 'brz: ERROR: Further revision history missing.\n')
306
self.assertEqual(output,
307
'------------------------------------------------------------\n'
308
'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
309
'git commit: ' + self.repo.head().decode('ascii') + '\n'
310
'committer: Somebody <user@example.com>\n'
311
'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
315
def test_version_info_rio(self):
316
output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
317
self.assertEqual(error, '')
318
self.assertNotIn('revno:', output)
320
def test_version_info_python(self):
321
output, error = self.run_bzr(['version-info', '--python', 'gitr'])
322
self.assertEqual(error, '')
323
self.assertNotIn('revno:', output)
325
def test_version_info_custom_with_revno(self):
326
output, error = self.run_bzr(
327
['version-info', '--custom',
328
'--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
330
error, 'brz: ERROR: Variable {revno} is not available.\n')
331
self.assertEqual(output, 'VERSION_INFO r')
333
def test_version_info_custom_without_revno(self):
334
output, error = self.run_bzr(
335
['version-info', '--custom', '--template=VERSION_INFO \n',
337
self.assertEqual(error, '')
338
self.assertEqual(output, 'VERSION_INFO \n')
341
class GrepTests(ExternalBase):
343
def test_simple_grep(self):
344
tree = self.make_branch_and_tree('.', format='git')
345
self.build_tree_contents([('a', 'text for a\n')])
347
output, error = self.run_bzr('grep text')
348
self.assertEqual(output, 'a:text for a\n')
349
self.assertEqual(error, '')