/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
1
# Copyright (C) 2006, 2007 Canonical Ltd
1185.84.4 by Aaron Bentley
Use parent branch as default base branch
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19
import os
20
from StringIO import StringIO
21
2681.1.13 by Aaron Bentley
Add support for submit_to config option
22
from bzrlib import (
23
    branch as _mod_branch,
24
    merge_directive,
25
    )
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
26
from bzrlib.bundle import serializer
1185.84.4 by Aaron Bentley
Use parent branch as default base branch
27
from bzrlib.bzrdir import BzrDir
2490.2.28 by Aaron Bentley
Fix handling of null revision
28
from bzrlib import tests
29
30
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
31
def read_bundle(fileobj):
32
    md = merge_directive.MergeDirective.from_lines(fileobj.readlines())
33
    return serializer.read_bundle(StringIO(md.get_raw_bundle()))
34
35
2659.1.3 by Aaron Bentley
Fix test_uses_submit testcase
36
class TestSend(tests.TestCaseWithTransport):
1185.84.4 by Aaron Bentley
Use parent branch as default base branch
37
1793.2.13 by Aaron Bentley
Use single make function for tests
38
    def make_trees(self):
39
        grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
40
        self.build_tree_contents([('grandparent/file1', 'grandparent')])
41
        grandparent_tree.add('file1')
1793.2.13 by Aaron Bentley
Use single make function for tests
42
        grandparent_tree.commit('initial commit', rev_id='revision1')
43
        parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
44
        parent_tree = parent_bzrdir.open_workingtree()
45
        parent_tree.commit('next commit', rev_id='revision2')
46
        branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
47
        self.build_tree_contents([('branch/file1', 'branch')])
1793.2.13 by Aaron Bentley
Use single make function for tests
48
        branch_tree.commit('last commit', rev_id='revision3')
49
1185.84.4 by Aaron Bentley
Use parent branch as default base branch
50
    def test_uses_parent(self):
51
        """Parent location is used as a basis by default"""
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
52
        self.make_trees()
1793.2.13 by Aaron Bentley
Use single make function for tests
53
        os.chdir('grandparent')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
54
        errmsg = self.run_bzr('send -o-', retcode=3)[1]
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
55
        self.assertContainsRe(errmsg, 'No submit branch known or specified')
1185.84.4 by Aaron Bentley
Use parent branch as default base branch
56
        os.chdir('../branch')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
57
        stdout, stderr = self.run_bzr('send -o-')
1793.2.12 by Aaron Bentley
Avoid duplicating the 'Using specified base' message
58
        self.assertEqual(stderr.count('Using saved location'), 1)
59
        br = read_bundle(StringIO(stdout))
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
60
        self.assertRevisions(br, ['revision3'])
61
2681.1.1 by Aaron Bentley
Split 'send' into 'send' and 'bundle'.
62
    def test_bundle(self):
63
        """Bundle works like send, except -o is not required"""
64
        self.make_trees()
65
        os.chdir('grandparent')
66
        errmsg = self.run_bzr('bundle', retcode=3)[1]
67
        self.assertContainsRe(errmsg, 'No submit branch known or specified')
68
        os.chdir('../branch')
69
        stdout, stderr = self.run_bzr('bundle')
70
        self.assertEqual(stderr.count('Using saved location'), 1)
71
        br = read_bundle(StringIO(stdout))
72
        self.assertRevisions(br, ['revision3'])
73
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
74
    def assertRevisions(self, bi, expected):
2520.4.82 by Aaron Bentley
Fix tests to stop expecting bundles to build trees
75
        self.assertEqual(set(r.revision_id for r in bi.revisions),
76
            set(expected))
1804.1.1 by Aaron Bentley
Add support for submit location to bundles
77
78
    def test_uses_submit(self):
79
        """Submit location can be used and set"""
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
80
        self.make_trees()
1804.1.1 by Aaron Bentley
Add support for submit location to bundles
81
        os.chdir('branch')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
82
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
83
        self.assertRevisions(br, ['revision3'])
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
84
        br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
85
        self.assertRevisions(br, ['revision3', 'revision2'])
1804.1.1 by Aaron Bentley
Add support for submit location to bundles
86
        # submit location should be auto-remembered
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
87
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
88
        self.assertRevisions(br, ['revision3', 'revision2'])
89
        self.run_bzr('send ../parent -o-')
90
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
91
        self.assertRevisions(br, ['revision3', 'revision2'])
92
        self.run_bzr('send ../parent --remember -o-')
93
        br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
94
        self.assertRevisions(br, ['revision3'])
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
95
        err = self.run_bzr('send --remember -o-', retcode=3)[1]
1804.1.1 by Aaron Bentley
Add support for submit location to bundles
96
        self.assertContainsRe(err, 
97
                              '--remember requires a branch to be specified.')
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
98
99
    def test_revision_branch_interaction(self):
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
100
        self.make_trees()
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
101
        os.chdir('branch')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
102
        bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
103
        self.assertRevisions(bi, ['revision3', 'revision2'])
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
104
        out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
1793.2.14 by Aaron Bentley
Clean up bundle revision specification
105
        bi = read_bundle(out)
106
        self.assertRevisions(bi, ['revision2'])
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
107
        sio = StringIO(self.run_bzr('send -r -2..-1 -o-')[0])
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
108
        md = merge_directive.MergeDirective.from_lines(sio.readlines())
109
        self.assertEqual('revision2', md.base_revision_id)
110
        self.assertEqual('revision3', md.revision_id)
111
        sio.seek(0)
112
        bi = read_bundle(sio)
113
        self.assertRevisions(bi, ['revision2', 'revision3'])
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
114
        self.run_bzr('send ../grandparent -r -2..-1 -o-')
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
115
116
    def test_output(self):
117
        # check output for consistency
2178.4.5 by Alexander Belchenko
Spell-checking (thanks to Aaron)
118
        # win32 stdout converts LF to CRLF,
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
119
        # which would break patch-based bundles
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
120
        self.make_trees()        
121
        os.chdir('branch')
2665.4.1 by Aaron Bentley
teach run_bzr_subprocess to accept either a list of strings or a string
122
        stdout = self.run_bzr_subprocess('send -o-')[0]
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
123
        br = read_bundle(StringIO(stdout))
124
        self.assertRevisions(br, ['revision3'])
2490.2.28 by Aaron Bentley
Fix handling of null revision
125
126
    def test_no_common_ancestor(self):
127
        foo = self.make_branch_and_tree('foo')
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
128
        foo.commit('rev a')
2490.2.28 by Aaron Bentley
Fix handling of null revision
129
        bar = self.make_branch_and_tree('bar')
2520.5.4 by Aaron Bentley
Replace 'bundle-revisions' with 'submit' command
130
        bar.commit('rev b')
2490.2.28 by Aaron Bentley
Fix handling of null revision
131
        os.chdir('foo')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
132
        self.run_bzr('send ../bar -o-')
2520.4.121 by Aaron Bentley
Polish up submit command
133
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
134
    def send_directive(self, args):
135
        sio = StringIO(self.run_bzr(['send', '-o-'] + args)[0])
2520.4.121 by Aaron Bentley
Polish up submit command
136
        return merge_directive.MergeDirective.from_lines(sio.readlines())
137
138
    def test_content_options(self):
139
        """--no-patch and --no-bundle should work and be independant"""
140
        self.make_trees()
141
        os.chdir('branch')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
142
        md = self.send_directive([])
2520.4.121 by Aaron Bentley
Polish up submit command
143
        self.assertIsNot(None, md.bundle)
144
        self.assertIsNot(None, md.patch)
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
145
146
        md = self.send_directive(['--format=0.9'])
147
        self.assertIsNot(None, md.bundle)
148
        self.assertIsNot(None, md.patch)
149
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
150
        md = self.send_directive(['--no-patch'])
2520.4.121 by Aaron Bentley
Polish up submit command
151
        self.assertIsNot(None, md.bundle)
152
        self.assertIs(None, md.patch)
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
153
        self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
154
                      'send --no-patch --format=0.9 -o-')
155
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
156
        md = self.send_directive(['--no-bundle', '.', '.'])
2520.4.121 by Aaron Bentley
Polish up submit command
157
        self.assertIs(None, md.bundle)
158
        self.assertIsNot(None, md.patch)
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
159
160
        md = self.send_directive(['--no-bundle', '--format=0.9', '../parent',
161
                                  '.'])
162
        self.assertIs(None, md.bundle)
163
        self.assertIsNot(None, md.patch)
164
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
165
        md = self.send_directive(['--no-bundle', '--no-patch', '.', '.'])
2520.4.121 by Aaron Bentley
Polish up submit command
166
        self.assertIs(None, md.bundle)
167
        self.assertIs(None, md.patch)
168
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
169
        md = self.send_directive(['--no-bundle', '--no-patch', '--format=0.9',
170
                                  '../parent', '.'])
171
        self.assertIs(None, md.bundle)
172
        self.assertIs(None, md.patch)
173
2520.4.121 by Aaron Bentley
Polish up submit command
174
    def test_from_option(self):
175
        self.make_trees()
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
176
        self.run_bzr('send', retcode=3)
177
        md = self.send_directive(['--from', 'branch'])
2520.4.121 by Aaron Bentley
Polish up submit command
178
        self.assertEqual('revision3', md.revision_id)
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
179
        md = self.send_directive(['-f', 'branch'])
2520.4.121 by Aaron Bentley
Polish up submit command
180
        self.assertEqual('revision3', md.revision_id)
181
182
    def test_output_option(self):
183
        self.make_trees()
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
184
        stdout = self.run_bzr('send -f branch --output file1')[0]
2520.4.121 by Aaron Bentley
Polish up submit command
185
        self.assertEqual('', stdout)
186
        md_file = open('file1', 'rb')
187
        self.addCleanup(md_file.close)
188
        self.assertContainsRe(md_file.read(), 'revision3')
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
189
        stdout = self.run_bzr('send -f branch --output -')[0]
190
        self.assertContainsRe(stdout, 'revision3')
2520.4.132 by Aaron Bentley
Merge from bzr.dev
191
2681.1.13 by Aaron Bentley
Add support for submit_to config option
192
    def test_mailto_option(self):
2654.3.1 by Aaron Bentley
Rename submit to send, make -o required, support -o- for stdout
193
        self.make_trees()
2681.1.13 by Aaron Bentley
Add support for submit_to config option
194
        branch = _mod_branch.Branch.open('branch')
3042.1.1 by Lukáš Lalinský
Make mail-to address in ``bzr send`` optional for interactive mail clients.
195
        branch.get_config().set_user_option('mail_client', 'editor')
196
        self.run_bzr_error(('No mail-to address specified',), 'send -f branch')
2681.1.13 by Aaron Bentley
Add support for submit_to config option
197
        branch.get_config().set_user_option('mail_client', 'bogus')
2681.1.12 by Aaron Bentley
Fix bundle command
198
        self.run_bzr('send -f branch -o-')
2681.1.13 by Aaron Bentley
Add support for submit_to config option
199
        self.run_bzr_error(('Unknown mail client: bogus',),
200
                           'send -f branch --mail-to jrandom@example.org')
201
        branch.get_config().set_user_option('submit_to', 'jrandom@example.org')
202
        self.run_bzr_error(('Unknown mail client: bogus',),
203
                           'send -f branch')
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
204
205
    def test_format(self):
206
        self.make_trees()
207
        s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
208
        md = merge_directive.MergeDirective.from_lines(s.readlines())
209
        self.assertIs(merge_directive.MergeDirective2, md.__class__)
210
        s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
211
        md = merge_directive.MergeDirective.from_lines(s.readlines())
2681.1.3 by Aaron Bentley
Ensure that a format 0.9 bundle is generated
212
        self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
213
            '# Bazaar revision bundle v0.9')
214
        s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
215
        md = merge_directive.MergeDirective.from_lines(s.readlines())
216
        self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
217
            '# Bazaar revision bundle v0.9')
2681.1.2 by Aaron Bentley
Add support for selecting bundle format
218
        self.assertIs(merge_directive.MergeDirective, md.__class__)
219
        self.run_bzr_error(['Bad value .* for option .format.'],
220
                            'send -f branch -o- --format=0.999')[0]
2681.1.9 by Aaron Bentley
Add support for mail-from-editor
221
222
    def test_message_option(self):
223
        self.make_trees()
224
        self.run_bzr('send', retcode=3)
225
        md = self.send_directive(['--from', 'branch'])
226
        self.assertIs(None, md.message)
227
        md = self.send_directive(['--from', 'branch', '-m', 'my message'])
228
        self.assertEqual('my message', md.message)
2747.3.1 by Aaron Bentley
'send' and 'bundle' now handle partial ranges correctly (#61685)
229
230
    def test_omitted_revision(self):
231
        self.make_trees()
232
        md = self.send_directive(['-r-2..', '--from', 'branch'])
233
        self.assertEqual('revision2', md.base_revision_id)
234
        self.assertEqual('revision3', md.revision_id)
235
        md = self.send_directive(['-r..3', '--from', 'branch',
236
                                 'grandparent'])
237
        self.assertEqual('revision1', md.base_revision_id)
238
        self.assertEqual('revision3', md.revision_id)