~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
# Copyright (C) 2007 Canonical Ltd
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os
import smtplib

from bzrlib import (
    gpg,
    merge_directive,
    tests,
    workingtree,
    )


EMAIL1 = """From: "J. Random Hacker" <jrandom@example.com>
Subject: bar
To: pqm@example.com
User-Agent: Bazaar \(.*\)

# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
# revision_id: bar-id
# target_branch: ../tree2
# testament_sha1: .*
# timestamp: .*
# source_branch: .
#"""


class TestMergeDirective(tests.TestCaseWithTransport):

    def prepare_merge_directive(self):
        self.tree1 = self.make_branch_and_tree('tree1')
        self.build_tree_contents([('tree1/file', 'a\nb\nc\nd\n')])
        self.tree1.branch.get_config().set_user_option('email',
            'J. Random Hacker <jrandom@example.com>')
        self.tree1.add('file')
        self.tree1.commit('foo', rev_id='foo-id')
        self.tree2 = self.tree1.bzrdir.sprout('tree2').open_workingtree()
        self.build_tree_contents([('tree1/file', 'a\nb\nc\nd\ne\n')])
        self.tree1.commit('bar', rev_id='bar-id')
        os.chdir('tree1')
        return self.tree1, self.tree2

    def test_merge_directive(self):
        self.prepare_merge_directive()
        md_text = self.run_bzr('merge-directive ../tree2')[0]
        self.assertContainsRe(md_text, "\\+e")
        md_text = self.run_bzr('merge-directive -r -2 ../tree2')[0]
        self.assertNotContainsRe(md_text, "\\+e")
        md_text = self.run_bzr('merge-directive -r -1..-2 ../tree2')[0]
        md2 = merge_directive.MergeDirective.from_lines(
            md_text.splitlines(True))
        self.assertEqual('foo-id', md2.revision_id)
        self.assertEqual('bar-id', md2.base_revision_id)

    def test_submit_branch(self):
        self.prepare_merge_directive()
        self.run_bzr_error(('No submit branch',), 'merge-directive', retcode=3)
        self.run_bzr('merge-directive ../tree2')

    def test_public_branch(self):
        self.prepare_merge_directive()
        self.run_bzr_error(('No public branch',),
                           'merge-directive --diff ../tree2', retcode=3)
        md_text = self.run_bzr('merge-directive ../tree2')[0]
        self.assertNotContainsRe(md_text, 'source_branch:')
        self.run_bzr('merge-directive --diff ../tree2 .')
        self.run_bzr('merge-directive --diff')[0]
        self.assertNotContainsRe(md_text, 'source_branch:')

    def test_patch_types(self):
        self.prepare_merge_directive()
        md_text = self.run_bzr('merge-directive ../tree2')[0]
        self.assertContainsRe(md_text, "# Begin bundle")
        self.assertContainsRe(md_text, "\\+e")
        md_text = self.run_bzr('merge-directive ../tree2 --diff .')[0]
        self.assertNotContainsRe(md_text, "# Begin bundle")
        self.assertContainsRe(md_text, "\\+e")
        md_text = self.run_bzr('merge-directive --plain')[0]
        self.assertNotContainsRe(md_text, "\\+e")

    def test_message(self):
        self.prepare_merge_directive()
        md_text = self.run_bzr('merge-directive ../tree2')[0]
        self.assertNotContainsRe(md_text, 'message: Message for merge')
        md_text = self.run_bzr('merge-directive -m Message_for_merge')[0]
        self.assertContainsRe(md_text, 'message: Message_for_merge')

    def test_signing(self):
        self.prepare_merge_directive()
        old_strategy = gpg.GPGStrategy
        gpg.GPGStrategy = gpg.LoopbackGPGStrategy
        try:
            md_text = self.run_bzr('merge-directive --sign ../tree2')[0]
        finally:
            gpg.GPGStrategy = old_strategy
        self.assertContainsRe(md_text, '^-----BEGIN PSEUDO-SIGNED CONTENT')

    def run_bzr_fakemail(self, *args, **kwargs):
        sendmail_calls = []
        def sendmail(self, from_, to, message):
            sendmail_calls.append((self, from_, to, message))
        connect_calls = []
        def connect(self, host='localhost', port=0):
            connect_calls.append((self, host, port))
        def starttls(self):
            pass
        old_sendmail = smtplib.SMTP.sendmail
        smtplib.SMTP.sendmail = sendmail
        old_connect = smtplib.SMTP.connect
        smtplib.SMTP.connect = connect
        old_starttls = smtplib.SMTP.starttls
        smtplib.SMTP.starttls = starttls
        try:
            result = self.run_bzr(*args, **kwargs)
        finally:
            smtplib.SMTP.sendmail = old_sendmail
            smtplib.SMTP.connect = old_connect
            smtplib.SMTP.starttls = old_starttls
        return result + (connect_calls, sendmail_calls)

    def test_mail_default(self):
        tree1, tree2 = self.prepare_merge_directive()
        md_text, errr, connect_calls, sendmail_calls =\
            self.run_bzr_fakemail(['merge-directive', '--mail-to',
                                   'pqm@example.com', '--plain', '../tree2',
                                   '.'])
        self.assertEqual('', md_text)
        self.assertEqual(1, len(connect_calls))
        call = connect_calls[0]
        self.assertEqual(('localhost', 0), call[1:3])
        self.assertEqual(1, len(sendmail_calls))
        call = sendmail_calls[0]
        self.assertEqual(('jrandom@example.com', ['pqm@example.com']),
                call[1:3])
        self.assertContainsRe(call[3], EMAIL1)

    def test_pull_raw(self):
        self.prepare_merge_directive()
        self.tree1.commit('baz', rev_id='baz-id')
        md_text = self.run_bzr(['merge-directive', self.tree2.basedir,
                                '-r', '2', self.tree1.basedir, '--plain'])[0]
        self.build_tree_contents([('../directive', md_text)])
        os.chdir('../tree2')
        self.run_bzr('pull ../directive')
        wt = workingtree.WorkingTree.open('.')
        self.assertEqual('bar-id', wt.last_revision())

    def test_pull_user_r(self):
        """If the user supplies -r, an error is emitted"""
        self.prepare_merge_directive()
        self.tree1.commit('baz', rev_id='baz-id')
        md_text = self.run_bzr(['merge-directive', self.tree2.basedir,
                                self.tree1.basedir, '--plain'])[0]
        self.build_tree_contents([('../directive', md_text)])
        os.chdir('../tree2')
        self.run_bzr_error(
            ('Cannot use -r with merge directives or bundles',),
            'pull -r 2 ../directive')

    def test_pull_bundle(self):
        self.prepare_merge_directive()
        self.tree1.commit('baz', rev_id='baz-id')
        md_text = self.run_bzr(['merge-directive', self.tree2.basedir,
                                '-r', '2', '/dev/null', '--bundle'])[0]
        self.build_tree_contents([('../directive', md_text)])
        os.chdir('../tree2')
        self.run_bzr('pull ../directive')
        wt = workingtree.WorkingTree.open('.')
        self.assertEqual('bar-id', wt.last_revision())

    def test_merge_raw(self):
        self.prepare_merge_directive()
        self.tree1.commit('baz', rev_id='baz-id')
        md_text = self.run_bzr(['merge-directive', self.tree2.basedir,
                                '-r', '2', self.tree1.basedir, '--plain'])[0]
        self.build_tree_contents([('../directive', md_text)])
        os.chdir('../tree2')
        self.run_bzr('merge ../directive')
        wt = workingtree.WorkingTree.open('.')
        self.assertEqual('bar-id', wt.get_parent_ids()[1])

    def test_merge_user_r(self):
        """If the user supplies -r, an error is emitted"""
        self.prepare_merge_directive()
        self.tree1.commit('baz', rev_id='baz-id')
        md_text = self.run_bzr(['merge-directive', self.tree2.basedir,
                                self.tree1.basedir, '--plain'])[0]
        self.build_tree_contents([('../directive', md_text)])
        os.chdir('../tree2')
        self.run_bzr_error(
            ('Cannot use -r with merge directives or bundles',),
            'merge -r 2 ../directive')

    def test_merge_bundle(self):
        self.prepare_merge_directive()
        self.tree1.commit('baz', rev_id='baz-id')
        md_text = self.run_bzr(['merge-directive', self.tree2.basedir,
                               '-r', '2', '/dev/null', '--bundle'])[0]
        self.build_tree_contents([('../directive', md_text)])
        os.chdir('../tree2')
        self.run_bzr('merge ../directive')
        wt = workingtree.WorkingTree.open('.')
        self.assertEqual('bar-id', wt.get_parent_ids()[1])

    def test_mail_uses_config(self):
        tree1, tree2 = self.prepare_merge_directive()
        tree1.branch.get_config().set_user_option('smtp_server', 'bogushost')
        md_text, errr, connect_calls, sendmail_calls =\
            self.run_bzr_fakemail('merge-directive --mail-to'
                                  ' pqm@example.com --plain ../tree2 .')
        call = connect_calls[0]
        self.assertEqual(('bogushost', 0), call[1:3])

    def test_no_common_ancestor(self):
        foo = self.make_branch_and_tree('foo')
        foo.commit('rev1')
        bar = self.make_branch_and_tree('bar')
        os.chdir('foo')
        self.run_bzr('merge-directive ../bar')

    def test_no_commits(self):
        foo = self.make_branch_and_tree('foo')
        bar = self.make_branch_and_tree('bar')
        os.chdir('foo')
        self.run_bzr_error(('No revisions to bundle.', ),
                            'merge-directive ../bar')

    def test_encoding_exact(self):
        tree1, tree2 = self.prepare_merge_directive()
        tree1.commit(u'messag\xe9')
        self.run_bzr('merge-directive ../tree2') # no exception raised