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 (
29
version_info as breezy_version,
31
from ...controldir import (
35
from ...tests.blackbox import ExternalBase
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, '')
68
self.assertTrue("Standalone tree (format: git)" in output)
70
def test_branch(self):
72
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
74
builder = tests.GitBranchBuilder()
75
builder.set_file(b'a', b'text for a\n', False)
76
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
80
output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
82
(error == 'Branched 1 revision(s).\n') or
83
(error == 'Branched 1 revision.\n'),
86
def test_checkout(self):
88
GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
90
builder = tests.GitBranchBuilder()
91
builder.set_file(b'a', b'text for a\n', False)
92
builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
96
output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
97
self.assertEqual(error,
98
'Fetching from Git to Bazaar repository. '
99
'For better performance, fetch into a Git repository.\n')
100
self.assertEqual(output, '')
102
def test_branch_ls(self):
104
output, error = self.run_bzr(['ls', '-r-1'])
105
self.assertEqual(error, '')
106
self.assertEqual(output, "a\n")
109
self.run_bzr("init --format=git repo")
111
def test_info_verbose(self):
114
output, error = self.run_bzr(['info', '-v'])
115
self.assertEqual(error, '')
116
self.assertTrue("Standalone tree (format: git)" in output)
117
self.assertTrue("control: Local Git Repository" in output)
118
self.assertTrue("branch: Local Git Branch" in output)
119
self.assertTrue("repository: Git Repository" in output)
121
def test_push_roundtripping(self):
122
self.knownFailure("roundtripping is not yet supported")
123
self.with_roundtripping()
125
GitRepo.init(os.path.join(self.test_dir, "bla"))
126
self.run_bzr(['init', 'foo'])
127
self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
128
# when roundtripping is supported
129
output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
130
self.assertEqual(b"", output)
131
self.assertTrue(error.endswith(b"Created new branch.\n"))
134
# Smoke test for "bzr log" in a git repository.
137
# Check that bzr log does not fail and includes the revision.
138
output, error = self.run_bzr(['log'])
139
self.assertEqual(error, '')
141
'<The commit message>' in output,
142
"Commit message was not found in output:\n%s" % (output,))
144
def test_log_verbose(self):
145
# Smoke test for "bzr log -v" in a git repository.
148
# Check that bzr log does not fail and includes the revision.
149
output, error = self.run_bzr(['log', '-v'])
152
git_repo, commit_sha1 = self.simple_commit()
153
git_repo.refs[b"refs/tags/foo"] = commit_sha1
155
output, error = self.run_bzr(['tags'])
156
self.assertEqual(error, '')
157
self.assertEqual(output, "foo 1\n")
162
output, error = self.run_bzr(["tag", "bar"])
164
# bzr <= 2.2 emits this message in the output stream
165
# bzr => 2.3 emits this message in the error stream
166
self.assertEqual(error + output, 'Created tag bar.\n')
168
def test_init_repo(self):
169
output, error = self.run_bzr(["init", "--format=git", "bla.git"])
170
self.assertEqual(error, '')
171
self.assertEqual(output, 'Created a standalone tree (format: git)\n')
173
def test_diff_format(self):
174
tree = self.make_branch_and_tree('.')
175
self.build_tree(['a'])
177
output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
178
self.assertEqual(error, '')
179
self.assertEqual(output,
180
'diff --git /dev/null b/a\n'
183
'index 0000000..c197bd8 100644\n'
189
def test_git_import_uncolocated(self):
190
r = GitRepo.init("a", mkdir=True)
191
self.build_tree(["a/file"])
193
r.do_commit(ref=b"refs/heads/abranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
194
r.do_commit(ref=b"refs/heads/bbranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
195
self.run_bzr(["git-import", "a", "b"])
196
self.assertEqual(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", committer=b"Joe <joe@example.com>", message=b"Dummy")
203
r.do_commit(ref=b"refs/heads/bbranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
204
self.run_bzr(["git-import", "--colocated", "a", "b"])
205
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
206
self.assertEqual(set(["abranch", "bbranch"]),
207
set(ControlDir.open("b").get_branches().keys()))
209
def test_git_import_incremental(self):
210
r = GitRepo.init("a", mkdir=True)
211
self.build_tree(["a/file"])
213
r.do_commit(ref=b"refs/heads/abranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
214
self.run_bzr(["git-import", "--colocated", "a", "b"])
215
self.run_bzr(["git-import", "--colocated", "a", "b"])
216
self.assertEqual(set([".bzr"]), set(os.listdir("b")))
217
b = ControlDir.open("b")
218
self.assertEqual(["abranch"], list(b.get_branches().keys()))
220
def test_git_import_tags(self):
221
r = GitRepo.init("a", mkdir=True)
222
self.build_tree(["a/file"])
224
cid = r.do_commit(ref=b"refs/heads/abranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
225
r[b"refs/tags/atag"] = cid
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()))
230
self.assertEqual(["atag"],
231
list(b.open_branch("abranch").tags.get_tag_dict().keys()))
233
def test_git_import_colo(self):
234
r = GitRepo.init("a", mkdir=True)
235
self.build_tree(["a/file"])
237
r.do_commit(ref=b"refs/heads/abranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
238
r.do_commit(ref=b"refs/heads/bbranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
239
self.make_controldir("b", format="development-colo")
240
self.run_bzr(["git-import", "--colocated", "a", "b"])
242
set([b.name for b in ControlDir.open("b").list_branches()]),
243
set(["abranch", "bbranch"]))
245
def test_git_refs_from_git(self):
246
r = GitRepo.init("a", mkdir=True)
247
self.build_tree(["a/file"])
249
cid = r.do_commit(ref=b"refs/heads/abranch", committer=b"Joe <joe@example.com>", message=b"Dummy")
250
r[b"refs/tags/atag"] = cid
251
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
252
self.assertEqual(stderr, "")
253
self.assertEqual(stdout,
254
'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
255
'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
257
def test_git_refs_from_bzr(self):
258
tree = self.make_branch_and_tree('a')
259
self.build_tree(["a/file"])
261
revid = tree.commit(committer=b"Joe <joe@example.com>", message=b"Dummy")
262
tree.branch.tags.set_tag("atag", revid)
263
(stdout, stderr) = self.run_bzr(["git-refs", "a"])
264
self.assertEqual(stderr, "")
265
self.assertTrue("refs/tags/atag -> " in stdout)
266
self.assertTrue("HEAD -> " in stdout)
268
def test_check(self):
269
r = GitRepo.init("gitr", mkdir=True)
270
self.build_tree_contents([("gitr/foo", b"hello from git")])
272
r.do_commit(b"message", committer=b"Somebody <user@example.com>")
273
out, err = self.run_bzr(["check", "gitr"])
275
self.assertEqual(out, '')
276
self.assertTrue(err.endswith, '3 objects\n')
279
class ShallowTests(ExternalBase):
282
super(ShallowTests, self).setUp()
283
# Smoke test for "bzr log" in a git repository with shallow depth.
284
self.repo = GitRepo.init('gitr', mkdir=True)
285
self.build_tree_contents([("gitr/foo", b"hello from git")])
286
self.repo.stage("foo")
288
b"message", committer=b"Somebody <user@example.com>",
289
commit_timestamp=1526330165, commit_timezone=0,
290
author_timestamp=1526330165, author_timezone=0,
291
merge_heads=[b'aa' * 20])
293
def test_log_shallow(self):
294
# Check that bzr log does not fail and includes the revision.
295
output, error = self.run_bzr(['log', 'gitr'], retcode=3)
296
self.assertEqual(error, 'brz: ERROR: Further revision history missing.\n')
297
self.assertEqual(output,
298
'------------------------------------------------------------\n'
299
'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
300
'git commit: ' + self.repo.head().decode('ascii') + '\n'
301
'committer: Somebody <user@example.com>\n'
302
'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
306
def test_version_info_rio(self):
307
output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
308
self.assertEqual(error, '')
309
self.assertNotIn('revno:', output)
311
def test_version_info_python(self):
312
output, error = self.run_bzr(['version-info', '--python', 'gitr'])
313
self.assertEqual(error, '')
314
self.assertNotIn('revno:', output)
316
def test_version_info_custom_with_revno(self):
317
output, error = self.run_bzr(
318
['version-info', '--custom',
319
'--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
320
self.assertEqual(error, 'brz: ERROR: Variable {revno} is not available.\n')
321
self.assertEqual(output, 'VERSION_INFO r')
323
def test_version_info_custom_without_revno(self):
324
output, error = self.run_bzr(
325
['version-info', '--custom', '--template=VERSION_INFO \n',
327
self.assertEqual(error, '')
328
self.assertEqual(output, 'VERSION_INFO \n')