/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/tests/blackbox/test_send.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-26 19:15:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7055.
  • Revision ID: jelmer@jelmer.uk-20180726191527-wniq205k6tzfo1xx
Install fastimport from git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
 
2
# Authors: Aaron Bentley
 
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
 
 
19
from ... import (
 
20
    branch,
 
21
    merge_directive,
 
22
    tests,
 
23
    )
 
24
from ...controldir import ControlDir
 
25
from ...bundle import serializer
 
26
from ...sixish import (
 
27
    BytesIO,
 
28
    )
 
29
from ...transport import memory
 
30
from .. import (
 
31
    scenarios,
 
32
    )
 
33
from ..matchers import ContainsNoVfsCalls
 
34
 
 
35
 
 
36
load_tests = scenarios.load_tests_apply_scenarios
 
37
 
 
38
 
 
39
class TestSendMixin(object):
 
40
 
 
41
    _default_command = ['send', '-o-']
 
42
    _default_wd = 'branch'
 
43
 
 
44
    def run_send(self, args, cmd=None, rc=0, wd=None, err_re=None):
 
45
        if cmd is None: cmd = self._default_command
 
46
        if wd is None: wd = self._default_wd
 
47
        if err_re is None: err_re = []
 
48
        return self.run_bzr(cmd + args, retcode=rc,
 
49
                            working_dir=wd,
 
50
                            error_regexes=err_re)
 
51
 
 
52
    def get_MD(self, args, cmd=None, wd='branch'):
 
53
        md = self.run_send(args, cmd=cmd, wd=wd)[0]
 
54
        out = BytesIO(md.encode('utf-8'))
 
55
        return merge_directive.MergeDirective.from_lines(out)
 
56
 
 
57
    def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
 
58
        md = self.get_MD(args, cmd=cmd, wd=wd)
 
59
        br = serializer.read_bundle(BytesIO(md.get_raw_bundle()))
 
60
        self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
 
61
 
 
62
 
 
63
class TestSend(tests.TestCaseWithTransport, TestSendMixin):
 
64
 
 
65
    def setUp(self):
 
66
        super(TestSend, self).setUp()
 
67
        grandparent_tree = ControlDir.create_standalone_workingtree(
 
68
            'grandparent')
 
69
        self.build_tree_contents([('grandparent/file1', b'grandparent')])
 
70
        grandparent_tree.add('file1')
 
71
        grandparent_tree.commit('initial commit', rev_id=b'rev1')
 
72
 
 
73
        parent_bzrdir = grandparent_tree.controldir.sprout('parent')
 
74
        parent_tree = parent_bzrdir.open_workingtree()
 
75
        parent_tree.commit('next commit', rev_id=b'rev2')
 
76
 
 
77
        branch_tree = parent_tree.controldir.sprout('branch').open_workingtree()
 
78
        self.build_tree_contents([('branch/file1', b'branch')])
 
79
        branch_tree.commit('last commit', rev_id=b'rev3')
 
80
 
 
81
    def assertFormatIs(self, fmt_string, md):
 
82
        self.assertEqual(fmt_string, md.get_raw_bundle().splitlines()[0])
 
83
 
 
84
    def test_uses_parent(self):
 
85
        """Parent location is used as a basis by default"""
 
86
        errmsg = self.run_send([], rc=3, wd='grandparent')[1]
 
87
        self.assertContainsRe(errmsg, 'No submit branch known or specified')
 
88
        stdout, stderr = self.run_send([])
 
89
        self.assertEqual(stderr.count('Using saved parent location'), 1)
 
90
        self.assertBundleContains([b'rev3'], [])
 
91
 
 
92
    def test_bundle(self):
 
93
        """Bundle works like send, except -o is not required"""
 
94
        errmsg = self.run_send([], cmd=['bundle'], rc=3, wd='grandparent')[1]
 
95
        self.assertContainsRe(errmsg, 'No submit branch known or specified')
 
96
        stdout, stderr = self.run_send([], cmd=['bundle'])
 
97
        self.assertEqual(stderr.count('Using saved parent location'), 1)
 
98
        self.assertBundleContains([b'rev3'], [], cmd=['bundle'])
 
99
 
 
100
    def test_uses_submit(self):
 
101
        """Submit location can be used and set"""
 
102
        self.assertBundleContains([b'rev3'], [])
 
103
        self.assertBundleContains([b'rev3', b'rev2'], ['../grandparent'])
 
104
        # submit location should be auto-remembered
 
105
        self.assertBundleContains([b'rev3', b'rev2'], [])
 
106
 
 
107
        self.run_send(['../parent'])
 
108
        # We still point to ../grandparent
 
109
        self.assertBundleContains([b'rev3', b'rev2'], [])
 
110
        # Remember parent now
 
111
        self.run_send(['../parent', '--remember'])
 
112
        # Now we point to parent
 
113
        self.assertBundleContains([b'rev3'], [])
 
114
 
 
115
        err = self.run_send(['--remember'], rc=3)[1]
 
116
        self.assertContainsRe(err,
 
117
                              '--remember requires a branch to be specified.')
 
118
 
 
119
    def test_revision_branch_interaction(self):
 
120
        self.assertBundleContains([b'rev3', b'rev2'], ['../grandparent'])
 
121
        self.assertBundleContains([b'rev2'], ['../grandparent', '-r-2'])
 
122
        self.assertBundleContains([b'rev3', b'rev2'],
 
123
                                  ['../grandparent', '-r-2..-1'])
 
124
        md = self.get_MD(['-r-2..-1'])
 
125
        self.assertEqual(b'rev2', md.base_revision_id)
 
126
        self.assertEqual(b'rev3', md.revision_id)
 
127
 
 
128
    def test_output(self):
 
129
        # check output for consistency
 
130
        # win32 stdout converts LF to CRLF,
 
131
        # which would break patch-based bundles
 
132
        self.assertBundleContains([b'rev3'], [])
 
133
 
 
134
    def test_no_common_ancestor(self):
 
135
        foo = self.make_branch_and_tree('foo')
 
136
        foo.commit('rev a')
 
137
        bar = self.make_branch_and_tree('bar')
 
138
        bar.commit('rev b')
 
139
        self.run_send(['--from', 'foo', '../bar'], wd='foo')
 
140
 
 
141
    def test_content_options(self):
 
142
        """--no-patch and --no-bundle should work and be independant"""
 
143
        md = self.get_MD([])
 
144
        self.assertIsNot(None, md.bundle)
 
145
        self.assertIsNot(None, md.patch)
 
146
 
 
147
        md = self.get_MD(['--format=0.9'])
 
148
        self.assertIsNot(None, md.bundle)
 
149
        self.assertIsNot(None, md.patch)
 
150
 
 
151
        md = self.get_MD(['--no-patch'])
 
152
        self.assertIsNot(None, md.bundle)
 
153
        self.assertIs(None, md.patch)
 
154
        self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
 
155
                           ['send', '--no-patch', '--format=0.9', '-o-'],
 
156
                           working_dir='branch')
 
157
        md = self.get_MD(['--no-bundle', '.', '.'])
 
158
        self.assertIs(None, md.bundle)
 
159
        self.assertIsNot(None, md.patch)
 
160
 
 
161
        md = self.get_MD(['--no-bundle', '--format=0.9', '../parent',
 
162
                                  '.'])
 
163
        self.assertIs(None, md.bundle)
 
164
        self.assertIsNot(None, md.patch)
 
165
 
 
166
        md = self.get_MD(['--no-bundle', '--no-patch', '.', '.'])
 
167
        self.assertIs(None, md.bundle)
 
168
        self.assertIs(None, md.patch)
 
169
 
 
170
        md = self.get_MD(['--no-bundle', '--no-patch', '--format=0.9',
 
171
                          '../parent', '.'])
 
172
        self.assertIs(None, md.bundle)
 
173
        self.assertIs(None, md.patch)
 
174
 
 
175
    def test_from_option(self):
 
176
        self.run_bzr('send', retcode=3)
 
177
        md = self.get_MD(['--from', 'branch'])
 
178
        self.assertEqual(b'rev3', md.revision_id)
 
179
        md = self.get_MD(['-f', 'branch'])
 
180
        self.assertEqual(b'rev3', md.revision_id)
 
181
 
 
182
    def test_output_option(self):
 
183
        stdout = self.run_bzr('send -f branch --output file1')[0]
 
184
        self.assertEqual('', stdout)
 
185
        md_file = open('file1', 'rb')
 
186
        self.addCleanup(md_file.close)
 
187
        self.assertContainsRe(md_file.read(), b'rev3')
 
188
        stdout = self.run_bzr('send -f branch --output -')[0]
 
189
        self.assertContainsRe(stdout, 'rev3')
 
190
 
 
191
    def test_note_revisions(self):
 
192
        stderr = self.run_send([])[1]
 
193
        self.assertEndsWith(stderr, '\nBundling 1 revision.\n')
 
194
 
 
195
    def test_mailto_option(self):
 
196
        b = branch.Branch.open('branch')
 
197
        b.get_config_stack().set('mail_client', 'editor')
 
198
        self.run_bzr_error(
 
199
            ('No mail-to address \\(--mail-to\\) or output \\(-o\\) specified',
 
200
            ), 'send -f branch')
 
201
        b.get_config_stack().set('mail_client', 'bogus')
 
202
        self.run_send([])
 
203
        self.run_bzr_error(('Bad value "bogus" for option "mail_client"',),
 
204
                           'send -f branch --mail-to jrandom@example.org')
 
205
        b.get_config_stack().set('submit_to', 'jrandom@example.org')
 
206
        self.run_bzr_error(('Bad value "bogus" for option "mail_client"',),
 
207
                           'send -f branch')
 
208
 
 
209
    def test_mailto_child_option(self):
 
210
        """Make sure that child_submit_to is used."""
 
211
        b = branch.Branch.open('branch')
 
212
        b.get_config_stack().set('mail_client', 'bogus')
 
213
        parent = branch.Branch.open('parent')
 
214
        parent.get_config_stack().set('child_submit_to', 'somebody@example.org')
 
215
        self.run_bzr_error(('Bad value "bogus" for option "mail_client"',),
 
216
                'send -f branch')
 
217
 
 
218
    def test_format(self):
 
219
        md = self.get_MD(['--format=4'])
 
220
        self.assertIs(merge_directive.MergeDirective2, md.__class__)
 
221
        self.assertFormatIs(b'# Bazaar revision bundle v4', md)
 
222
 
 
223
        md = self.get_MD(['--format=0.9'])
 
224
        self.assertFormatIs(b'# Bazaar revision bundle v0.9', md)
 
225
 
 
226
        md = self.get_MD(['--format=0.9'], cmd=['bundle'])
 
227
        self.assertFormatIs(b'# Bazaar revision bundle v0.9', md)
 
228
        self.assertIs(merge_directive.MergeDirective, md.__class__)
 
229
 
 
230
        self.run_bzr_error(['Bad value .* for option .format.'],
 
231
                            'send -f branch -o- --format=0.999')[0]
 
232
 
 
233
    def test_format_child_option(self):
 
234
        br = branch.Branch.open('parent')
 
235
        conf = br.get_config_stack()
 
236
        conf.set('child_submit_format', '4')
 
237
        md = self.get_MD([])
 
238
        self.assertIs(merge_directive.MergeDirective2, md.__class__)
 
239
 
 
240
        conf.set('child_submit_format', '0.9')
 
241
        md = self.get_MD([])
 
242
        self.assertFormatIs(b'# Bazaar revision bundle v0.9', md)
 
243
 
 
244
        md = self.get_MD([], cmd=['bundle'])
 
245
        self.assertFormatIs(b'# Bazaar revision bundle v0.9', md)
 
246
        self.assertIs(merge_directive.MergeDirective, md.__class__)
 
247
 
 
248
        conf.set('child_submit_format', '0.999')
 
249
        self.run_bzr_error(["No such send format '0.999'"],
 
250
                            'send -f branch -o-')[0]
 
251
 
 
252
    def test_message_option(self):
 
253
        self.run_bzr('send', retcode=3)
 
254
        md = self.get_MD([])
 
255
        self.assertIs(None, md.message)
 
256
        md = self.get_MD(['-m', 'my message'])
 
257
        self.assertEqual('my message', md.message)
 
258
 
 
259
    def test_omitted_revision(self):
 
260
        md = self.get_MD(['-r-2..'])
 
261
        self.assertEqual(b'rev2', md.base_revision_id)
 
262
        self.assertEqual(b'rev3', md.revision_id)
 
263
        md = self.get_MD(['-r..3', '--from', 'branch', 'grandparent'], wd='.')
 
264
        self.assertEqual(b'rev1', md.base_revision_id)
 
265
        self.assertEqual(b'rev3', md.revision_id)
 
266
 
 
267
    def test_nonexistant_branch(self):
 
268
        self.vfs_transport_factory = memory.MemoryServer
 
269
        location = self.get_url('absentdir/')
 
270
        out, err = self.run_bzr(["send", "--from", location], retcode=3)
 
271
        self.assertEqual(out, '')
 
272
        self.assertEqual(err, 'brz: ERROR: Not a branch: "%s".\n' % location)
 
273
 
 
274
 
 
275
class TestSendStrictMixin(TestSendMixin):
 
276
 
 
277
    def make_parent_and_local_branches(self):
 
278
        # Create a 'parent' branch as the base
 
279
        self.parent_tree = ControlDir.create_standalone_workingtree('parent')
 
280
        self.build_tree_contents([('parent/file', b'parent')])
 
281
        self.parent_tree.add('file')
 
282
        self.parent_tree.commit('first commit', rev_id=b'parent')
 
283
        # Branch 'local' from parent and do a change
 
284
        local_bzrdir = self.parent_tree.controldir.sprout('local')
 
285
        self.local_tree = local_bzrdir.open_workingtree()
 
286
        self.build_tree_contents([('local/file', b'local')])
 
287
        self.local_tree.commit('second commit', rev_id=b'local')
 
288
 
 
289
    _default_command = ['send', '-o-', '../parent']
 
290
    _default_wd = 'local'
 
291
    _default_sent_revs = [b'local']
 
292
    _default_errors = ['Working tree ".*/local/" has uncommitted '
 
293
                       'changes \\(See brz status\\)\\.',]
 
294
    _default_additional_error = 'Use --no-strict to force the send.\n'
 
295
    _default_additional_warning = 'Uncommitted changes will not be sent.'
 
296
 
 
297
    def set_config_send_strict(self, value):
 
298
        br = branch.Branch.open('local')
 
299
        br.get_config_stack().set('send_strict', value)
 
300
 
 
301
    def assertSendFails(self, args):
 
302
        out, err = self.run_send(args, rc=3, err_re=self._default_errors)
 
303
        self.assertContainsRe(err, self._default_additional_error)
 
304
 
 
305
    def assertSendSucceeds(self, args, revs=None, with_warning=False):
 
306
        if with_warning:
 
307
            err_re = self._default_errors
 
308
        else:
 
309
            err_re = []
 
310
        if revs is None:
 
311
            revs = self._default_sent_revs
 
312
        out, err = self.run_send(args, err_re=err_re)
 
313
        if len(revs) == 1:
 
314
            bundling_revs = 'Bundling %d revision.\n'% len(revs)
 
315
        else:
 
316
            bundling_revs = 'Bundling %d revisions.\n' % len(revs)
 
317
        if with_warning:
 
318
            self.assertContainsRe(err, self._default_additional_warning)
 
319
            self.assertEndsWith(err, bundling_revs)
 
320
        else:
 
321
            self.assertEqual(bundling_revs, err)
 
322
        md = merge_directive.MergeDirective.from_lines(BytesIO(out.encode('utf-8')))
 
323
        self.assertEqual(b'parent', md.base_revision_id)
 
324
        br = serializer.read_bundle(BytesIO(md.get_raw_bundle()))
 
325
        self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
 
326
 
 
327
 
 
328
class TestSendStrictWithoutChanges(tests.TestCaseWithTransport,
 
329
                                   TestSendStrictMixin):
 
330
 
 
331
    def setUp(self):
 
332
        super(TestSendStrictWithoutChanges, self).setUp()
 
333
        self.make_parent_and_local_branches()
 
334
 
 
335
    def test_send_without_workingtree(self):
 
336
        ControlDir.open("local").destroy_workingtree()
 
337
        self.assertSendSucceeds([])
 
338
 
 
339
    def test_send_default(self):
 
340
        self.assertSendSucceeds([])
 
341
 
 
342
    def test_send_strict(self):
 
343
        self.assertSendSucceeds(['--strict'])
 
344
 
 
345
    def test_send_no_strict(self):
 
346
        self.assertSendSucceeds(['--no-strict'])
 
347
 
 
348
    def test_send_config_var_strict(self):
 
349
        self.set_config_send_strict('true')
 
350
        self.assertSendSucceeds([])
 
351
 
 
352
    def test_send_config_var_no_strict(self):
 
353
        self.set_config_send_strict('false')
 
354
        self.assertSendSucceeds([])
 
355
 
 
356
 
 
357
class TestSendStrictWithChanges(tests.TestCaseWithTransport,
 
358
                                TestSendStrictMixin):
 
359
 
 
360
    # These are textually the same as test_push.strict_push_change_scenarios,
 
361
    # but since the functions are reimplemented here, the definitions are left
 
362
    # here too.
 
363
    scenarios = [
 
364
        ('uncommitted',
 
365
         dict(_changes_type='_uncommitted_changes')),
 
366
        ('pending_merges',
 
367
         dict(_changes_type='_pending_merges')),
 
368
        ('out-of-sync-trees',
 
369
         dict(_changes_type='_out_of_sync_trees')),
 
370
        ]
 
371
 
 
372
    _changes_type = None # Set by load_tests
 
373
 
 
374
    def setUp(self):
 
375
        super(TestSendStrictWithChanges, self).setUp()
 
376
        # load tests set _changes_types to the name of the method we want to
 
377
        # call now
 
378
        do_changes_func = getattr(self, self._changes_type)
 
379
        do_changes_func()
 
380
 
 
381
    def _uncommitted_changes(self):
 
382
        self.make_parent_and_local_branches()
 
383
        # Make a change without committing it
 
384
        self.build_tree_contents([('local/file', b'modified')])
 
385
 
 
386
    def _pending_merges(self):
 
387
        self.make_parent_and_local_branches()
 
388
        # Create 'other' branch containing a new file
 
389
        other_bzrdir = self.parent_tree.controldir.sprout('other')
 
390
        other_tree = other_bzrdir.open_workingtree()
 
391
        self.build_tree_contents([('other/other-file', b'other')])
 
392
        other_tree.add('other-file')
 
393
        other_tree.commit('other commit', rev_id=b'other')
 
394
        # Merge and revert, leaving a pending merge
 
395
        self.local_tree.merge_from_branch(other_tree.branch)
 
396
        self.local_tree.revert(filenames=['other-file'], backups=False)
 
397
 
 
398
    def _out_of_sync_trees(self):
 
399
        self.make_parent_and_local_branches()
 
400
        self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
 
401
        # Make a change and commit it
 
402
        self.build_tree_contents([('local/file', b'modified in local')])
 
403
        self.local_tree.commit('modify file', rev_id=b'modified-in-local')
 
404
        # Exercise commands from the checkout directory
 
405
        self._default_wd = 'checkout'
 
406
        self._default_errors = ["Working tree is out of date, please run"
 
407
                                " 'brz update'\\.",]
 
408
        self._default_sent_revs = [b'modified-in-local', b'local']
 
409
 
 
410
    def test_send_default(self):
 
411
        self.assertSendSucceeds([], with_warning=True)
 
412
 
 
413
    def test_send_with_revision(self):
 
414
        self.assertSendSucceeds(['-r', 'revid:local'], revs=[b'local'])
 
415
 
 
416
    def test_send_no_strict(self):
 
417
        self.assertSendSucceeds(['--no-strict'])
 
418
 
 
419
    def test_send_strict_with_changes(self):
 
420
        self.assertSendFails(['--strict'])
 
421
 
 
422
    def test_send_respect_config_var_strict(self):
 
423
        self.set_config_send_strict('true')
 
424
        self.assertSendFails([])
 
425
        self.assertSendSucceeds(['--no-strict'])
 
426
 
 
427
    def test_send_bogus_config_var_ignored(self):
 
428
        self.set_config_send_strict("I'm unsure")
 
429
        self.assertSendSucceeds([], with_warning=True)
 
430
 
 
431
    def test_send_no_strict_command_line_override_config(self):
 
432
        self.set_config_send_strict('true')
 
433
        self.assertSendFails([])
 
434
        self.assertSendSucceeds(['--no-strict'])
 
435
 
 
436
    def test_send_strict_command_line_override_config(self):
 
437
        self.set_config_send_strict('false')
 
438
        self.assertSendSucceeds([])
 
439
        self.assertSendFails(['--strict'])
 
440
 
 
441
 
 
442
class TestBundleStrictWithoutChanges(TestSendStrictWithoutChanges):
 
443
 
 
444
    _default_command = ['bundle-revisions', '../parent']
 
445
 
 
446
 
 
447
class TestSmartServerSend(tests.TestCaseWithTransport):
 
448
 
 
449
    def test_send(self):
 
450
        self.setup_smart_server_with_call_log()
 
451
        t = self.make_branch_and_tree('branch')
 
452
        self.build_tree_contents([('branch/foo', b'thecontents')])
 
453
        t.add("foo")
 
454
        t.commit("message")
 
455
        local = t.controldir.sprout('local-branch').open_workingtree()
 
456
        self.build_tree_contents([('branch/foo', b'thenewcontents')])
 
457
        local.commit("anothermessage")
 
458
        self.reset_smart_call_log()
 
459
        out, err = self.run_bzr(
 
460
            ['send', '-o', 'x.diff', self.get_url('branch')], working_dir='local-branch')
 
461
        # This figure represent the amount of work to perform this use case. It
 
462
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
463
        # being too low. If rpc_count increases, more network roundtrips have
 
464
        # become necessary for this use case. Please do not adjust this number
 
465
        # upwards without agreement from bzr's network support maintainers.
 
466
        self.assertLength(7, self.hpss_calls)
 
467
        self.assertLength(1, self.hpss_connections)
 
468
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)