/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-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

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_cat_revision(self):
 
82
        self.simple_commit()
 
83
        output, error = self.run_bzr(['cat-revision', '-r-1'], retcode=3)
 
84
        self.assertContainsRe(
 
85
            error,
 
86
            'brz: ERROR: Repository .* does not support access to raw '
 
87
            'revision texts')
 
88
        self.assertEqual(output, '')
 
89
 
 
90
    def test_branch(self):
 
91
        os.mkdir("gitbranch")
 
92
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
93
        os.chdir('gitbranch')
 
94
        builder = tests.GitBranchBuilder()
 
95
        builder.set_file(b'a', b'text for a\n', False)
 
96
        builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
 
97
        builder.finish()
 
98
        os.chdir('..')
 
99
 
 
100
        output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
 
101
        self.assertTrue(
 
102
            (error == 'Branched 1 revision(s).\n') or
 
103
            (error == 'Branched 1 revision.\n'),
 
104
            error)
 
105
 
 
106
    def test_checkout(self):
 
107
        os.mkdir("gitbranch")
 
108
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
109
        os.chdir('gitbranch')
 
110
        builder = tests.GitBranchBuilder()
 
111
        builder.set_file(b'a', b'text for a\n', False)
 
112
        builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
 
113
        builder.finish()
 
114
        os.chdir('..')
 
115
 
 
116
        output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
 
117
        self.assertEqual(error,
 
118
                         'Fetching from Git to Bazaar repository. '
 
119
                         'For better performance, fetch into a Git repository.\n')
 
120
        self.assertEqual(output, '')
 
121
 
 
122
    def test_branch_ls(self):
 
123
        self.simple_commit()
 
124
        output, error = self.run_bzr(['ls', '-r-1'])
 
125
        self.assertEqual(error, '')
 
126
        self.assertEqual(output, "a\n")
 
127
 
 
128
    def test_init(self):
 
129
        self.run_bzr("init --format=git repo")
 
130
 
 
131
    def test_info_verbose(self):
 
132
        self.simple_commit()
 
133
 
 
134
        output, error = self.run_bzr(['info', '-v'])
 
135
        self.assertEqual(error, '')
 
136
        self.assertTrue("Standalone tree (format: git)" in output)
 
137
        self.assertTrue("control: Local Git Repository" in output)
 
138
        self.assertTrue("branch: Local Git Branch" in output)
 
139
        self.assertTrue("repository: Git Repository" in output)
 
140
 
 
141
    def test_push_roundtripping(self):
 
142
        self.knownFailure("roundtripping is not yet supported")
 
143
        self.with_roundtripping()
 
144
        os.mkdir("bla")
 
145
        GitRepo.init(os.path.join(self.test_dir, "bla"))
 
146
        self.run_bzr(['init', 'foo'])
 
147
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
148
        # when roundtripping is supported
 
149
        output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
 
150
        self.assertEqual(b"", output)
 
151
        self.assertTrue(error.endswith(b"Created new branch.\n"))
 
152
 
 
153
    def test_log(self):
 
154
        # Smoke test for "bzr log" in a git repository.
 
155
        self.simple_commit()
 
156
 
 
157
        # Check that bzr log does not fail and includes the revision.
 
158
        output, error = self.run_bzr(['log'])
 
159
        self.assertEqual(error, '')
 
160
        self.assertTrue(
 
161
            '<The commit message>' in output,
 
162
            "Commit message was not found in output:\n%s" % (output,))
 
163
 
 
164
    def test_log_verbose(self):
 
165
        # Smoke test for "bzr log -v" in a git repository.
 
166
        self.simple_commit()
 
167
 
 
168
        # Check that bzr log does not fail and includes the revision.
 
169
        output, error = self.run_bzr(['log', '-v'])
 
170
 
 
171
    def test_tags(self):
 
172
        git_repo, commit_sha1 = self.simple_commit()
 
173
        git_repo.refs[b"refs/tags/foo"] = commit_sha1
 
174
 
 
175
        output, error = self.run_bzr(['tags'])
 
176
        self.assertEqual(error, '')
 
177
        self.assertEqual(output, "foo                  1\n")
 
178
 
 
179
    def test_tag(self):
 
180
        self.simple_commit()
 
181
 
 
182
        output, error = self.run_bzr(["tag", "bar"])
 
183
 
 
184
        # bzr <= 2.2 emits this message in the output stream
 
185
        # bzr => 2.3 emits this message in the error stream
 
186
        self.assertEqual(error + output, 'Created tag bar.\n')
 
187
 
 
188
    def test_init_repo(self):
 
189
        output, error = self.run_bzr(["init", "--format=git", "bla.git"])
 
190
        self.assertEqual(error, '')
 
191
        self.assertEqual(output, 'Created a standalone tree (format: git)\n')
 
192
 
 
193
    def test_diff_format(self):
 
194
        tree = self.make_branch_and_tree('.')
 
195
        self.build_tree(['a'])
 
196
        tree.add(['a'])
 
197
        output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
 
198
        self.assertEqual(error, '')
 
199
        self.assertEqual(output,
 
200
                         'diff --git /dev/null b/a\n'
 
201
                         'old mode 0\n'
 
202
                         'new mode 100644\n'
 
203
                         'index 0000000..c197bd8 100644\n'
 
204
                         '--- /dev/null\n'
 
205
                         '+++ b/a\n'
 
206
                         '@@ -0,0 +1 @@\n'
 
207
                         '+contents of a\n')
 
208
 
 
209
    def test_git_import_uncolocated(self):
 
210
        r = GitRepo.init("a", mkdir=True)
 
211
        self.build_tree(["a/file"])
 
212
        r.stage("file")
 
213
        r.do_commit(ref=b"refs/heads/abranch",
 
214
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
215
        r.do_commit(ref=b"refs/heads/bbranch",
 
216
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
217
        self.run_bzr(["git-import", "a", "b"])
 
218
        self.assertEqual(
 
219
            set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
 
220
 
 
221
    def test_git_import(self):
 
222
        r = GitRepo.init("a", mkdir=True)
 
223
        self.build_tree(["a/file"])
 
224
        r.stage("file")
 
225
        r.do_commit(ref=b"refs/heads/abranch",
 
226
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
227
        r.do_commit(ref=b"refs/heads/bbranch",
 
228
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
229
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
230
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
231
        self.assertEqual(set(["abranch", "bbranch"]),
 
232
                         set(ControlDir.open("b").get_branches().keys()))
 
233
 
 
234
    def test_git_import_incremental(self):
 
235
        r = GitRepo.init("a", mkdir=True)
 
236
        self.build_tree(["a/file"])
 
237
        r.stage("file")
 
238
        r.do_commit(ref=b"refs/heads/abranch",
 
239
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
240
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
241
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
242
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
243
        b = ControlDir.open("b")
 
244
        self.assertEqual(["abranch"], list(b.get_branches().keys()))
 
245
 
 
246
    def test_git_import_tags(self):
 
247
        r = GitRepo.init("a", mkdir=True)
 
248
        self.build_tree(["a/file"])
 
249
        r.stage("file")
 
250
        cid = r.do_commit(ref=b"refs/heads/abranch",
 
251
                          committer=b"Joe <joe@example.com>", message=b"Dummy")
 
252
        r[b"refs/tags/atag"] = cid
 
253
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
254
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
255
        b = ControlDir.open("b")
 
256
        self.assertEqual(["abranch"], list(b.get_branches().keys()))
 
257
        self.assertEqual(["atag"],
 
258
                         list(b.open_branch("abranch").tags.get_tag_dict().keys()))
 
259
 
 
260
    def test_git_import_colo(self):
 
261
        r = GitRepo.init("a", mkdir=True)
 
262
        self.build_tree(["a/file"])
 
263
        r.stage("file")
 
264
        r.do_commit(ref=b"refs/heads/abranch",
 
265
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
266
        r.do_commit(ref=b"refs/heads/bbranch",
 
267
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
268
        self.make_controldir("b", format="development-colo")
 
269
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
270
        self.assertEqual(
 
271
            set([b.name for b in ControlDir.open("b").list_branches()]),
 
272
            set(["abranch", "bbranch"]))
 
273
 
 
274
    def test_git_refs_from_git(self):
 
275
        r = GitRepo.init("a", mkdir=True)
 
276
        self.build_tree(["a/file"])
 
277
        r.stage("file")
 
278
        cid = r.do_commit(ref=b"refs/heads/abranch",
 
279
                          committer=b"Joe <joe@example.com>", message=b"Dummy")
 
280
        r[b"refs/tags/atag"] = cid
 
281
        (stdout, stderr) = self.run_bzr(["git-refs", "a"])
 
282
        self.assertEqual(stderr, "")
 
283
        self.assertEqual(stdout,
 
284
                         'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
 
285
                         'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
 
286
 
 
287
    def test_git_refs_from_bzr(self):
 
288
        tree = self.make_branch_and_tree('a')
 
289
        self.build_tree(["a/file"])
 
290
        tree.add(["file"])
 
291
        revid = tree.commit(
 
292
            committer=b"Joe <joe@example.com>", message=b"Dummy")
 
293
        tree.branch.tags.set_tag("atag", revid)
 
294
        (stdout, stderr) = self.run_bzr(["git-refs", "a"])
 
295
        self.assertEqual(stderr, "")
 
296
        self.assertTrue("refs/tags/atag -> " in stdout)
 
297
        self.assertTrue("HEAD -> " in stdout)
 
298
 
 
299
    def test_check(self):
 
300
        r = GitRepo.init("gitr", mkdir=True)
 
301
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
302
        r.stage("foo")
 
303
        r.do_commit(b"message", committer=b"Somebody <user@example.com>")
 
304
        out, err = self.run_bzr(["check", "gitr"])
 
305
        self.maxDiff = None
 
306
        self.assertEqual(out, '')
 
307
        self.assertTrue(err.endswith, '3 objects\n')
 
308
 
 
309
 
 
310
class ShallowTests(ExternalBase):
 
311
 
 
312
    def setUp(self):
 
313
        super(ShallowTests, self).setUp()
 
314
        # Smoke test for "bzr log" in a git repository with shallow depth.
 
315
        self.repo = GitRepo.init('gitr', mkdir=True)
 
316
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
317
        self.repo.stage("foo")
 
318
        self.repo.do_commit(
 
319
            b"message", committer=b"Somebody <user@example.com>",
 
320
            author=b"Somebody <user@example.com>",
 
321
            commit_timestamp=1526330165, commit_timezone=0,
 
322
            author_timestamp=1526330165, author_timezone=0,
 
323
            merge_heads=[b'aa' * 20])
 
324
 
 
325
    def test_log_shallow(self):
 
326
        # Check that bzr log does not fail and includes the revision.
 
327
        output, error = self.run_bzr(['log', 'gitr'], retcode=3)
 
328
        self.assertEqual(
 
329
            error, 'brz: ERROR: Further revision history missing.\n')
 
330
        self.assertEqual(output,
 
331
                         '------------------------------------------------------------\n'
 
332
                         'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
 
333
                         'git commit: ' + self.repo.head().decode('ascii') + '\n'
 
334
                         'committer: Somebody <user@example.com>\n'
 
335
                         'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
 
336
                         'message:\n'
 
337
                         '  message\n')
 
338
 
 
339
    def test_version_info_rio(self):
 
340
        output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
 
341
        self.assertEqual(error, '')
 
342
        self.assertNotIn('revno:', output)
 
343
 
 
344
    def test_version_info_python(self):
 
345
        output, error = self.run_bzr(['version-info', '--python', 'gitr'])
 
346
        self.assertEqual(error, '')
 
347
        self.assertNotIn('revno:', output)
 
348
 
 
349
    def test_version_info_custom_with_revno(self):
 
350
        output, error = self.run_bzr(
 
351
            ['version-info', '--custom',
 
352
             '--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
 
353
        self.assertEqual(
 
354
            error, 'brz: ERROR: Variable {revno} is not available.\n')
 
355
        self.assertEqual(output, 'VERSION_INFO r')
 
356
 
 
357
    def test_version_info_custom_without_revno(self):
 
358
        output, error = self.run_bzr(
 
359
            ['version-info', '--custom', '--template=VERSION_INFO \n',
 
360
             'gitr'])
 
361
        self.assertEqual(error, '')
 
362
        self.assertEqual(output, 'VERSION_INFO \n')
 
363
 
 
364
 
 
365
class SwitchTests(ExternalBase):
 
366
 
 
367
    def test_switch_branch(self):
 
368
        # Create a git repository with a revision.
 
369
        repo = GitRepo.init(self.test_dir)
 
370
        builder = tests.GitBranchBuilder()
 
371
        builder.set_branch(b'refs/heads/oldbranch')
 
372
        builder.set_file('a', b'text for a\n', False)
 
373
        builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
374
        builder.set_branch(b'refs/heads/newbranch')
 
375
        builder.reset()
 
376
        builder.set_file('a', b'text for new a\n', False)
 
377
        builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
378
        builder.finish()
 
379
 
 
380
        repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
 
381
 
 
382
        repo.reset_index()
 
383
 
 
384
        output, error = self.run_bzr('switch oldbranch')
 
385
        self.assertEqual(output, '')
 
386
        self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
 
387
 
 
388
        self.assertFileEqual("text for a\n", 'a')
 
389
        tree = WorkingTree.open('.')
 
390
        with tree.lock_read():
 
391
            basis_tree = tree.basis_tree()
 
392
            with basis_tree.lock_read():
 
393
                self.assertEqual([], list(tree.iter_changes(basis_tree)))
 
394
 
 
395
 
 
396
class GrepTests(ExternalBase):
 
397
 
 
398
    def test_simple_grep(self):
 
399
        tree = self.make_branch_and_tree('.', format='git')
 
400
        self.build_tree_contents([('a', 'text for a\n')])
 
401
        tree.add(['a'])
 
402
        output, error = self.run_bzr('grep text')
 
403
        self.assertEqual(output, 'a:text for a\n')
 
404
        self.assertEqual(error, '')
 
405
 
 
406
 
 
407
class ReconcileTests(ExternalBase):
 
408
 
 
409
    def test_simple_reconcile(self):
 
410
        tree = self.make_branch_and_tree('.', format='git')
 
411
        self.build_tree_contents([('a', 'text for a\n')])
 
412
        tree.add(['a'])
 
413
        output, error = self.run_bzr('reconcile')
 
414
        self.assertContainsRe(
 
415
            output,
 
416
            'Reconciling branch file://.*\n'
 
417
            'Reconciling repository file://.*\n'
 
418
            'Reconciliation complete.\n')
 
419
        self.assertEqual(error, '')
 
420
 
 
421
 
 
422
class StatusTests(ExternalBase):
 
423
 
 
424
    def test_empty_dir(self):
 
425
        tree = self.make_branch_and_tree('.', format='git')
 
426
        self.build_tree(['a/', 'a/foo'])
 
427
        self.build_tree_contents([('.gitignore', 'foo\n')])
 
428
        tree.add(['.gitignore'])
 
429
        tree.commit('add ignore')
 
430
        output, error = self.run_bzr('st')
 
431
        self.assertEqual(output, '')
 
432
        self.assertEqual(error, '')
 
433
 
 
434
 
 
435
class StatsTests(ExternalBase):
 
436
 
 
437
    def test_simple_stats(self):
 
438
        self.requireFeature(PluginLoadedFeature('stats'))
 
439
        tree = self.make_branch_and_tree('.', format='git')
 
440
        self.build_tree_contents([('a', 'text for a\n')])
 
441
        tree.add(['a'])
 
442
        tree.commit('a commit', committer='Somebody <somebody@example.com>')
 
443
        output, error = self.run_bzr('stats')
 
444
        self.assertEqual(output, '   1 Somebody <somebody@example.com>\n')
 
445
 
 
446
 
 
447
class GitObjectsTests(ExternalBase):
 
448
 
 
449
    def run_simple(self, format):
 
450
        tree = self.make_branch_and_tree('.', format=format)
 
451
        self.build_tree(['a/', 'a/foo'])
 
452
        tree.add(['a'])
 
453
        tree.commit('add a')
 
454
        output, error = self.run_bzr('git-objects')
 
455
        shas = list(output.splitlines())
 
456
        self.assertEqual([40, 40], [len(s) for s in shas])
 
457
        self.assertEqual(error, '')
 
458
 
 
459
        output, error = self.run_bzr('git-object %s' % shas[0])
 
460
        self.assertEqual('', error)
 
461
 
 
462
    def test_in_native(self):
 
463
        self.run_simple(format='git')
 
464
 
 
465
    def test_in_bzr(self):
 
466
        self.run_simple(format='2a')
 
467
 
 
468
 
 
469
class GitApplyTests(ExternalBase):
 
470
 
 
471
    def test_apply(self):
 
472
        b = self.make_branch_and_tree('.')
 
473
 
 
474
        with open('foo.patch', 'w') as f:
 
475
            f.write("""\
 
476
From bdefb25fab801e6af0a70e965f60cb48f2b759fa Mon Sep 17 00:00:00 2001
 
477
From: Dmitry Bogatov <KAction@debian.org>
 
478
Date: Fri, 8 Feb 2019 23:28:30 +0000
 
479
Subject: [PATCH] Add fixed for out-of-date-standards-version
 
480
 
 
481
---
 
482
 message           | 3 +++
 
483
 1 files changed, 14 insertions(+)
 
484
 create mode 100644 message
 
485
 
 
486
diff --git a/message b/message
 
487
new file mode 100644
 
488
index 0000000..05ec0b1
 
489
--- /dev/null
 
490
+++ b/message
 
491
@@ -0,0 +1,3 @@
 
492
+Update standards version, no changes needed.
 
493
+Certainty: certain
 
494
+Fixed-Lintian-Tags: out-of-date-standards-version
 
495
""")
 
496
        output, error = self.run_bzr('git-apply foo.patch')
 
497
        self.assertContainsRe(
 
498
            error,
 
499
            'Committing to: .*\n'
 
500
            'Committed revision 1.\n')