/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.171.1 by Robert Collins
Start working toward publish - get a publishing_root option
1
# Copyright (C) 2005 by Canonical Ltd
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
#
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.
8
#
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.
13
#
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
17
18
from cStringIO import StringIO
19
from unittest import TestLoader
20
0.175.5 by John Arbash Meinel
Update the email process to give email addresses that work better with SMTP servers.
21
from bzrlib import (
22
    config,
0.171.24 by John Arbash Meinel
Switch to using a local repository if available,
23
    tests,
0.175.5 by John Arbash Meinel
Update the email process to give email addresses that work better with SMTP servers.
24
    )
0.171.11 by Robert Collins
Update tests to use non deprecated API's.
25
from bzrlib.bzrdir import BzrDir
0.175.7 by John Arbash Meinel
split out SMTPConnection to its own file.
26
from bzrlib.tests import TestCaseInTempDir
0.175.3 by John Arbash Meinel
Move guts into another file to improve startup time, fix bug when old revid is None.
27
from bzrlib.plugins.email import post_commit
0.175.7 by John Arbash Meinel
split out SMTPConnection to its own file.
28
from bzrlib.plugins.email.emailer import EmailSender
0.171.1 by Robert Collins
Start working toward publish - get a publishing_root option
29
30
31
def test_suite():
32
    return TestLoader().loadTestsFromName(__name__)
33
34
35
sample_config=("[DEFAULT]\n"
0.171.3 by Robert Collins
convert the switch basics to an email plugin
36
               "post_commit_to=demo@example.com\n"
37
               "post_commit_sender=Sample <foo@example.com>\n")
38
0.171.5 by Robert Collins
Working plugin!
39
unconfigured_config=("[DEFAULT]\n"
40
                     "email=Robert <foo@example.com>\n")
0.171.3 by Robert Collins
convert the switch basics to an email plugin
41
0.173.4 by Matthieu Moy
More tests
42
sender_configured_config=("[DEFAULT]\n"
43
                          "post_commit_sender=Sample <foo@example.com>\n")
44
45
to_configured_config=("[DEFAULT]\n"
46
                      "post_commit_to=Sample <foo@example.com>\n")
47
0.175.7 by John Arbash Meinel
split out SMTPConnection to its own file.
48
multiple_to_configured_config=("[DEFAULT]\n"
49
              "post_commit_sender=Sender <from@example.com>\n"
50
              "post_commit_to=Sample <foo@example.com>, Other <baz@bar.com>\n")
51
0.171.39 by Robert Collins
Draft support for mailing on push/pull.
52
push_config=("[DEFAULT]\n"
53
    "post_commit_to=demo@example.com\n"
54
    "post_commit_push_pull=True\n")
55
0.173.4 by Matthieu Moy
More tests
56
with_url_config=("[DEFAULT]\n"
57
                 "post_commit_url=http://some.fake/url/\n"
58
                 "post_commit_to=demo@example.com\n"
59
                 "post_commit_sender=Sample <foo@example.com>\n")
60
0.171.24 by John Arbash Meinel
Switch to using a local repository if available,
61
0.171.3 by Robert Collins
convert the switch basics to an email plugin
62
class TestGetTo(TestCaseInTempDir):
63
0.171.4 by Robert Collins
basics are all there
64
    def test_body(self):
65
        sender = self.get_sender()
66
        # FIXME: this should not use a literal log, rather grab one from bzrlib.log
67
        self.assertEqual(
0.175.13 by John Arbash Meinel
Add an entry in the body to make it easier to find the url
68
            'At %s\n'
69
            '\n'
0.171.4 by Robert Collins
basics are all there
70
            '------------------------------------------------------------\n'
71
            'revno: 1\n'
72
            'revision-id: A\n'
73
            'committer: Sample <john@example.com>\n'
0.171.13 by Robert Collins
0.11 compatability.
74
            'branch nick: work\n'
0.171.4 by Robert Collins
basics are all there
75
            'timestamp: Thu 1970-01-01 00:00:01 +0000\n'
76
            'message:\n'
77
            '  foo bar baz\n'
78
            '  fuzzy\n'
0.175.13 by John Arbash Meinel
Add an entry in the body to make it easier to find the url
79
            '  wuzzy\n' % sender.url(), sender.body())
0.171.4 by Robert Collins
basics are all there
80
0.171.5 by Robert Collins
Working plugin!
81
    def test_command_line(self):
82
        sender = self.get_sender()
0.175.12 by John Arbash Meinel
Remove trailing whitespace.
83
        self.assertEqual(['mail', '-s', sender.subject(), '-a',
0.171.6 by Robert Collins
-a From: from_address, not -a from_address
84
                          'From: ' + sender.from_address(), sender.to()],
0.171.5 by Robert Collins
Working plugin!
85
                         sender._command_line())
86
0.171.3 by Robert Collins
convert the switch basics to an email plugin
87
    def test_to(self):
88
        sender = self.get_sender()
89
        self.assertEqual('demo@example.com', sender.to())
90
91
    def test_from(self):
92
        sender = self.get_sender()
93
        self.assertEqual('Sample <foo@example.com>', sender.from_address())
94
0.171.5 by Robert Collins
Working plugin!
95
    def test_from_default(self):
96
        sender = self.get_sender(unconfigured_config)
97
        self.assertEqual('Robert <foo@example.com>', sender.from_address())
98
0.171.3 by Robert Collins
convert the switch basics to an email plugin
99
    def test_should_send(self):
100
        sender = self.get_sender()
0.177.2 by Vincent Ladeuil
Fixed as per Robert's review.
101
        self.assertEqual(True, sender.should_send())
0.171.3 by Robert Collins
convert the switch basics to an email plugin
102
103
    def test_should_not_send(self):
0.171.5 by Robert Collins
Working plugin!
104
        sender = self.get_sender(unconfigured_config)
0.177.2 by Vincent Ladeuil
Fixed as per Robert's review.
105
        self.assertEqual(False, sender.should_send())
0.171.3 by Robert Collins
convert the switch basics to an email plugin
106
0.173.4 by Matthieu Moy
More tests
107
    def test_should_not_send_sender_configured(self):
108
        sender = self.get_sender(sender_configured_config)
0.177.2 by Vincent Ladeuil
Fixed as per Robert's review.
109
        self.assertEqual(False, sender.should_send())
0.173.4 by Matthieu Moy
More tests
110
111
    def test_should_not_send_to_configured(self):
112
        sender = self.get_sender(to_configured_config)
0.177.2 by Vincent Ladeuil
Fixed as per Robert's review.
113
        self.assertEqual(True, sender.should_send())
0.173.4 by Matthieu Moy
More tests
114
0.175.7 by John Arbash Meinel
split out SMTPConnection to its own file.
115
    def test_send_to_multiple(self):
116
        sender = self.get_sender(multiple_to_configured_config)
117
        self.assertEqual([u'Sample <foo@example.com>', u'Other <baz@bar.com>'],
118
                         sender.to())
119
        self.assertEqual([u'Sample <foo@example.com>', u'Other <baz@bar.com>'],
120
                         sender._command_line()[-2:])
121
0.173.4 by Matthieu Moy
More tests
122
    def test_url_set(self):
123
        sender = self.get_sender(with_url_config)
124
        self.assertEqual(sender.url(), 'http://some.fake/url/')
125
0.171.17 by Jelmer Vernooij
Look at 'public_branch' as well for URL to report.
126
    def test_public_url_set(self):
127
        config=("[DEFAULT]\n"
128
                "public_branch=http://the.publication/location/\n")
129
        sender = self.get_sender(config)
130
        self.assertEqual(sender.url(), 'http://the.publication/location/')
131
132
    def test_url_precedence(self):
133
        config=("[DEFAULT]\n"
134
                "post_commit_url=http://some.fake/url/\n"
135
                "public_branch=http://the.publication/location/\n")
136
        sender = self.get_sender(config)
137
        self.assertEqual(sender.url(), 'http://some.fake/url/')
138
0.173.4 by Matthieu Moy
More tests
139
    def test_url_unset(self):
140
        sender = self.get_sender()
141
        self.assertEqual(sender.url(), sender.branch.base)
142
0.171.4 by Robert Collins
basics are all there
143
    def test_subject(self):
144
        sender = self.get_sender()
0.175.12 by John Arbash Meinel
Remove trailing whitespace.
145
        self.assertEqual("Rev 1: foo bar baz in %s" %
0.171.4 by Robert Collins
basics are all there
146
                            sender.branch.base,
147
                         sender.subject())
148
0.175.7 by John Arbash Meinel
split out SMTPConnection to its own file.
149
    def test_diff_filename(self):
150
        sender = self.get_sender()
151
        self.assertEqual('patch-1.diff', sender.diff_filename())
152
0.171.3 by Robert Collins
convert the switch basics to an email plugin
153
    def get_sender(self, text=sample_config):
0.171.11 by Robert Collins
Update tests to use non deprecated API's.
154
        self.branch = BzrDir.create_branch_convenience('.')
155
        tree = self.branch.bzrdir.open_workingtree()
0.171.38 by Vincent Ladeuil
Fix test suite ERRORs.
156
        tree.commit('foo bar baz\nfuzzy\nwuzzy', rev_id='A',
0.171.11 by Robert Collins
Update tests to use non deprecated API's.
157
            allow_pointless=True,
158
            timestamp=1,
159
            timezone=0,
160
            committer="Sample <john@example.com>",
161
            )
0.175.4 by John Arbash Meinel
Add a class for handling emails properly.
162
        my_config = self.branch.get_config()
0.171.2 by Robert Collins
publishing_product tied down
163
        config_file = StringIO(text)
0.171.12 by Robert Collins
Update Config usage in test suite to bzr.dev.
164
        (my_config._get_global_config()._get_parser(config_file))
0.171.24 by John Arbash Meinel
Switch to using a local repository if available,
165
        sender = EmailSender(self.branch, 'A', my_config)
166
        # This is usually only done after the EmailSender has locked the branch
167
        # and repository during send(), however, for testing, we need to do it
168
        # earlier, since send() is not called.
169
        sender._setup_revision_and_revno()
170
        return sender
171
172
173
class TestEmailerWithLocal(tests.TestCaseWithTransport):
174
    """Test that Emailer will use a local branch if supplied."""
175
176
    def test_local_has_revision(self):
177
        master_tree = self.make_branch_and_tree('master')
178
        self.build_tree(['master/a'])
179
        master_tree.add('a')
180
        master_tree.commit('a')
181
182
        child_tree = master_tree.bzrdir.sprout('child').open_workingtree()
183
        child_tree.branch.bind(master_tree.branch)
184
185
        self.build_tree(['child/b'])
186
        child_tree.add(['b'])
187
        revision_id = child_tree.commit('b')
188
189
        sender = EmailSender(master_tree.branch, revision_id,
190
                             master_tree.branch.get_config(),
191
                             local_branch=child_tree.branch)
192
193
        # Make sure we are using the 'local_branch' repository, and not the
194
        # remote one.
195
        self.assertIs(child_tree.branch.repository, sender.repository)
196
197
    def test_local_missing_revision(self):
198
        master_tree = self.make_branch_and_tree('master')
199
        self.build_tree(['master/a'])
200
        master_tree.add('a')
201
        master_tree.commit('a')
202
203
        child_tree = master_tree.bzrdir.sprout('child').open_workingtree()
204
        child_tree.branch.bind(master_tree.branch)
205
206
        self.build_tree(['master/c'])
207
        master_tree.add(['c'])
208
        revision_id = master_tree.commit('c')
209
210
        self.failIf(child_tree.branch.repository.has_revision(revision_id))
211
        sender = EmailSender(master_tree.branch, revision_id,
212
                             master_tree.branch.get_config(),
213
                             local_branch=child_tree.branch)
214
        # We should be using the master repository here, because the child
215
        # repository doesn't contain the revision.
216
        self.assertIs(master_tree.branch.repository, sender.repository)