1
# Copyright (C) 2005-2008, 2010 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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
18
from unittest import TestLoader
24
from ....bzr.bzrdir import BzrDir
25
from ....tests import TestCaseInTempDir
26
from ..emailer import EmailSender
30
return TestLoader().loadTestsFromName(__name__)
33
sample_config = (b"[DEFAULT]\n"
34
b"post_commit_to=demo@example.com\n"
35
b"post_commit_sender=Sample <foo@example.com>\n"
36
b"revision_mail_headers=X-Cheese: to the rescue!\n")
38
unconfigured_config = (b"[DEFAULT]\n"
39
b"email=Robert <foo@example.com>\n")
41
sender_configured_config = (b"[DEFAULT]\n"
42
b"post_commit_sender=Sample <foo@example.com>\n")
44
to_configured_config = (b"[DEFAULT]\n"
45
b"post_commit_to=Sample <foo@example.com>\n")
47
multiple_to_configured_config = (b"[DEFAULT]\n"
48
b"post_commit_sender=Sender <from@example.com>\n"
49
b"post_commit_to=Sample <foo@example.com>, Other <baz@bar.com>\n")
51
customized_mail_config = (b"[DEFAULT]\n"
52
b"post_commit_to=demo@example.com\n"
53
b"post_commit_sender=Sample <foo@example.com>\n"
54
b"post_commit_subject=[commit] $message\n"
55
b"post_commit_body='''$committer has committed "
56
b"revision 1 at $url.\n\n'''\n")
58
push_config = (b"[DEFAULT]\n"
59
b"post_commit_to=demo@example.com\n"
60
b"post_commit_push_pull=True\n")
62
with_url_config = (b"[DEFAULT]\n"
63
b"post_commit_url=http://some.fake/url/\n"
64
b"post_commit_to=demo@example.com\n"
65
b"post_commit_sender=Sample <foo@example.com>\n")
67
# FIXME: this should not use a literal log, rather grab one from breezy.log
68
sample_log = ('------------------------------------------------------------\n'
71
'committer: Sample <john@example.com>\n'
73
'timestamp: Thu 1970-01-01 00:00:01 +0000\n'
80
class TestGetTo(TestCaseInTempDir):
83
sender = self.get_sender()
84
self.assertEqual('At %s\n\n%s' % (sender.url(), sample_log),
87
def test_custom_body(self):
88
sender = self.get_sender(customized_mail_config)
89
self.assertEqual('%s has committed revision 1 at %s.\n\n%s' %
90
(sender.revision.committer, sender.url(), sample_log),
93
def test_command_line(self):
94
sender = self.get_sender()
95
self.assertEqual(['mail', '-s', sender.subject(), '-a',
96
'From: ' + sender.from_address()] + sender.to(),
97
sender._command_line())
100
sender = self.get_sender()
101
self.assertEqual(['demo@example.com'], sender.to())
104
sender = self.get_sender()
105
self.assertEqual('Sample <foo@example.com>', sender.from_address())
107
def test_from_default(self):
108
sender = self.get_sender(unconfigured_config)
109
self.assertEqual('Robert <foo@example.com>', sender.from_address())
111
def test_should_send(self):
112
sender = self.get_sender()
113
self.assertEqual(True, sender.should_send())
115
def test_should_not_send(self):
116
sender = self.get_sender(unconfigured_config)
117
self.assertEqual(False, sender.should_send())
119
def test_should_not_send_sender_configured(self):
120
sender = self.get_sender(sender_configured_config)
121
self.assertEqual(False, sender.should_send())
123
def test_should_not_send_to_configured(self):
124
sender = self.get_sender(to_configured_config)
125
self.assertEqual(True, sender.should_send())
127
def test_send_to_multiple(self):
128
sender = self.get_sender(multiple_to_configured_config)
129
self.assertEqual([u'Sample <foo@example.com>', u'Other <baz@bar.com>'],
131
self.assertEqual([u'Sample <foo@example.com>', u'Other <baz@bar.com>'],
132
sender._command_line()[-2:])
134
def test_url_set(self):
135
sender = self.get_sender(with_url_config)
136
self.assertEqual(sender.url(), 'http://some.fake/url/')
138
def test_public_url_set(self):
139
config = (b"[DEFAULT]\n"
140
b"public_branch=http://the.publication/location/\n")
141
sender = self.get_sender(config)
142
self.assertEqual(sender.url(), 'http://the.publication/location/')
144
def test_url_precedence(self):
145
config = (b"[DEFAULT]\n"
146
b"post_commit_url=http://some.fake/url/\n"
147
b"public_branch=http://the.publication/location/\n")
148
sender = self.get_sender(config)
149
self.assertEqual(sender.url(), 'http://some.fake/url/')
151
def test_url_unset(self):
152
sender = self.get_sender()
153
self.assertEqual(sender.url(), sender.branch.base)
155
def test_subject(self):
156
sender = self.get_sender()
157
self.assertEqual("Rev 1: foo bar baz in %s" %
161
def test_custom_subject(self):
162
sender = self.get_sender(customized_mail_config)
163
self.assertEqual("[commit] %s" %
164
sender.revision.get_summary(),
167
def test_diff_filename(self):
168
sender = self.get_sender()
169
self.assertEqual('patch-1.diff', sender.diff_filename())
171
def test_headers(self):
172
sender = self.get_sender()
173
self.assertEqual({'X-Cheese': 'to the rescue!'},
174
sender.extra_headers())
176
def get_sender(self, text=sample_config):
177
my_config = config.MemoryStack(text)
178
self.branch = BzrDir.create_branch_convenience('.')
179
tree = self.branch.controldir.open_workingtree()
180
tree.commit('foo bar baz\nfuzzy\nwuzzy', rev_id=b'A',
181
allow_pointless=True,
184
committer="Sample <john@example.com>",
186
sender = EmailSender(self.branch, b'A', my_config)
187
# This is usually only done after the EmailSender has locked the branch
188
# and repository during send(), however, for testing, we need to do it
189
# earlier, since send() is not called.
190
sender._setup_revision_and_revno()
194
class TestEmailerWithLocal(tests.TestCaseWithTransport):
195
"""Test that Emailer will use a local branch if supplied."""
197
def test_local_has_revision(self):
198
master_tree = self.make_branch_and_tree('master')
199
self.build_tree(['master/a'])
201
master_tree.commit('a')
203
child_tree = master_tree.controldir.sprout('child').open_workingtree()
204
child_tree.branch.bind(master_tree.branch)
206
self.build_tree(['child/b'])
207
child_tree.add(['b'])
208
revision_id = child_tree.commit('b')
210
sender = EmailSender(master_tree.branch, revision_id,
211
master_tree.branch.get_config(),
212
local_branch=child_tree.branch)
214
# Make sure we are using the 'local_branch' repository, and not the
216
self.assertIs(child_tree.branch.repository, sender.repository)
218
def test_local_missing_revision(self):
219
master_tree = self.make_branch_and_tree('master')
220
self.build_tree(['master/a'])
222
master_tree.commit('a')
224
child_tree = master_tree.controldir.sprout('child').open_workingtree()
225
child_tree.branch.bind(master_tree.branch)
227
self.build_tree(['master/c'])
228
master_tree.add(['c'])
229
revision_id = master_tree.commit('c')
232
child_tree.branch.repository.has_revision(revision_id))
233
sender = EmailSender(master_tree.branch, revision_id,
234
master_tree.branch.get_config(),
235
local_branch=child_tree.branch)
236
# We should be using the master repository here, because the child
237
# repository doesn't contain the revision.
238
self.assertIs(master_tree.branch.repository, sender.repository)