1
# Copyright (C) 2006-2011 Canonical Ltd
2
# Authors: Aaron Bentley
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.
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.
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
20
from cStringIO import StringIO
28
from bzrlib.bundle import serializer
29
from bzrlib.transport import memory
30
from bzrlib.tests import (
36
load_tests = scenarios.load_tests_apply_scenarios
39
class TestSendMixin(object):
41
_default_command = ['send', '-o-']
42
_default_wd = 'branch'
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,
52
def get_MD(self, args, cmd=None, wd='branch'):
53
out = StringIO(self.run_send(args, cmd=cmd, wd=wd)[0])
54
return merge_directive.MergeDirective.from_lines(out)
56
def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
57
md = self.get_MD(args, cmd=cmd, wd=wd)
58
br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
59
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
62
class TestSend(tests.TestCaseWithTransport, TestSendMixin):
65
super(TestSend, self).setUp()
66
grandparent_tree = bzrdir.BzrDir.create_standalone_workingtree(
68
self.build_tree_contents([('grandparent/file1', 'grandparent')])
69
grandparent_tree.add('file1')
70
grandparent_tree.commit('initial commit', rev_id='rev1')
72
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
73
parent_tree = parent_bzrdir.open_workingtree()
74
parent_tree.commit('next commit', rev_id='rev2')
76
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
77
self.build_tree_contents([('branch/file1', 'branch')])
78
branch_tree.commit('last commit', rev_id='rev3')
80
def assertFormatIs(self, fmt_string, md):
81
self.assertEqual(fmt_string, md.get_raw_bundle().splitlines()[0])
83
def test_uses_parent(self):
84
"""Parent location is used as a basis by default"""
85
errmsg = self.run_send([], rc=3, wd='grandparent')[1]
86
self.assertContainsRe(errmsg, 'No submit branch known or specified')
87
stdout, stderr = self.run_send([])
88
self.assertEqual(stderr.count('Using saved parent location'), 1)
89
self.assertBundleContains(['rev3'], [])
91
def test_bundle(self):
92
"""Bundle works like send, except -o is not required"""
93
errmsg = self.run_send([], cmd=['bundle'], rc=3, wd='grandparent')[1]
94
self.assertContainsRe(errmsg, 'No submit branch known or specified')
95
stdout, stderr = self.run_send([], cmd=['bundle'])
96
self.assertEqual(stderr.count('Using saved parent location'), 1)
97
self.assertBundleContains(['rev3'], [], cmd=['bundle'])
99
def test_uses_submit(self):
100
"""Submit location can be used and set"""
101
self.assertBundleContains(['rev3'], [])
102
self.assertBundleContains(['rev3', 'rev2'], ['../grandparent'])
103
# submit location should be auto-remembered
104
self.assertBundleContains(['rev3', 'rev2'], [])
106
self.run_send(['../parent'])
107
# We still point to ../grandparent
108
self.assertBundleContains(['rev3', 'rev2'], [])
109
# Remember parent now
110
self.run_send(['../parent', '--remember'])
111
# Now we point to parent
112
self.assertBundleContains(['rev3'], [])
114
err = self.run_send(['--remember'], rc=3)[1]
115
self.assertContainsRe(err,
116
'--remember requires a branch to be specified.')
118
def test_revision_branch_interaction(self):
119
self.assertBundleContains(['rev3', 'rev2'], ['../grandparent'])
120
self.assertBundleContains(['rev2'], ['../grandparent', '-r-2'])
121
self.assertBundleContains(['rev3', 'rev2'],
122
['../grandparent', '-r-2..-1'])
123
md = self.get_MD(['-r-2..-1'])
124
self.assertEqual('rev2', md.base_revision_id)
125
self.assertEqual('rev3', md.revision_id)
127
def test_output(self):
128
# check output for consistency
129
# win32 stdout converts LF to CRLF,
130
# which would break patch-based bundles
131
self.assertBundleContains(['rev3'], [])
133
def test_no_common_ancestor(self):
134
foo = self.make_branch_and_tree('foo')
136
bar = self.make_branch_and_tree('bar')
138
self.run_send(['--from', 'foo', '../bar'], wd='foo')
140
def test_content_options(self):
141
"""--no-patch and --no-bundle should work and be independant"""
143
self.assertIsNot(None, md.bundle)
144
self.assertIsNot(None, md.patch)
146
md = self.get_MD(['--format=0.9'])
147
self.assertIsNot(None, md.bundle)
148
self.assertIsNot(None, md.patch)
150
md = self.get_MD(['--no-patch'])
151
self.assertIsNot(None, md.bundle)
152
self.assertIs(None, md.patch)
153
self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
154
['send', '--no-patch', '--format=0.9', '-o-'],
155
working_dir='branch')
156
md = self.get_MD(['--no-bundle', '.', '.'])
157
self.assertIs(None, md.bundle)
158
self.assertIsNot(None, md.patch)
160
md = self.get_MD(['--no-bundle', '--format=0.9', '../parent',
162
self.assertIs(None, md.bundle)
163
self.assertIsNot(None, md.patch)
165
md = self.get_MD(['--no-bundle', '--no-patch', '.', '.'])
166
self.assertIs(None, md.bundle)
167
self.assertIs(None, md.patch)
169
md = self.get_MD(['--no-bundle', '--no-patch', '--format=0.9',
171
self.assertIs(None, md.bundle)
172
self.assertIs(None, md.patch)
174
def test_from_option(self):
175
self.run_bzr('send', retcode=3)
176
md = self.get_MD(['--from', 'branch'])
177
self.assertEqual('rev3', md.revision_id)
178
md = self.get_MD(['-f', 'branch'])
179
self.assertEqual('rev3', md.revision_id)
181
def test_output_option(self):
182
stdout = self.run_bzr('send -f branch --output file1')[0]
183
self.assertEqual('', stdout)
184
md_file = open('file1', 'rb')
185
self.addCleanup(md_file.close)
186
self.assertContainsRe(md_file.read(), 'rev3')
187
stdout = self.run_bzr('send -f branch --output -')[0]
188
self.assertContainsRe(stdout, 'rev3')
190
def test_note_revisions(self):
191
stderr = self.run_send([])[1]
192
self.assertEndsWith(stderr, '\nBundling 1 revision.\n')
194
def test_mailto_option(self):
195
b = branch.Branch.open('branch')
196
b.get_config().set_user_option('mail_client', 'editor')
198
('No mail-to address \\(--mail-to\\) or output \\(-o\\) specified',
200
b.get_config().set_user_option('mail_client', 'bogus')
202
self.run_bzr_error(('Unknown mail client: bogus',),
203
'send -f branch --mail-to jrandom@example.org')
204
b.get_config().set_user_option('submit_to', 'jrandom@example.org')
205
self.run_bzr_error(('Unknown mail client: bogus',),
208
def test_mailto_child_option(self):
209
"""Make sure that child_submit_to is used."""
210
b = branch.Branch.open('branch')
211
b.get_config().set_user_option('mail_client', 'bogus')
212
parent = branch.Branch.open('parent')
213
parent.get_config().set_user_option('child_submit_to',
214
'somebody@example.org')
215
self.run_bzr_error(('Unknown mail client: bogus',),
218
def test_format(self):
219
md = self.get_MD(['--format=4'])
220
self.assertIs(merge_directive.MergeDirective2, md.__class__)
221
self.assertFormatIs('# Bazaar revision bundle v4', md)
223
md = self.get_MD(['--format=0.9'])
224
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
226
md = self.get_MD(['--format=0.9'], cmd=['bundle'])
227
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
228
self.assertIs(merge_directive.MergeDirective, md.__class__)
230
self.run_bzr_error(['Bad value .* for option .format.'],
231
'send -f branch -o- --format=0.999')[0]
233
def test_format_child_option(self):
234
parent_config = branch.Branch.open('parent').get_config()
235
parent_config.set_user_option('child_submit_format', '4')
237
self.assertIs(merge_directive.MergeDirective2, md.__class__)
239
parent_config.set_user_option('child_submit_format', '0.9')
241
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
243
md = self.get_MD([], cmd=['bundle'])
244
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
245
self.assertIs(merge_directive.MergeDirective, md.__class__)
247
parent_config.set_user_option('child_submit_format', '0.999')
248
self.run_bzr_error(["No such send format '0.999'"],
249
'send -f branch -o-')[0]
251
def test_message_option(self):
252
self.run_bzr('send', retcode=3)
254
self.assertIs(None, md.message)
255
md = self.get_MD(['-m', 'my message'])
256
self.assertEqual('my message', md.message)
258
def test_omitted_revision(self):
259
md = self.get_MD(['-r-2..'])
260
self.assertEqual('rev2', md.base_revision_id)
261
self.assertEqual('rev3', md.revision_id)
262
md = self.get_MD(['-r..3', '--from', 'branch', 'grandparent'], wd='.')
263
self.assertEqual('rev1', md.base_revision_id)
264
self.assertEqual('rev3', md.revision_id)
266
def test_nonexistant_branch(self):
267
self.vfs_transport_factory = memory.MemoryServer
268
location = self.get_url('absentdir/')
269
out, err = self.run_bzr(["send", "--from", location], retcode=3)
270
self.assertEqual(out, '')
271
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)
274
class TestSendStrictMixin(TestSendMixin):
276
def make_parent_and_local_branches(self):
277
# Create a 'parent' branch as the base
278
self.parent_tree = bzrdir.BzrDir.create_standalone_workingtree('parent')
279
self.build_tree_contents([('parent/file', 'parent')])
280
self.parent_tree.add('file')
281
self.parent_tree.commit('first commit', rev_id='parent')
282
# Branch 'local' from parent and do a change
283
local_bzrdir = self.parent_tree.bzrdir.sprout('local')
284
self.local_tree = local_bzrdir.open_workingtree()
285
self.build_tree_contents([('local/file', 'local')])
286
self.local_tree.commit('second commit', rev_id='local')
288
_default_command = ['send', '-o-', '../parent']
289
_default_wd = 'local'
290
_default_sent_revs = ['local']
291
_default_errors = ['Working tree ".*/local/" has uncommitted '
292
'changes \(See bzr status\)\.',]
293
_default_additional_error = 'Use --no-strict to force the send.\n'
294
_default_additional_warning = 'Uncommitted changes will not be sent.'
296
def set_config_send_strict(self, value):
297
# set config var (any of bazaar.conf, locations.conf, branch.conf
299
conf = self.local_tree.branch.get_config_stack()
300
conf.set('send_strict', value)
302
def assertSendFails(self, args):
303
out, err = self.run_send(args, rc=3, err_re=self._default_errors)
304
self.assertContainsRe(err, self._default_additional_error)
306
def assertSendSucceeds(self, args, revs=None, with_warning=False):
308
err_re = self._default_errors
312
revs = self._default_sent_revs
313
out, err = self.run_send(args, err_re=err_re)
315
bundling_revs = 'Bundling %d revision.\n'% len(revs)
317
bundling_revs = 'Bundling %d revisions.\n' % len(revs)
319
self.assertContainsRe(err, self._default_additional_warning)
320
self.assertEndsWith(err, bundling_revs)
322
self.assertEquals(bundling_revs, err)
323
md = merge_directive.MergeDirective.from_lines(StringIO(out))
324
self.assertEqual('parent', md.base_revision_id)
325
br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
326
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
329
class TestSendStrictWithoutChanges(tests.TestCaseWithTransport,
330
TestSendStrictMixin):
333
super(TestSendStrictWithoutChanges, self).setUp()
334
self.make_parent_and_local_branches()
336
def test_send_default(self):
337
self.assertSendSucceeds([])
339
def test_send_strict(self):
340
self.assertSendSucceeds(['--strict'])
342
def test_send_no_strict(self):
343
self.assertSendSucceeds(['--no-strict'])
345
def test_send_config_var_strict(self):
346
self.set_config_send_strict('true')
347
self.assertSendSucceeds([])
349
def test_send_config_var_no_strict(self):
350
self.set_config_send_strict('false')
351
self.assertSendSucceeds([])
354
class TestSendStrictWithChanges(tests.TestCaseWithTransport,
355
TestSendStrictMixin):
357
# These are textually the same as test_push.strict_push_change_scenarios,
358
# but since the functions are reimplemented here, the definitions are left
362
dict(_changes_type='_uncommitted_changes')),
364
dict(_changes_type='_pending_merges')),
365
('out-of-sync-trees',
366
dict(_changes_type='_out_of_sync_trees')),
369
_changes_type = None # Set by load_tests
372
super(TestSendStrictWithChanges, self).setUp()
373
# load tests set _changes_types to the name of the method we want to
375
do_changes_func = getattr(self, self._changes_type)
378
def _uncommitted_changes(self):
379
self.make_parent_and_local_branches()
380
# Make a change without committing it
381
self.build_tree_contents([('local/file', 'modified')])
383
def _pending_merges(self):
384
self.make_parent_and_local_branches()
385
# Create 'other' branch containing a new file
386
other_bzrdir = self.parent_tree.bzrdir.sprout('other')
387
other_tree = other_bzrdir.open_workingtree()
388
self.build_tree_contents([('other/other-file', 'other')])
389
other_tree.add('other-file')
390
other_tree.commit('other commit', rev_id='other')
391
# Merge and revert, leaving a pending merge
392
self.local_tree.merge_from_branch(other_tree.branch)
393
self.local_tree.revert(filenames=['other-file'], backups=False)
395
def _out_of_sync_trees(self):
396
self.make_parent_and_local_branches()
397
self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
398
# Make a change and commit it
399
self.build_tree_contents([('local/file', 'modified in local')])
400
self.local_tree.commit('modify file', rev_id='modified-in-local')
401
# Exercise commands from the checkout directory
402
self._default_wd = 'checkout'
403
self._default_errors = ["Working tree is out of date, please run"
405
self._default_sent_revs = ['modified-in-local', 'local']
407
def test_send_default(self):
408
self.assertSendSucceeds([], with_warning=True)
410
def test_send_with_revision(self):
411
self.assertSendSucceeds(['-r', 'revid:local'], revs=['local'])
413
def test_send_no_strict(self):
414
self.assertSendSucceeds(['--no-strict'])
416
def test_send_strict_with_changes(self):
417
self.assertSendFails(['--strict'])
419
def test_send_respect_config_var_strict(self):
420
self.set_config_send_strict('true')
421
self.assertSendFails([])
422
self.assertSendSucceeds(['--no-strict'])
424
def test_send_bogus_config_var_ignored(self):
425
self.set_config_send_strict("I'm unsure")
426
self.assertSendSucceeds([], with_warning=True)
428
def test_send_no_strict_command_line_override_config(self):
429
self.set_config_send_strict('true')
430
self.assertSendFails([])
431
self.assertSendSucceeds(['--no-strict'])
433
def test_send_strict_command_line_override_config(self):
434
self.set_config_send_strict('false')
435
self.assertSendSucceeds([])
436
self.assertSendFails(['--strict'])
439
class TestBundleStrictWithoutChanges(TestSendStrictWithoutChanges):
441
_default_command = ['bundle-revisions', '../parent']