/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/tests/test_blackbox.py

  • Committer: Jelmer Vernooij
  • Date: 2019-02-03 01:42:11 UTC
  • mto: This revision was merged to the branch mainline in revision 7267.
  • Revision ID: jelmer@jelmer.uk-20190203014211-poj1fv922sejfsb4
Don't require that short git shas have an even number of characters.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 David Allouche <ddaa@ddaa.net>
 
2
# Copyright (C) 2007-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
"""Black-box tests for bzr-git."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from dulwich.repo import (
 
23
    Repo as GitRepo,
 
24
    )
 
25
 
 
26
import os
 
27
 
 
28
from ...controldir import (
 
29
    ControlDir,
 
30
    )
 
31
 
 
32
from ...tests.blackbox import ExternalBase
 
33
from ...workingtree import WorkingTree
 
34
 
 
35
from .. import (
 
36
    tests,
 
37
    )
 
38
from ...tests.features import PluginLoadedFeature
 
39
 
 
40
 
 
41
class TestGitBlackBox(ExternalBase):
 
42
 
 
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]
 
50
 
 
51
    def test_nick(self):
 
52
        r = GitRepo.init(self.test_dir)
 
53
        dir = ControlDir.open(self.test_dir)
 
54
        dir.create_branch()
 
55
        output, error = self.run_bzr(['nick'])
 
56
        self.assertEqual("master\n", output)
 
57
 
 
58
    def test_branches(self):
 
59
        self.simple_commit()
 
60
        output, error = self.run_bzr(['branches'])
 
61
        self.assertEqual("* master\n", output)
 
62
 
 
63
    def test_info(self):
 
64
        self.simple_commit()
 
65
        output, error = self.run_bzr(['info'])
 
66
        self.assertEqual(error, '')
 
67
        self.assertEqual(
 
68
            output,
 
69
            'Standalone tree (format: git)\n'
 
70
            'Location:\n'
 
71
            '            light checkout root: .\n'
 
72
            '  checkout of co-located branch: master\n')
 
73
 
 
74
    def test_ignore(self):
 
75
        self.simple_commit()
 
76
        output, error = self.run_bzr(['ignore', 'foo'])
 
77
        self.assertEqual(error, '')
 
78
        self.assertEqual(output, '')
 
79
        self.assertFileEqual("foo\n", ".gitignore")
 
80
 
 
81
    def test_branch(self):
 
82
        os.mkdir("gitbranch")
 
83
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
84
        os.chdir('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>')
 
88
        builder.finish()
 
89
        os.chdir('..')
 
90
 
 
91
        output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
 
92
        self.assertTrue(
 
93
            (error == 'Branched 1 revision(s).\n') or
 
94
            (error == 'Branched 1 revision.\n'),
 
95
            error)
 
96
 
 
97
    def test_checkout(self):
 
98
        os.mkdir("gitbranch")
 
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>')
 
104
        builder.finish()
 
105
        os.chdir('..')
 
106
 
 
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, '')
 
112
 
 
113
    def test_branch_ls(self):
 
114
        self.simple_commit()
 
115
        output, error = self.run_bzr(['ls', '-r-1'])
 
116
        self.assertEqual(error, '')
 
117
        self.assertEqual(output, "a\n")
 
118
 
 
119
    def test_init(self):
 
120
        self.run_bzr("init --format=git repo")
 
121
 
 
122
    def test_info_verbose(self):
 
123
        self.simple_commit()
 
124
 
 
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)
 
131
 
 
132
    def test_push_roundtripping(self):
 
133
        self.knownFailure("roundtripping is not yet supported")
 
134
        self.with_roundtripping()
 
135
        os.mkdir("bla")
 
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"))
 
143
 
 
144
    def test_log(self):
 
145
        # Smoke test for "bzr log" in a git repository.
 
146
        self.simple_commit()
 
147
 
 
148
        # Check that bzr log does not fail and includes the revision.
 
149
        output, error = self.run_bzr(['log'])
 
150
        self.assertEqual(error, '')
 
151
        self.assertTrue(
 
152
            '<The commit message>' in output,
 
153
            "Commit message was not found in output:\n%s" % (output,))
 
154
 
 
155
    def test_log_verbose(self):
 
156
        # Smoke test for "bzr log -v" in a git repository.
 
157
        self.simple_commit()
 
158
 
 
159
        # Check that bzr log does not fail and includes the revision.
 
160
        output, error = self.run_bzr(['log', '-v'])
 
161
 
 
162
    def test_tags(self):
 
163
        git_repo, commit_sha1 = self.simple_commit()
 
164
        git_repo.refs[b"refs/tags/foo"] = commit_sha1
 
165
 
 
166
        output, error = self.run_bzr(['tags'])
 
167
        self.assertEqual(error, '')
 
168
        self.assertEqual(output, "foo                  1\n")
 
169
 
 
170
    def test_tag(self):
 
171
        self.simple_commit()
 
172
 
 
173
        output, error = self.run_bzr(["tag", "bar"])
 
174
 
 
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')
 
178
 
 
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')
 
183
 
 
184
    def test_diff_format(self):
 
185
        tree = self.make_branch_and_tree('.')
 
186
        self.build_tree(['a'])
 
187
        tree.add(['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'
 
192
                         'old mode 0\n'
 
193
                         'new mode 100644\n'
 
194
                         'index 0000000..c197bd8 100644\n'
 
195
                         '--- /dev/null\n'
 
196
                         '+++ b/a\n'
 
197
                         '@@ -0,0 +1 @@\n'
 
198
                         '+contents of a\n')
 
199
 
 
200
    def test_git_import_uncolocated(self):
 
201
        r = GitRepo.init("a", mkdir=True)
 
202
        self.build_tree(["a/file"])
 
203
        r.stage("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"])
 
209
        self.assertEqual(
 
210
            set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
 
211
 
 
212
    def test_git_import(self):
 
213
        r = GitRepo.init("a", mkdir=True)
 
214
        self.build_tree(["a/file"])
 
215
        r.stage("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()))
 
224
 
 
225
    def test_git_import_incremental(self):
 
226
        r = GitRepo.init("a", mkdir=True)
 
227
        self.build_tree(["a/file"])
 
228
        r.stage("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()))
 
236
 
 
237
    def test_git_import_tags(self):
 
238
        r = GitRepo.init("a", mkdir=True)
 
239
        self.build_tree(["a/file"])
 
240
        r.stage("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()))
 
250
 
 
251
    def test_git_import_colo(self):
 
252
        r = GitRepo.init("a", mkdir=True)
 
253
        self.build_tree(["a/file"])
 
254
        r.stage("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"])
 
261
        self.assertEqual(
 
262
            set([b.name for b in ControlDir.open("b").list_branches()]),
 
263
            set(["abranch", "bbranch"]))
 
264
 
 
265
    def test_git_refs_from_git(self):
 
266
        r = GitRepo.init("a", mkdir=True)
 
267
        self.build_tree(["a/file"])
 
268
        r.stage("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')
 
277
 
 
278
    def test_git_refs_from_bzr(self):
 
279
        tree = self.make_branch_and_tree('a')
 
280
        self.build_tree(["a/file"])
 
281
        tree.add(["file"])
 
282
        revid = tree.commit(
 
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)
 
289
 
 
290
    def test_check(self):
 
291
        r = GitRepo.init("gitr", mkdir=True)
 
292
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
293
        r.stage("foo")
 
294
        r.do_commit(b"message", committer=b"Somebody <user@example.com>")
 
295
        out, err = self.run_bzr(["check", "gitr"])
 
296
        self.maxDiff = None
 
297
        self.assertEqual(out, '')
 
298
        self.assertTrue(err.endswith, '3 objects\n')
 
299
 
 
300
 
 
301
class ShallowTests(ExternalBase):
 
302
 
 
303
    def setUp(self):
 
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")
 
309
        self.repo.do_commit(
 
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])
 
314
 
 
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)
 
318
        self.assertEqual(
 
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'
 
326
                         'message:\n'
 
327
                         '  message\n')
 
328
 
 
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)
 
333
 
 
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)
 
338
 
 
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)
 
343
        self.assertEqual(
 
344
            error, 'brz: ERROR: Variable {revno} is not available.\n')
 
345
        self.assertEqual(output, 'VERSION_INFO r')
 
346
 
 
347
    def test_version_info_custom_without_revno(self):
 
348
        output, error = self.run_bzr(
 
349
            ['version-info', '--custom', '--template=VERSION_INFO \n',
 
350
             'gitr'])
 
351
        self.assertEqual(error, '')
 
352
        self.assertEqual(output, 'VERSION_INFO \n')
 
353
 
 
354
 
 
355
class SwitchTests(ExternalBase):
 
356
 
 
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')
 
365
        builder.reset()
 
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>')
 
368
        builder.finish()
 
369
 
 
370
        repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
 
371
 
 
372
        repo.reset_index()
 
373
 
 
374
        output, error = self.run_bzr('switch oldbranch')
 
375
        self.assertEqual(output, '')
 
376
        self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
 
377
 
 
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)))
 
384
 
 
385
 
 
386
class GrepTests(ExternalBase):
 
387
 
 
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')])
 
392
        tree.add(['a'])
 
393
        output, error = self.run_bzr('grep text')
 
394
        self.assertEqual(output, 'a:text for a\n')
 
395
        self.assertEqual(error, '')
 
396
 
 
397
 
 
398
class ReconcileTests(ExternalBase):
 
399
 
 
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')])
 
403
        tree.add(['a'])
 
404
        output, error = self.run_bzr('reconcile')
 
405
        self.assertContainsRe(
 
406
            output,
 
407
            'Reconciling branch file://.*\n'
 
408
            'Reconciling repository file://.*\n'
 
409
            'Reconciliation complete.\n')
 
410
        self.assertEqual(error, '')
 
411
 
 
412
 
 
413
class StatusTests(ExternalBase):
 
414
 
 
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, '')
 
424
 
 
425
 
 
426
class StatsTests(ExternalBase):
 
427
 
 
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')])
 
432
        tree.add(['a'])
 
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')
 
436
 
 
437
 
 
438
class GitObjectsTests(ExternalBase):
 
439
 
 
440
    def run_simple(self, format):
 
441
        tree = self.make_branch_and_tree('.', format=format)
 
442
        self.build_tree(['a/', 'a/foo'])
 
443
        tree.add(['a'])
 
444
        tree.commit('add a')
 
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, '')
 
449
 
 
450
        output, error = self.run_bzr('git-object %s' % shas[0])
 
451
        self.assertEqual('', error)
 
452
 
 
453
    def test_in_native(self):
 
454
        self.run_simple(format='git')
 
455
 
 
456
    def test_in_bzr(self):
 
457
        self.run_simple(format='2a')