~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright (C) 2006, 2007 Canonical Ltd
# Authors: Aaron Bentley
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


import os
import sys
from StringIO import StringIO

from bzrlib import (
    branch as _mod_branch,
    merge_directive,
    )
from bzrlib.bundle import serializer
from bzrlib.bzrdir import BzrDir
from bzrlib import tests


def read_bundle(fileobj):
    md = merge_directive.MergeDirective.from_lines(fileobj.readlines())
    return serializer.read_bundle(StringIO(md.get_raw_bundle()))


class TestSend(tests.TestCaseWithTransport):

    def make_trees(self):
        grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
        self.build_tree_contents([('grandparent/file1', 'grandparent')])
        grandparent_tree.add('file1')
        grandparent_tree.commit('initial commit', rev_id='revision1')
        parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
        parent_tree = parent_bzrdir.open_workingtree()
        parent_tree.commit('next commit', rev_id='revision2')
        branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
        self.build_tree_contents([('branch/file1', 'branch')])
        branch_tree.commit('last commit', rev_id='revision3')

    def test_uses_parent(self):
        """Parent location is used as a basis by default"""
        self.make_trees()
        os.chdir('grandparent')
        errmsg = self.run_bzr('send -o-', retcode=3)[1]
        self.assertContainsRe(errmsg, 'No submit branch known or specified')
        os.chdir('../branch')
        stdout, stderr = self.run_bzr('send -o-')
        self.assertEqual(stderr.count('Using saved parent location'), 1)
        br = read_bundle(StringIO(stdout))
        self.assertRevisions(br, ['revision3'])

    def test_bundle(self):
        """Bundle works like send, except -o is not required"""
        self.make_trees()
        os.chdir('grandparent')
        errmsg = self.run_bzr('bundle', retcode=3)[1]
        self.assertContainsRe(errmsg, 'No submit branch known or specified')
        os.chdir('../branch')
        stdout, stderr = self.run_bzr('bundle')
        self.assertEqual(stderr.count('Using saved parent location'), 1)
        br = read_bundle(StringIO(stdout))
        self.assertRevisions(br, ['revision3'])

    def assertRevisions(self, bi, expected):
        self.assertEqual(set(r.revision_id for r in bi.revisions),
            set(expected))

    def test_uses_submit(self):
        """Submit location can be used and set"""
        self.make_trees()
        os.chdir('branch')
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
        self.assertRevisions(br, ['revision3'])
        br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
        self.assertRevisions(br, ['revision3', 'revision2'])
        # submit location should be auto-remembered
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
        self.assertRevisions(br, ['revision3', 'revision2'])
        self.run_bzr('send ../parent -o-')
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
        self.assertRevisions(br, ['revision3', 'revision2'])
        self.run_bzr('send ../parent --remember -o-')
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
        self.assertRevisions(br, ['revision3'])
        err = self.run_bzr('send --remember -o-', retcode=3)[1]
        self.assertContainsRe(err,
                              '--remember requires a branch to be specified.')

    def test_revision_branch_interaction(self):
        self.make_trees()
        os.chdir('branch')
        bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
        self.assertRevisions(bi, ['revision3', 'revision2'])
        out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
        bi = read_bundle(out)
        self.assertRevisions(bi, ['revision2'])
        sio = StringIO(self.run_bzr('send -r -2..-1 -o-')[0])
        md = merge_directive.MergeDirective.from_lines(sio.readlines())
        self.assertEqual('revision2', md.base_revision_id)
        self.assertEqual('revision3', md.revision_id)
        sio.seek(0)
        bi = read_bundle(sio)
        self.assertRevisions(bi, ['revision2', 'revision3'])
        self.run_bzr('send ../grandparent -r -2..-1 -o-')

    def test_output(self):
        # check output for consistency
        # win32 stdout converts LF to CRLF,
        # which would break patch-based bundles
        self.make_trees()
        os.chdir('branch')
        stdout = self.run_bzr_subprocess('send -o-')[0]
        br = read_bundle(StringIO(stdout))
        self.assertRevisions(br, ['revision3'])

    def test_no_common_ancestor(self):
        foo = self.make_branch_and_tree('foo')
        foo.commit('rev a')
        bar = self.make_branch_and_tree('bar')
        bar.commit('rev b')
        os.chdir('foo')
        self.run_bzr('send ../bar -o-')

    def send_directive(self, args):
        sio = StringIO(self.run_bzr(['send', '-o-'] + args)[0])
        return merge_directive.MergeDirective.from_lines(sio.readlines())

    def test_content_options(self):
        """--no-patch and --no-bundle should work and be independant"""
        self.make_trees()
        os.chdir('branch')
        md = self.send_directive([])
        self.assertIsNot(None, md.bundle)
        self.assertIsNot(None, md.patch)

        md = self.send_directive(['--format=0.9'])
        self.assertIsNot(None, md.bundle)
        self.assertIsNot(None, md.patch)

        md = self.send_directive(['--no-patch'])
        self.assertIsNot(None, md.bundle)
        self.assertIs(None, md.patch)
        self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
                      'send --no-patch --format=0.9 -o-')

        md = self.send_directive(['--no-bundle', '.', '.'])
        self.assertIs(None, md.bundle)
        self.assertIsNot(None, md.patch)

        md = self.send_directive(['--no-bundle', '--format=0.9', '../parent',
                                  '.'])
        self.assertIs(None, md.bundle)
        self.assertIsNot(None, md.patch)

        md = self.send_directive(['--no-bundle', '--no-patch', '.', '.'])
        self.assertIs(None, md.bundle)
        self.assertIs(None, md.patch)

        md = self.send_directive(['--no-bundle', '--no-patch', '--format=0.9',
                                  '../parent', '.'])
        self.assertIs(None, md.bundle)
        self.assertIs(None, md.patch)

    def test_from_option(self):
        self.make_trees()
        self.run_bzr('send', retcode=3)
        md = self.send_directive(['--from', 'branch'])
        self.assertEqual('revision3', md.revision_id)
        md = self.send_directive(['-f', 'branch'])
        self.assertEqual('revision3', md.revision_id)

    def test_output_option(self):
        self.make_trees()
        stdout = self.run_bzr('send -f branch --output file1')[0]
        self.assertEqual('', stdout)
        md_file = open('file1', 'rb')
        self.addCleanup(md_file.close)
        self.assertContainsRe(md_file.read(), 'revision3')
        stdout = self.run_bzr('send -f branch --output -')[0]
        self.assertContainsRe(stdout, 'revision3')

    def test_note_revisions(self):
        self.make_trees()
        stderr = self.run_bzr('send -f branch --output file1')[1]
        self.assertContainsRe(stderr, r'Bundling 1 revision\(s\).\n')

    def test_mailto_option(self):
        self.make_trees()
        branch = _mod_branch.Branch.open('branch')
        branch.get_config().set_user_option('mail_client', 'editor')
        self.run_bzr_error(
            ('No mail-to address \\(--mail-to\\) or output \\(-o\\) specified',
            ), 'send -f branch')
        branch.get_config().set_user_option('mail_client', 'bogus')
        self.run_bzr('send -f branch -o-')
        self.run_bzr_error(('Unknown mail client: bogus',),
                           'send -f branch --mail-to jrandom@example.org')
        branch.get_config().set_user_option('submit_to', 'jrandom@example.org')
        self.run_bzr_error(('Unknown mail client: bogus',),
                           'send -f branch')

    def test_mailto_child_option(self):
        """Make sure that child_submit_to is used."""
        self.make_trees()
        branch = _mod_branch.Branch.open('branch')
        branch.get_config().set_user_option('mail_client', 'bogus')
        parent = _mod_branch.Branch.open('parent')
        parent.get_config().set_user_option('child_submit_to',
                           'somebody@example.org')
        self.run_bzr_error(('Unknown mail client: bogus',),
                           'send -f branch')

    def test_format(self):
        self.make_trees()
        s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
        md = merge_directive.MergeDirective.from_lines(s.readlines())
        self.assertIs(merge_directive.MergeDirective2, md.__class__)
        s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
        md = merge_directive.MergeDirective.from_lines(s.readlines())
        self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
            '# Bazaar revision bundle v0.9')
        s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
        md = merge_directive.MergeDirective.from_lines(s.readlines())
        self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
            '# Bazaar revision bundle v0.9')
        self.assertIs(merge_directive.MergeDirective, md.__class__)
        self.run_bzr_error(['Bad value .* for option .format.'],
                            'send -f branch -o- --format=0.999')[0]

    def test_format_child_option(self):
        self.make_trees()
        parent = _mod_branch.Branch.open('parent')
        parent.get_config().set_user_option('child_submit_format', '4')
        s = StringIO(self.run_bzr('send -f branch -o-')[0])
        md = merge_directive.MergeDirective.from_lines(s.readlines())
        self.assertIs(merge_directive.MergeDirective2, md.__class__)
        parent.get_config().set_user_option('child_submit_format', '0.9')
        s = StringIO(self.run_bzr('send -f branch -o-')[0])
        md = merge_directive.MergeDirective.from_lines(s.readlines())
        self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
            '# Bazaar revision bundle v0.9')
        s = StringIO(self.run_bzr('bundle -f branch -o-')[0])
        md = merge_directive.MergeDirective.from_lines(s.readlines())
        self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
            '# Bazaar revision bundle v0.9')
        self.assertIs(merge_directive.MergeDirective, md.__class__)
        parent.get_config().set_user_option('child_submit_format', '0.999')
        self.run_bzr_error(["No such send format '0.999'"],
                            'send -f branch -o-')[0]

    def test_message_option(self):
        self.make_trees()
        self.run_bzr('send', retcode=3)
        md = self.send_directive(['--from', 'branch'])
        self.assertIs(None, md.message)
        md = self.send_directive(['--from', 'branch', '-m', 'my message'])
        self.assertEqual('my message', md.message)

    def test_omitted_revision(self):
        self.make_trees()
        md = self.send_directive(['-r-2..', '--from', 'branch'])
        self.assertEqual('revision2', md.base_revision_id)
        self.assertEqual('revision3', md.revision_id)
        md = self.send_directive(['-r..3', '--from', 'branch',
                                 'grandparent'])
        self.assertEqual('revision1', md.base_revision_id)
        self.assertEqual('revision3', md.revision_id)

    def test_nonexistant_branch(self):
        if sys.platform == "win32":
            location = "C:/i/do/not/exist/"
        else:
            location = "/i/do/not/exist/"
        out, err = self.run_bzr(["send", "--from", location], retcode=3)
        self.assertEqual(out, '')
        self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)