~bzr-pqm/bzr/bzr.dev

1551.12.14 by Aaron Bentley
Get merge-directive command basically working
1
# Copyright (C) 2007 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
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
import os
1551.12.42 by Aaron Bentley
Clean up blackbox tests
18
import smtplib
1551.12.16 by Aaron Bentley
Enable signing merge directives
19
20
from bzrlib import gpg, tests
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
21
1551.12.26 by Aaron Bentley
Get email working, with optional message
22
23
EMAIL1 = """To: pqm@example.com
24
From: J. Random Hacker <jrandom@example.com>
25
Subject: bar
26
1551.12.45 by Aaron Bentley
Change format marker to not experimental
27
# Bazaar merge directive format 1
1551.12.26 by Aaron Bentley
Get email working, with optional message
28
# revision_id: jrandom@example.com-.*
29
# target_branch: ../tree2
30
# testament_sha1: .*
31
# timestamp: .*
1551.12.33 by Aaron Bentley
Take public_branch as a string, not object
32
# source_branch: .
1551.12.26 by Aaron Bentley
Get email working, with optional message
33
#"""
34
35
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
36
class TestMergeDirective(tests.TestCaseWithTransport):
37
1551.12.42 by Aaron Bentley
Clean up blackbox tests
38
    def prepare_merge_directive(self):
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
39
        tree1 = self.make_branch_and_tree('tree1')
40
        self.build_tree_contents([('tree1/file', 'a\nb\nc\nd\n')])
1551.12.26 by Aaron Bentley
Get email working, with optional message
41
        tree1.branch.get_config().set_user_option('email',
42
            'J. Random Hacker <jrandom@example.com>')
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
43
        tree1.add('file')
44
        tree1.commit('foo')
45
        tree2=tree1.bzrdir.sprout('tree2').open_workingtree()
46
        self.build_tree_contents([('tree1/file', 'a\nb\nc\nd\ne\n')])
47
        tree1.commit('bar')
48
        os.chdir('tree1')
1551.12.42 by Aaron Bentley
Clean up blackbox tests
49
50
    def test_merge_directive(self):
51
        self.prepare_merge_directive()
52
        md_text = self.run_bzr('merge-directive', '../tree2')[0]
53
        self.assertContainsRe(md_text, "\\+e")
54
        md_text = self.run_bzr('merge-directive', '-r', '-2', '../tree2')[0]
55
        self.assertNotContainsRe(md_text, "\\+e")
56
57
    def test_submit_branch(self):
58
        self.prepare_merge_directive()
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
59
        self.run_bzr_error(('No submit branch',), 'merge-directive', retcode=3)
60
        self.run_bzr('merge-directive', '../tree2')
1551.12.42 by Aaron Bentley
Clean up blackbox tests
61
62
    def test_public_branch(self):
63
        self.prepare_merge_directive()
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
64
        self.run_bzr_error(('No public branch',), 'merge-directive', '--diff',
1551.12.42 by Aaron Bentley
Clean up blackbox tests
65
                           '../tree2', retcode=3)
66
        md_text = self.run_bzr('merge-directive', '../tree2')[0]
67
        self.assertNotContainsRe(md_text, 'source_branch:')
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
68
        self.run_bzr('merge-directive', '--diff', '../tree2', '.')
1551.12.42 by Aaron Bentley
Clean up blackbox tests
69
        self.run_bzr('merge-directive', '--diff')[0]
70
        self.assertNotContainsRe(md_text, 'source_branch:')
71
72
    def test_patch_types(self):
73
        self.prepare_merge_directive()
74
        md_text = self.run_bzr('merge-directive', '../tree2')[0]
75
        self.assertContainsRe(md_text, "Bazaar revision bundle")
76
        self.assertContainsRe(md_text, "\\+e")
77
        md_text = self.run_bzr('merge-directive', '../tree2', '--diff', '.')[0]
1551.12.14 by Aaron Bentley
Get merge-directive command basically working
78
        self.assertNotContainsRe(md_text, "Bazaar revision bundle")
79
        self.assertContainsRe(md_text, "\\+e")
80
        md_text = self.run_bzr('merge-directive', '--plain')[0]
81
        self.assertNotContainsRe(md_text, "\\+e")
1551.12.42 by Aaron Bentley
Clean up blackbox tests
82
83
    def test_message(self):
84
        self.prepare_merge_directive()
85
        md_text = self.run_bzr('merge-directive', '../tree2')[0]
1551.12.27 by Aaron Bentley
support custom message everywhere
86
        self.assertNotContainsRe(md_text, 'message: Message for merge')
87
        md_text = self.run_bzr('merge-directive', '-m', 'Message for merge')[0]
88
        self.assertContainsRe(md_text, 'message: Message for merge')
1551.12.42 by Aaron Bentley
Clean up blackbox tests
89
90
    def test_signing(self):
91
        self.prepare_merge_directive()
1551.12.16 by Aaron Bentley
Enable signing merge directives
92
        old_strategy = gpg.GPGStrategy
93
        gpg.GPGStrategy = gpg.LoopbackGPGStrategy
94
        try:
1551.12.42 by Aaron Bentley
Clean up blackbox tests
95
            md_text = self.run_bzr('merge-directive', '--sign', '../tree2')[0]
1551.12.16 by Aaron Bentley
Enable signing merge directives
96
        finally:
97
            gpg.GPGStrategy = old_strategy
98
        self.assertContainsRe(md_text, '^-----BEGIN PSEUDO-SIGNED CONTENT')
1551.12.42 by Aaron Bentley
Clean up blackbox tests
99
100
    def test_mail(self):
101
        self.prepare_merge_directive()
1551.12.26 by Aaron Bentley
Get email working, with optional message
102
        sendmail_calls = []
103
        def sendmail(self, from_, to, message):
104
            sendmail_calls.append((self, from_, to, message))
105
        old_sendmail = smtplib.SMTP.sendmail
106
        smtplib.SMTP.sendmail = sendmail
107
        try:
108
            md_text = self.run_bzr('merge-directive', '--mail-to',
1551.12.42 by Aaron Bentley
Clean up blackbox tests
109
                                   'pqm@example.com', '--plain', '../tree2',
110
                                   '.')[0]
1551.12.26 by Aaron Bentley
Get email working, with optional message
111
        finally:
112
            smtplib.SMTP.sendmail = old_sendmail
113
        self.assertEqual('', md_text)
114
        self.assertEqual(1, len(sendmail_calls))
115
        call = sendmail_calls[0]
116
        self.assertEqual(('J. Random Hacker <jrandom@example.com>',
117
                          'pqm@example.com'), call[1:3])
118
        self.assertContainsRe(call[3], EMAIL1)