/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-24 15:56:05 UTC
  • mto: This revision was merged to the branch mainline in revision 7214.
  • Revision ID: jelmer@jelmer.uk-20181124155605-g8yh28kpruubzgjk
Remove references to bzrtools.

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