/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: 2018-11-18 18:23:32 UTC
  • mto: This revision was merged to the branch mainline in revision 7197.
  • Revision ID: jelmer@jelmer.uk-20181118182332-viz1qvqese2mo9i6
Fix some more Bazaar references.

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
 
 
39
 
 
40
class TestGitBlackBox(ExternalBase):
 
41
 
 
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]
 
49
 
 
50
    def test_nick(self):
 
51
        r = GitRepo.init(self.test_dir)
 
52
        dir = ControlDir.open(self.test_dir)
 
53
        dir.create_branch()
 
54
        output, error = self.run_bzr(['nick'])
 
55
        self.assertEqual("master\n", output)
 
56
 
 
57
    def test_branches(self):
 
58
        self.simple_commit()
 
59
        output, error = self.run_bzr(['branches'])
 
60
        self.assertEqual("* master\n", output)
 
61
 
 
62
    def test_info(self):
 
63
        self.simple_commit()
 
64
        output, error = self.run_bzr(['info'])
 
65
        self.assertEqual(error, '')
 
66
        self.assertTrue("Standalone tree (format: git)" in output)
 
67
 
 
68
    def test_branch(self):
 
69
        os.mkdir("gitbranch")
 
70
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
71
        os.chdir('gitbranch')
 
72
        builder = tests.GitBranchBuilder()
 
73
        builder.set_file(b'a', b'text for a\n', False)
 
74
        builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
 
75
        builder.finish()
 
76
        os.chdir('..')
 
77
 
 
78
        output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
 
79
        self.assertTrue(
 
80
            (error == 'Branched 1 revision(s).\n') or
 
81
            (error == 'Branched 1 revision.\n'),
 
82
            error)
 
83
 
 
84
    def test_checkout(self):
 
85
        os.mkdir("gitbranch")
 
86
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
87
        os.chdir('gitbranch')
 
88
        builder = tests.GitBranchBuilder()
 
89
        builder.set_file(b'a', b'text for a\n', False)
 
90
        builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
 
91
        builder.finish()
 
92
        os.chdir('..')
 
93
 
 
94
        output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
 
95
        self.assertEqual(error,
 
96
                         'Fetching from Git to Bazaar repository. '
 
97
                         'For better performance, fetch into a Git repository.\n')
 
98
        self.assertEqual(output, '')
 
99
 
 
100
    def test_branch_ls(self):
 
101
        self.simple_commit()
 
102
        output, error = self.run_bzr(['ls', '-r-1'])
 
103
        self.assertEqual(error, '')
 
104
        self.assertEqual(output, "a\n")
 
105
 
 
106
    def test_init(self):
 
107
        self.run_bzr("init --format=git repo")
 
108
 
 
109
    def test_info_verbose(self):
 
110
        self.simple_commit()
 
111
 
 
112
        output, error = self.run_bzr(['info', '-v'])
 
113
        self.assertEqual(error, '')
 
114
        self.assertTrue("Standalone tree (format: git)" in output)
 
115
        self.assertTrue("control: Local Git Repository" in output)
 
116
        self.assertTrue("branch: Local Git Branch" in output)
 
117
        self.assertTrue("repository: Git Repository" in output)
 
118
 
 
119
    def test_push_roundtripping(self):
 
120
        self.knownFailure("roundtripping is not yet supported")
 
121
        self.with_roundtripping()
 
122
        os.mkdir("bla")
 
123
        GitRepo.init(os.path.join(self.test_dir, "bla"))
 
124
        self.run_bzr(['init', 'foo'])
 
125
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
126
        # when roundtripping is supported
 
127
        output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
 
128
        self.assertEqual(b"", output)
 
129
        self.assertTrue(error.endswith(b"Created new branch.\n"))
 
130
 
 
131
    def test_log(self):
 
132
        # Smoke test for "bzr log" in a git repository.
 
133
        self.simple_commit()
 
134
 
 
135
        # Check that bzr log does not fail and includes the revision.
 
136
        output, error = self.run_bzr(['log'])
 
137
        self.assertEqual(error, '')
 
138
        self.assertTrue(
 
139
            '<The commit message>' in output,
 
140
            "Commit message was not found in output:\n%s" % (output,))
 
141
 
 
142
    def test_log_verbose(self):
 
143
        # Smoke test for "bzr log -v" in a git repository.
 
144
        self.simple_commit()
 
145
 
 
146
        # Check that bzr log does not fail and includes the revision.
 
147
        output, error = self.run_bzr(['log', '-v'])
 
148
 
 
149
    def test_tags(self):
 
150
        git_repo, commit_sha1 = self.simple_commit()
 
151
        git_repo.refs[b"refs/tags/foo"] = commit_sha1
 
152
 
 
153
        output, error = self.run_bzr(['tags'])
 
154
        self.assertEqual(error, '')
 
155
        self.assertEqual(output, "foo                  1\n")
 
156
 
 
157
    def test_tag(self):
 
158
        self.simple_commit()
 
159
 
 
160
        output, error = self.run_bzr(["tag", "bar"])
 
161
 
 
162
        # bzr <= 2.2 emits this message in the output stream
 
163
        # bzr => 2.3 emits this message in the error stream
 
164
        self.assertEqual(error + output, 'Created tag bar.\n')
 
165
 
 
166
    def test_init_repo(self):
 
167
        output, error = self.run_bzr(["init", "--format=git", "bla.git"])
 
168
        self.assertEqual(error, '')
 
169
        self.assertEqual(output, 'Created a standalone tree (format: git)\n')
 
170
 
 
171
    def test_diff_format(self):
 
172
        tree = self.make_branch_and_tree('.')
 
173
        self.build_tree(['a'])
 
174
        tree.add(['a'])
 
175
        output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
 
176
        self.assertEqual(error, '')
 
177
        self.assertEqual(output,
 
178
                         'diff --git /dev/null b/a\n'
 
179
                         'old mode 0\n'
 
180
                         'new mode 100644\n'
 
181
                         'index 0000000..c197bd8 100644\n'
 
182
                         '--- /dev/null\n'
 
183
                         '+++ b/a\n'
 
184
                         '@@ -0,0 +1 @@\n'
 
185
                         '+contents of a\n')
 
186
 
 
187
    def test_git_import_uncolocated(self):
 
188
        r = GitRepo.init("a", mkdir=True)
 
189
        self.build_tree(["a/file"])
 
190
        r.stage("file")
 
191
        r.do_commit(ref=b"refs/heads/abranch",
 
192
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
193
        r.do_commit(ref=b"refs/heads/bbranch",
 
194
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
195
        self.run_bzr(["git-import", "a", "b"])
 
196
        self.assertEqual(
 
197
            set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
 
198
 
 
199
    def test_git_import(self):
 
200
        r = GitRepo.init("a", mkdir=True)
 
201
        self.build_tree(["a/file"])
 
202
        r.stage("file")
 
203
        r.do_commit(ref=b"refs/heads/abranch",
 
204
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
205
        r.do_commit(ref=b"refs/heads/bbranch",
 
206
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
207
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
208
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
209
        self.assertEqual(set(["abranch", "bbranch"]),
 
210
                         set(ControlDir.open("b").get_branches().keys()))
 
211
 
 
212
    def test_git_import_incremental(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
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
219
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
220
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
221
        b = ControlDir.open("b")
 
222
        self.assertEqual(["abranch"], list(b.get_branches().keys()))
 
223
 
 
224
    def test_git_import_tags(self):
 
225
        r = GitRepo.init("a", mkdir=True)
 
226
        self.build_tree(["a/file"])
 
227
        r.stage("file")
 
228
        cid = r.do_commit(ref=b"refs/heads/abranch",
 
229
                          committer=b"Joe <joe@example.com>", message=b"Dummy")
 
230
        r[b"refs/tags/atag"] = cid
 
231
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
232
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
233
        b = ControlDir.open("b")
 
234
        self.assertEqual(["abranch"], list(b.get_branches().keys()))
 
235
        self.assertEqual(["atag"],
 
236
                         list(b.open_branch("abranch").tags.get_tag_dict().keys()))
 
237
 
 
238
    def test_git_import_colo(self):
 
239
        r = GitRepo.init("a", mkdir=True)
 
240
        self.build_tree(["a/file"])
 
241
        r.stage("file")
 
242
        r.do_commit(ref=b"refs/heads/abranch",
 
243
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
244
        r.do_commit(ref=b"refs/heads/bbranch",
 
245
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
246
        self.make_controldir("b", format="development-colo")
 
247
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
248
        self.assertEqual(
 
249
            set([b.name for b in ControlDir.open("b").list_branches()]),
 
250
            set(["abranch", "bbranch"]))
 
251
 
 
252
    def test_git_refs_from_git(self):
 
253
        r = GitRepo.init("a", mkdir=True)
 
254
        self.build_tree(["a/file"])
 
255
        r.stage("file")
 
256
        cid = r.do_commit(ref=b"refs/heads/abranch",
 
257
                          committer=b"Joe <joe@example.com>", message=b"Dummy")
 
258
        r[b"refs/tags/atag"] = cid
 
259
        (stdout, stderr) = self.run_bzr(["git-refs", "a"])
 
260
        self.assertEqual(stderr, "")
 
261
        self.assertEqual(stdout,
 
262
                         'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
 
263
                         'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
 
264
 
 
265
    def test_git_refs_from_bzr(self):
 
266
        tree = self.make_branch_and_tree('a')
 
267
        self.build_tree(["a/file"])
 
268
        tree.add(["file"])
 
269
        revid = tree.commit(
 
270
            committer=b"Joe <joe@example.com>", message=b"Dummy")
 
271
        tree.branch.tags.set_tag("atag", revid)
 
272
        (stdout, stderr) = self.run_bzr(["git-refs", "a"])
 
273
        self.assertEqual(stderr, "")
 
274
        self.assertTrue("refs/tags/atag -> " in stdout)
 
275
        self.assertTrue("HEAD -> " in stdout)
 
276
 
 
277
    def test_check(self):
 
278
        r = GitRepo.init("gitr", mkdir=True)
 
279
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
280
        r.stage("foo")
 
281
        r.do_commit(b"message", committer=b"Somebody <user@example.com>")
 
282
        out, err = self.run_bzr(["check", "gitr"])
 
283
        self.maxDiff = None
 
284
        self.assertEqual(out, '')
 
285
        self.assertTrue(err.endswith, '3 objects\n')
 
286
 
 
287
 
 
288
class ShallowTests(ExternalBase):
 
289
 
 
290
    def setUp(self):
 
291
        super(ShallowTests, self).setUp()
 
292
        # Smoke test for "bzr log" in a git repository with shallow depth.
 
293
        self.repo = GitRepo.init('gitr', mkdir=True)
 
294
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
295
        self.repo.stage("foo")
 
296
        self.repo.do_commit(
 
297
            b"message", committer=b"Somebody <user@example.com>",
 
298
            commit_timestamp=1526330165, commit_timezone=0,
 
299
            author_timestamp=1526330165, author_timezone=0,
 
300
            merge_heads=[b'aa' * 20])
 
301
 
 
302
    def test_log_shallow(self):
 
303
        # Check that bzr log does not fail and includes the revision.
 
304
        output, error = self.run_bzr(['log', 'gitr'], retcode=3)
 
305
        self.assertEqual(
 
306
            error, 'brz: ERROR: Further revision history missing.\n')
 
307
        self.assertEqual(output,
 
308
                         '------------------------------------------------------------\n'
 
309
                         'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
 
310
                         'git commit: ' + self.repo.head().decode('ascii') + '\n'
 
311
                         'committer: Somebody <user@example.com>\n'
 
312
                         'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
 
313
                         'message:\n'
 
314
                         '  message\n')
 
315
 
 
316
    def test_version_info_rio(self):
 
317
        output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
 
318
        self.assertEqual(error, '')
 
319
        self.assertNotIn('revno:', output)
 
320
 
 
321
    def test_version_info_python(self):
 
322
        output, error = self.run_bzr(['version-info', '--python', 'gitr'])
 
323
        self.assertEqual(error, '')
 
324
        self.assertNotIn('revno:', output)
 
325
 
 
326
    def test_version_info_custom_with_revno(self):
 
327
        output, error = self.run_bzr(
 
328
            ['version-info', '--custom',
 
329
             '--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
 
330
        self.assertEqual(
 
331
            error, 'brz: ERROR: Variable {revno} is not available.\n')
 
332
        self.assertEqual(output, 'VERSION_INFO r')
 
333
 
 
334
    def test_version_info_custom_without_revno(self):
 
335
        output, error = self.run_bzr(
 
336
            ['version-info', '--custom', '--template=VERSION_INFO \n',
 
337
             'gitr'])
 
338
        self.assertEqual(error, '')
 
339
        self.assertEqual(output, 'VERSION_INFO \n')
 
340
 
 
341
 
 
342
class SwitchTests(ExternalBase):
 
343
 
 
344
    def test_switch_branch(self):
 
345
        # Create a git repository with a revision.
 
346
        repo = GitRepo.init(self.test_dir)
 
347
        builder = tests.GitBranchBuilder()
 
348
        builder.set_branch(b'refs/heads/oldbranch')
 
349
        builder.set_file('a', b'text for a\n', False)
 
350
        builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
351
        builder.set_branch(b'refs/heads/newbranch')
 
352
        builder.reset()
 
353
        builder.set_file('a', b'text for new a\n', False)
 
354
        builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
355
        builder.finish()
 
356
 
 
357
        repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
 
358
 
 
359
        repo.reset_index()
 
360
 
 
361
        output, error = self.run_bzr('switch oldbranch')
 
362
        self.assertEqual(output, '')
 
363
        self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
 
364
 
 
365
        self.assertFileEqual("text for a\n", 'a')
 
366
        tree = WorkingTree.open('.')
 
367
        with tree.lock_read():
 
368
            basis_tree = tree.basis_tree()
 
369
            with basis_tree.lock_read():
 
370
                self.assertEqual([], list(tree.iter_changes(basis_tree)))
 
371
 
 
372
 
 
373
class GrepTests(ExternalBase):
 
374
 
 
375
    def test_simple_grep(self):
 
376
        tree = self.make_branch_and_tree('.', format='git')
 
377
        self.build_tree_contents([('a', 'text for a\n')])
 
378
        tree.add(['a'])
 
379
        output, error = self.run_bzr('grep text')
 
380
        self.assertEqual(output, 'a:text for a\n')
 
381
        self.assertEqual(error, '')