~jteh/bzr-email/pushPullEnhancements

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
# Copyright (C) 2005-2008, 2010 by Canonical Ltd
#   Authors: Robert Collins <robert.collins@canonical.com>
#
# 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

from cStringIO import StringIO
from unittest import TestLoader

from bzrlib import (
    config,
    tests,
    )
from bzrlib.bzrdir import BzrDir
from bzrlib.tests import TestCaseInTempDir
from bzrlib.plugins.email import post_commit
from bzrlib.plugins.email.emailer import EmailSender


def test_suite():
    return TestLoader().loadTestsFromName(__name__)


sample_config=("[DEFAULT]\n"
               "post_commit_to=demo@example.com\n"
               "post_commit_sender=Sample <foo@example.com>\n"
               "revision_mail_headers=X-Cheese: to the rescue!\n")

unconfigured_config=("[DEFAULT]\n"
                     "email=Robert <foo@example.com>\n")

sender_configured_config=("[DEFAULT]\n"
                          "post_commit_sender=Sample <foo@example.com>\n")

to_configured_config=("[DEFAULT]\n"
                      "post_commit_to=Sample <foo@example.com>\n")

multiple_to_configured_config=("[DEFAULT]\n"
              "post_commit_sender=Sender <from@example.com>\n"
              "post_commit_to=Sample <foo@example.com>, Other <baz@bar.com>\n")

push_config=("[DEFAULT]\n"
    "post_commit_to=demo@example.com\n"
    "post_commit_push_pull=True\n")

with_url_config=("[DEFAULT]\n"
                 "post_commit_url=http://some.fake/url/\n"
                 "post_commit_to=demo@example.com\n"
                 "post_commit_sender=Sample <foo@example.com>\n")


class TestGetTo(TestCaseInTempDir):

    def test_body(self):
        sender = self.get_sender()
        # FIXME: this should not use a literal log, rather grab one from bzrlib.log
        self.assertEqual(
            'At %s\n'
            '\n'
            '------------------------------------------------------------\n'
            'revno: 1\n'
            'revision-id: A\n'
            'committer: Sample <john@example.com>\n'
            'branch nick: work\n'
            'timestamp: Thu 1970-01-01 00:00:01 +0000\n'
            'message:\n'
            '  foo bar baz\n'
            '  fuzzy\n'
            '  wuzzy\n' % sender.url(), sender.body())

    def test_command_line(self):
        sender = self.get_sender()
        self.assertEqual(['mail', '-s', sender.subject(), '-a',
                          'From: ' + sender.from_address(), sender.to()],
                         sender._command_line())

    def test_to(self):
        sender = self.get_sender()
        self.assertEqual('demo@example.com', sender.to())

    def test_from(self):
        sender = self.get_sender()
        self.assertEqual('Sample <foo@example.com>', sender.from_address())

    def test_from_default(self):
        sender = self.get_sender(unconfigured_config)
        self.assertEqual('Robert <foo@example.com>', sender.from_address())

    def test_should_send(self):
        sender = self.get_sender()
        self.assertEqual(True, sender.should_send())

    def test_should_not_send(self):
        sender = self.get_sender(unconfigured_config)
        self.assertEqual(False, sender.should_send())

    def test_should_not_send_sender_configured(self):
        sender = self.get_sender(sender_configured_config)
        self.assertEqual(False, sender.should_send())

    def test_should_not_send_to_configured(self):
        sender = self.get_sender(to_configured_config)
        self.assertEqual(True, sender.should_send())

    def test_send_to_multiple(self):
        sender = self.get_sender(multiple_to_configured_config)
        self.assertEqual([u'Sample <foo@example.com>', u'Other <baz@bar.com>'],
                         sender.to())
        self.assertEqual([u'Sample <foo@example.com>', u'Other <baz@bar.com>'],
                         sender._command_line()[-2:])

    def test_url_set(self):
        sender = self.get_sender(with_url_config)
        self.assertEqual(sender.url(), 'http://some.fake/url/')

    def test_public_url_set(self):
        config=("[DEFAULT]\n"
                "public_branch=http://the.publication/location/\n")
        sender = self.get_sender(config)
        self.assertEqual(sender.url(), 'http://the.publication/location/')

    def test_url_precedence(self):
        config=("[DEFAULT]\n"
                "post_commit_url=http://some.fake/url/\n"
                "public_branch=http://the.publication/location/\n")
        sender = self.get_sender(config)
        self.assertEqual(sender.url(), 'http://some.fake/url/')

    def test_url_unset(self):
        sender = self.get_sender()
        self.assertEqual(sender.url(), sender.branch.base)

    def test_subject(self):
        sender = self.get_sender()
        self.assertEqual("Rev 1: foo bar baz in %s" %
                            sender.branch.base,
                         sender.subject())

    def test_diff_filename(self):
        sender = self.get_sender()
        self.assertEqual('patch-1.diff', sender.diff_filename())

    def test_headers(self):
        sender = self.get_sender()
        self.assertEqual({'X-Cheese': 'to the rescue!'}, sender.extra_headers())

    def get_sender(self, text=sample_config):
        config.GlobalConfig.from_string(text, save=True)
        self.branch = BzrDir.create_branch_convenience('.')
        tree = self.branch.bzrdir.open_workingtree()
        tree.commit('foo bar baz\nfuzzy\nwuzzy', rev_id='A',
            allow_pointless=True,
            timestamp=1,
            timezone=0,
            committer="Sample <john@example.com>",
            )
        my_config = self.branch.get_config()
        sender = EmailSender(self.branch, 'A', my_config)
        # This is usually only done after the EmailSender has locked the branch
        # and repository during send(), however, for testing, we need to do it
        # earlier, since send() is not called.
        sender._setup_revision_and_revno()
        return sender


class TestEmailerWithLocal(tests.TestCaseWithTransport):
    """Test that Emailer will use a local branch if supplied."""

    def test_local_has_revision(self):
        master_tree = self.make_branch_and_tree('master')
        self.build_tree(['master/a'])
        master_tree.add('a')
        master_tree.commit('a')

        child_tree = master_tree.bzrdir.sprout('child').open_workingtree()
        child_tree.branch.bind(master_tree.branch)

        self.build_tree(['child/b'])
        child_tree.add(['b'])
        revision_id = child_tree.commit('b')

        sender = EmailSender(master_tree.branch, revision_id,
                             master_tree.branch.get_config(),
                             local_branch=child_tree.branch)

        # Make sure we are using the 'local_branch' repository, and not the
        # remote one.
        self.assertIs(child_tree.branch.repository, sender.repository)

    def test_local_missing_revision(self):
        master_tree = self.make_branch_and_tree('master')
        self.build_tree(['master/a'])
        master_tree.add('a')
        master_tree.commit('a')

        child_tree = master_tree.bzrdir.sprout('child').open_workingtree()
        child_tree.branch.bind(master_tree.branch)

        self.build_tree(['master/c'])
        master_tree.add(['c'])
        revision_id = master_tree.commit('c')

        self.failIf(child_tree.branch.repository.has_revision(revision_id))
        sender = EmailSender(master_tree.branch, revision_id,
                             master_tree.branch.get_config(),
                             local_branch=child_tree.branch)
        # We should be using the master repository here, because the child
        # repository doesn't contain the revision.
        self.assertIs(master_tree.branch.repository, sender.repository)