~ursinha/lp-qa-tools/bzr-tarmacland

« back to all changes in this revision

Viewing changes to tests/test_pqm_submit.py

  • Committer: Ursula Junque (Ursinha)
  • Date: 2010-12-16 03:14:26 UTC
  • Revision ID: ursinha@canonical.com-20101216031426-bbk2xmnt9vs8hvqz
Removing unnecessary files for the tarmac-land command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 by Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License along
14
 
# with this program; if not, write to the Free Software Foundation, Inc.,
15
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
 
"""Test cases for pqm submit."""
17
 
 
18
 
import smtplib
19
 
 
20
 
from bzrlib import (
21
 
    config as _mod_config,
22
 
    errors,
23
 
    gpg,
24
 
    )
25
 
from bzrlib.plugins.pqm import pqm_submit
26
 
from bzrlib.tests import TestCaseWithMemoryTransport, TestCaseWithTransport
27
 
 
28
 
 
29
 
EMAIL = """\
30
 
From: "J. Random Hacker" <jrandom@example.com>
31
 
Subject: commit message
32
 
To: PQM <pqm@example.com>
33
 
User-Agent: Bazaar \(.*\)
34
 
 
35
 
-----BEGIN PSEUDO-SIGNED CONTENT-----
36
 
star-merge .*/public/ .*/submit/
37
 
-----END PSEUDO-SIGNED CONTENT-----
38
 
"""
39
 
 
40
 
 
41
 
class PQMSubmissionTests(TestCaseWithMemoryTransport):
42
 
 
43
 
    def test_no_source_branch(self):
44
 
        self.assertRaises(
45
 
            errors.NoMergeSource, pqm_submit.PQMSubmission,
46
 
            source_branch=None)
47
 
 
48
 
    def test_newlines_in_message(self):
49
 
        source_branch = self.make_branch('source')
50
 
        self.assertRaises(pqm_submit.BadCommitMessage,
51
 
                          pqm_submit.PQMSubmission,
52
 
                          source_branch=source_branch,
53
 
                          public_location='public-branch',
54
 
                          submit_location='submit-branch',
55
 
                          message='foo\nbar')
56
 
 
57
 
    def test_check_tree_clean(self):
58
 
        tree = self.make_branch_and_memory_tree('source')
59
 
        submission = pqm_submit.PQMSubmission(
60
 
            source_branch=tree.branch,
61
 
            public_location=tree.branch.base,
62
 
            submit_location='submit-branch',
63
 
            message='not much to say',
64
 
            tree=tree,
65
 
            )
66
 
        submission.check_tree()
67
 
 
68
 
    def test_check_tree_no_tree(self):
69
 
        branch = self.make_branch('source')
70
 
        submission = pqm_submit.PQMSubmission(
71
 
            source_branch=branch,
72
 
            public_location=branch.base,
73
 
            submit_location='submit-branch',
74
 
            message='not much to say',
75
 
            tree=None,
76
 
            )
77
 
        submission.check_tree()
78
 
 
79
 
    def test_check_tree_dirty(self):
80
 
        tree = self.make_branch_and_memory_tree('source')
81
 
        tree.lock_write()
82
 
        try:
83
 
            tree.add('')
84
 
 
85
 
            submission = pqm_submit.PQMSubmission(
86
 
                source_branch=tree.branch,
87
 
                public_location=tree.branch.base,
88
 
                submit_location='submit-branch',
89
 
                message='not much to say',
90
 
                tree=tree,
91
 
                )
92
 
            self.assertRaises(errors.UncommittedChanges, submission.check_tree)
93
 
        finally:
94
 
            tree.unlock()
95
 
 
96
 
    def test_check_public_branch(self):
97
 
        tree = self.make_branch_and_memory_tree('source')
98
 
        source_branch = tree.branch
99
 
        public_branch = self.make_branch('public')
100
 
 
101
 
        # Commit something to the source branch
102
 
        tree.lock_write()
103
 
        tree.add('')
104
 
        tree.commit('message')
105
 
        tree.unlock()
106
 
 
107
 
        # Now the public branch is out of date:
108
 
        submission = pqm_submit.PQMSubmission(
109
 
            source_branch=source_branch,
110
 
            public_location=public_branch.base,
111
 
            submit_location='submit-branch',
112
 
            message='merge message')
113
 
        self.assertRaises(errors.PublicBranchOutOfDate,
114
 
                          submission.check_public_branch)
115
 
 
116
 
        # If we bring the public branch up to date, everything is fine.
117
 
        public_branch.pull(source_branch)
118
 
        submission.check_public_branch()
119
 
 
120
 
    def test_check_public_branch_missing(self):
121
 
        source_branch = self.make_branch('source')
122
 
        submission = pqm_submit.PQMSubmission(
123
 
            source_branch=source_branch,
124
 
            public_location=self.get_transport().abspath('public'),
125
 
            submit_location='submit-branch',
126
 
            message='merge message')
127
 
        self.assertRaises(errors.NotBranchError,
128
 
                          submission.check_public_branch)
129
 
 
130
 
    def test_ignore_local(self):
131
 
        submission = pqm_submit.PQMSubmission(
132
 
            source_branch=None,
133
 
            public_location='public-location',
134
 
            submit_location='submit-branch',
135
 
            message='merge message')
136
 
        message = submission.to_email('from@address', 'to@address', sign=False)
137
 
        self.assertContainsRe(
138
 
            message.as_string(), 'star-merge public-location submit-branch')
139
 
 
140
 
    def test_to_lines(self):
141
 
        source_branch = self.make_branch('source')
142
 
        submission = pqm_submit.PQMSubmission(
143
 
            source_branch=source_branch,
144
 
            public_location='public-branch',
145
 
            submit_location='submit-branch',
146
 
            message='commit message')
147
 
        lines = submission.to_lines()
148
 
        self.assertEqual(['star-merge public-branch submit-branch\n'], lines)
149
 
 
150
 
    def test_to_signed(self):
151
 
        source_branch = self.make_branch('source')
152
 
        submission = pqm_submit.PQMSubmission(
153
 
            source_branch=source_branch,
154
 
            public_location='public-branch',
155
 
            submit_location='submit-branch',
156
 
            message='commit message')
157
 
        old_strategy = gpg.GPGStrategy
158
 
        gpg.GPGStrategy = gpg.LoopbackGPGStrategy
159
 
        try:
160
 
            signed = submission.to_signed()
161
 
        finally:
162
 
            gpg.GPGStrategy = old_strategy
163
 
        self.assertEqual('-----BEGIN PSEUDO-SIGNED CONTENT-----\n'
164
 
                         'star-merge public-branch submit-branch\n'
165
 
                         '-----END PSEUDO-SIGNED CONTENT-----\n', signed)
166
 
 
167
 
    def test_to_email(self):
168
 
        source_branch = self.make_branch('source')
169
 
        submission = pqm_submit.PQMSubmission(
170
 
            source_branch=source_branch,
171
 
            public_location='public-branch',
172
 
            submit_location='submit-branch',
173
 
            message='commit message')
174
 
        message = submission.to_email('from@example.com', 'to@example.com',
175
 
                                      sign=False)
176
 
        self.assertEqual('from@example.com', message.get('From'))
177
 
        self.assertEqual('to@example.com', message.get('To'))
178
 
        self.assertEqual('commit message', message.get('Subject'))
179
 
 
180
 
    def test_to_unicode_email(self):
181
 
        """Subject has to be raw UTF-8 not email encoded."""
182
 
        source_branch = self.make_branch('source')
183
 
        submission = pqm_submit.PQMSubmission(
184
 
            source_branch=source_branch,
185
 
            public_location='public-branch',
186
 
            submit_location='submit-branch',
187
 
            message=u'Commit m\xe5ss\xb5ge')
188
 
        message = submission.to_email('from@example.com', 'to@example.com',
189
 
                                      sign=False)
190
 
        self.assertEqual('from@example.com', message.get('From'))
191
 
        self.assertEqual('to@example.com', message.get('To'))
192
 
        self.assertEqual('Commit m\xc3\xa5ss\xc2\xb5ge',
193
 
                         message.get('Subject'))
194
 
 
195
 
    def test_submit_branch_public_location(self):
196
 
        source_branch = self.make_branch('source')
197
 
        public_branch = self.make_branch('public')
198
 
        submit_branch = self.make_branch('submit')
199
 
        submit_public_branch = self.make_branch('submit_public')
200
 
        submit_branch.set_public_branch(submit_public_branch.base)
201
 
        source_branch.set_submit_branch(submit_branch.base)
202
 
        submission = pqm_submit.PQMSubmission(
203
 
            source_branch=source_branch,
204
 
            public_location='public',
205
 
            message=u'Commit m\xe5ss\xb5ge')
206
 
        self.assertEqual(submit_public_branch.base, submission.submit_location)
207
 
 
208
 
 
209
 
class PQMSubmissionLocationsTests(TestCaseWithTransport):
210
 
 
211
 
    def test_find_public_branch(self):
212
 
        source_branch = self.make_branch('source')
213
 
        source_branch.set_public_branch('http://example.com/public')
214
 
        # Also set the deprecated public_repository config item to
215
 
        # show that public_branch is used in preference to it.
216
 
        source_branch.get_config().set_user_option(
217
 
            'public_repository', 'bad-value')
218
 
 
219
 
        submission = pqm_submit.PQMSubmission(
220
 
            source_branch=source_branch,
221
 
            submit_location='submit-branch',
222
 
            message='commit message')
223
 
        self.assertEqual('http://example.com/public',
224
 
                         submission.public_location)
225
 
 
226
 
    def test_find_public_branch_from_repo(self):
227
 
        # Test that public_location can be inferred from the obsolete
228
 
        # "public_repository" config option.
229
 
        source_repo = self.make_repository('repo', shared=True)
230
 
        source_branch = self.make_bzrdir('repo/source').create_branch()
231
 
 
232
 
        config = _mod_config.LocationConfig(
233
 
            source_repo.bzrdir.root_transport.base)
234
 
        config.set_user_option(
235
 
            'public_repository', 'http://example.com/repo',
236
 
            store=_mod_config.STORE_LOCATION_NORECURSE)
237
 
 
238
 
        submission = pqm_submit.PQMSubmission(
239
 
            source_branch=source_branch,
240
 
            submit_location='submit-branch',
241
 
            message='commit message')
242
 
        self.assertEqual('http://example.com/repo/source',
243
 
                         submission.public_location)
244
 
 
245
 
    def test_find_public_branch_missing(self):
246
 
        source_branch = self.make_branch('source')
247
 
        self.assertRaises(
248
 
            errors.NoPublicBranch, pqm_submit.PQMSubmission,
249
 
            source_branch=source_branch,
250
 
            submit_location='submit-branch',
251
 
            message='commit message')
252
 
 
253
 
    def test_find_submit_branch(self):
254
 
        source_branch = self.make_branch('source')
255
 
        submit_transport = self.get_transport().clone('submit')
256
 
        submit_transport.ensure_base()
257
 
        source_branch.set_submit_branch(submit_transport.base)
258
 
 
259
 
        submission = pqm_submit.PQMSubmission(
260
 
            source_branch=source_branch,
261
 
            public_location='public-branch',
262
 
            message='commit message')
263
 
        self.assertEqual(submit_transport.base, submission.submit_location)
264
 
 
265
 
    def test_find_submit_branch_from_pqm_branch(self):
266
 
        # Test that submit_branch can be picked up from the obsolete
267
 
        # pqm_branch config option.
268
 
        source_branch = self.make_branch('source')
269
 
        submit_transport = self.get_transport().clone('submit')
270
 
        submit_transport.ensure_base()
271
 
        source_branch.get_config().set_user_option(
272
 
            'pqm_branch', submit_transport.base)
273
 
        # pqm_branch is used in preference to the submit_branch:
274
 
        source_branch.set_submit_branch('bad-value')
275
 
 
276
 
        submission = pqm_submit.PQMSubmission(
277
 
            source_branch=source_branch,
278
 
            public_location='public-branch',
279
 
            message='commit message')
280
 
        self.assertEqual(submit_transport.base, submission.submit_location)
281
 
 
282
 
    def test_find_submit_branch_missing(self):
283
 
        source_branch = self.make_branch('source')
284
 
        self.assertRaises(
285
 
            errors.NoSubmitBranch, pqm_submit.PQMSubmission,
286
 
            source_branch=source_branch,
287
 
            public_location='public-branch',
288
 
            message='commit message')
289
 
 
290
 
    def run_bzr_fakemail(self, *args, **kwargs):
291
 
        # Run with fake smtplib and gpg stubs in place:
292
 
        sendmail_calls = []
293
 
        def sendmail(self, from_, to, message):
294
 
            sendmail_calls.append((self, from_, to, message))
295
 
        connect_calls = []
296
 
        def connect(self, host='localhost', port=0):
297
 
            connect_calls.append((self, host, port))
298
 
        def ehlo(self):
299
 
            return (200, 'Ok')
300
 
        def has_extn(self, extn):
301
 
            return False
302
 
        def starttls(self):
303
 
            pass
304
 
        old_sendmail = smtplib.SMTP.sendmail
305
 
        smtplib.SMTP.sendmail = sendmail
306
 
        old_connect = smtplib.SMTP.connect
307
 
        smtplib.SMTP.connect = connect
308
 
        old_ehlo = smtplib.SMTP.ehlo
309
 
        smtplib.SMTP.ehlo = ehlo
310
 
        old_has_extn = smtplib.SMTP.has_extn
311
 
        smtplib.SMTP.has_extn = has_extn
312
 
        old_starttls = smtplib.SMTP.starttls
313
 
        smtplib.SMTP.starttls = starttls
314
 
        old_strategy = gpg.GPGStrategy
315
 
        gpg.GPGStrategy = gpg.LoopbackGPGStrategy
316
 
        try:
317
 
            result = self.run_bzr(*args, **kwargs)
318
 
        finally:
319
 
            smtplib.SMTP.sendmail = old_sendmail
320
 
            smtplib.SMTP.connect = old_connect
321
 
            smtplib.SMTP.ehlo = old_ehlo
322
 
            smtplib.SMTP.has_extn = old_has_extn
323
 
            smtplib.SMTP.starttls = old_starttls
324
 
            gpg.GPGStrategy = old_strategy
325
 
 
326
 
        return result + (connect_calls, sendmail_calls)
327
 
 
328
 
    def test_pqm_submit(self):
329
 
        source_branch = self.make_branch('source')
330
 
        public_branch = self.make_branch('public')
331
 
        source_branch.set_public_branch(public_branch.base)
332
 
        submit_transport = self.get_transport().clone('submit')
333
 
        submit_transport.ensure_base()
334
 
        source_branch.set_submit_branch(submit_transport.base)
335
 
        config = source_branch.get_config()
336
 
        config.set_user_option('pqm_email', 'PQM <pqm@example.com>')
337
 
        config.set_user_option(
338
 
            'email', 'J. Random Hacker <jrandom@example.com>')
339
 
 
340
 
        out, err, connect_calls, sendmail_calls = \
341
 
            self.run_bzr_fakemail(['pqm-submit', '-m', 'commit message',
342
 
                                   './source'])
343
 
        self.assertEqual('', out)
344
 
        self.assertEqual(1, len(connect_calls))
345
 
        call = connect_calls[0]
346
 
        self.assertEqual(('localhost', 0), call[1:3])
347
 
        self.assertEqual(1, len(sendmail_calls))
348
 
        call = sendmail_calls[0]
349
 
        self.assertEqual(('jrandom@example.com', ['pqm@example.com']),
350
 
                         call[1:3])
351
 
        self.assertContainsRe(call[3], EMAIL)
352
 
 
353
 
    def test_pqm_submit_no_message(self):
354
 
        tree = self.make_branch_and_tree('source')
355
 
        tree.commit("one")
356
 
        out, err, connect_calls, sendmail_calls = \
357
 
            self.run_bzr_fakemail(['pqm-submit', './source'],
358
 
                                  retcode=3)
359
 
        self.assertContainsRe(err, 'You must supply a commit message')
360
 
 
361
 
    def test_dirty_pqm_submit(self):
362
 
        source_tree = self.make_branch_and_tree('source')
363
 
        self.build_tree(['source/foo'])
364
 
        source_tree.add(['foo'])
365
 
 
366
 
        public_branch = self.make_branch('public')
367
 
        source_tree.branch.set_public_branch(public_branch.base)
368
 
        submit_transport = self.get_transport().clone('submit')
369
 
        submit_transport.ensure_base()
370
 
        source_tree.branch.set_submit_branch(submit_transport.base)
371
 
        config = source_tree.branch.get_config()
372
 
        config.set_user_option('pqm_email', 'PQM <pqm@example.com>')
373
 
        config.set_user_option(
374
 
            'email', 'J. Random Hacker <jrandom@example.com>')
375
 
 
376
 
        out, err, connect_calls, sendmail_calls = \
377
 
            self.run_bzr_fakemail(['pqm-submit', '-m', 'commit message',
378
 
                                   './source'],
379
 
                                  retcode=3)
380
 
        self.assertContainsRe(err,
381
 
            r'Working tree ".*/source/" has uncommitted changes')
382
 
 
383
 
    def test_submit_subdir_of_branch(self):
384
 
        source_branch = self.make_branch('source')
385
 
        source_branch.set_submit_branch('http://example.com/submit')
386
 
        config = source_branch.get_config()
387
 
        out, err, connect_calls, sendmail_calls = \
388
 
            self.run_bzr_fakemail(['pqm-submit', '-m', 'commit message',
389
 
                                   './source/subdir'],
390
 
                                  retcode=3)
391
 
        self.assertContainsRe(err, 'bzr: ERROR: No working tree was found')
392
 
 
393
 
    def test_submit_branch(self):
394
 
        """Test that --submit-branch overrides local config."""
395
 
        source_branch = self.make_branch('source')
396
 
        public_branch = self.make_branch('public')
397
 
        source_branch.set_public_branch(public_branch.base)
398
 
        source_branch.set_submit_branch('http://a/very/different/branch')
399
 
        config = source_branch.get_config()
400
 
        config.set_user_option('pqm_email', 'PQM <pqm@example.com>')
401
 
        config.set_user_option(
402
 
            'email', 'J. Random Hacker <jrandom@example.com>')
403
 
 
404
 
        (out, err, connect_calls,
405
 
         sendmail_calls) = self.run_bzr_fakemail(
406
 
                ['pqm-submit', '-m', 'commit message',
407
 
                 '--submit-branch', 'http://example.com/submit/',
408
 
                 './source'
409
 
                ])
410
 
        self.assertEqual('', out)
411
 
        self.assertEqual(1, len(connect_calls))
412
 
        call = connect_calls[0]
413
 
        self.assertEqual(('localhost', 0), call[1:3])
414
 
        self.assertEqual(1, len(sendmail_calls))
415
 
        call = sendmail_calls[0]
416
 
        self.assertEqual(('jrandom@example.com', ['pqm@example.com']),
417
 
                         call[1:3])
418
 
        self.assertContainsRe(call[3], EMAIL)
419
 
 
420
 
    def test_ignore_local(self):
421
 
        """--ignore-local can submit a branch that isn't available locally.
422
 
        
423
 
        It will use the location config of the public location to determine the
424
 
        from and to email addresses.
425
 
        """
426
 
        config = _mod_config.LocationConfig('http://example.com/')
427
 
        config.set_user_option('pqm_email', 'PQM <pqm@example.com>')
428
 
        config.set_user_option(
429
 
            'pqm_user_email', 'J. Random Hacker <jrandom@example.com>')
430
 
        (out, err, connect_calls,
431
 
         sendmail_calls) = self.run_bzr_fakemail(
432
 
                ['pqm-submit', '-m', 'commit message',
433
 
                 '--submit-branch', 'http://example.com/submit/',
434
 
                 '--ignore-local', '--public-location',
435
 
                 'http://example.com/public/'
436
 
                ])
437
 
        self.assertEqual('', out)
438
 
        self.assertEqual(1, len(connect_calls))
439
 
        call = connect_calls[0]
440
 
        self.assertEqual(('localhost', 0), call[1:3])
441
 
        self.assertEqual(1, len(sendmail_calls))
442
 
        call = sendmail_calls[0]
443
 
        self.assertEqual(('jrandom@example.com', ['pqm@example.com']),
444
 
                         call[1:3])
445
 
        self.assertContainsRe(call[3], EMAIL)
446
 
 
447
 
    def test_ignore_local_global_config_fallback(self):
448
 
        """--ignore-local can submit a branch that isn't available locally.
449
 
        
450
 
        If there's no location config for the public location, it will
451
 
        determine the from and to email addresses from the global config.
452
 
        """
453
 
        config = _mod_config.GlobalConfig()
454
 
        config.set_user_option('pqm_email', 'PQM <pqm@example.com>')
455
 
        config.set_user_option(
456
 
            'pqm_user_email', 'J. Random Hacker <jrandom@example.com>')
457
 
        (out, err, connect_calls,
458
 
         sendmail_calls) = self.run_bzr_fakemail(
459
 
                ['pqm-submit', '-m', 'commit message',
460
 
                 '--submit-branch', 'http://example.com/submit/',
461
 
                 '--ignore-local', '--public-location',
462
 
                 'http://example.com/public/'
463
 
                ])
464
 
        self.assertEqual('', out)
465
 
        self.assertEqual(1, len(connect_calls))
466
 
        call = connect_calls[0]
467
 
        self.assertEqual(('localhost', 0), call[1:3])
468
 
        self.assertEqual(1, len(sendmail_calls))
469
 
        call = sendmail_calls[0]
470
 
        self.assertEqual(('jrandom@example.com', ['pqm@example.com']),
471
 
                         call[1:3])
472
 
        self.assertContainsRe(call[3], EMAIL)
473