/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
16
3921.2.9 by Aaron Bentley
Update test to pass under LANG=C
17
import urllib
18
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
19
from bzrlib import (
3042.1.2 by Lukáš Lalinský
Don't use None as address in TestXDGEmail and add a test to check if it raises NoMailAddressSpecified with None.
20
    errors,
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
21
    mail_client,
22
    tests,
23
    urlutils,
3921.2.9 by Aaron Bentley
Update test to pass under LANG=C
24
    osutils,
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
25
    )
26
2790.2.1 by Keir Mierle
Add Mutt as a supported client email program. Also rearranges various listings
27
class TestMutt(tests.TestCase):
28
29
    def test_commandline(self):
30
        mutt = mail_client.Mutt(None)
31
        commandline = mutt._get_compose_commandline(None, None, 'file%')
32
        self.assertEqual(['-a', 'file%'], commandline)
33
        commandline = mutt._get_compose_commandline('jrandom@example.org',
34
                                                     'Hi there!', None)
4292.1.1 by Jelmer Vernooij
Mutt requires -- before the recipient address if -a is being used.
35
        self.assertEqual(['-s', 'Hi there!', '--', 'jrandom@example.org'],
2790.2.1 by Keir Mierle
Add Mutt as a supported client email program. Also rearranges various listings
36
                         commandline)
37
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
38
    def test_commandline_is_8bit(self):
39
        mutt = mail_client.Mutt(None)
40
        cmdline = mutt._get_compose_commandline(u'jrandom@example.org',
41
            u'Hi there!', u'file%')
42
        self.assertEqual(
4292.1.1 by Jelmer Vernooij
Mutt requires -- before the recipient address if -a is being used.
43
            ['-s', 'Hi there!', '-a', 'file%', '--', 'jrandom@example.org'],
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
44
            cmdline)
45
        for item in cmdline:
46
            self.assertFalse(isinstance(item, unicode),
47
                'Command-line item %r is unicode!' % item)
48
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
49
50
class TestThunderbird(tests.TestCase):
51
52
    def test_commandline(self):
2681.1.9 by Aaron Bentley
Add support for mail-from-editor
53
        tbird = mail_client.Thunderbird(None)
2681.1.8 by Aaron Bentley
Add Thunderbird support to bzr send
54
        commandline = tbird._get_compose_commandline(None, None,
55
                                                     'file%')
56
        self.assertEqual(['-compose', "attachment='%s'" %
57
                          urlutils.local_path_to_url('file%')], commandline)
58
        commandline = tbird._get_compose_commandline('jrandom@example.org',
4098.5.1 by Aaron Bentley
Allow specifying body for t-bird, evo and xdg
59
                                                     'Hi there!', None,
60
                                                     "bo'dy")
61
        self.assertEqual(['-compose', "body=bo%27dy,"
62
                                      "subject='Hi there!',"
63
                                      "to='jrandom@example.org'"],
64
                                      commandline)
2681.2.1 by Lukáš Lalinsky
Support for Evolution mail client.
65
3234.2.3 by Alexander Belchenko
mail_client.py: provide new private method ExternalMailClient._get_compose_8bit_commandline to make bug #139318 testable (as Aaron requested).
66
    def test_commandline_is_8bit(self):
67
        # test for bug #139318
68
        tbird = mail_client.Thunderbird(None)
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
69
        cmdline = tbird._get_compose_commandline(u'jrandom@example.org',
70
            u'Hi there!', u'file%')
71
        self.assertEqual(['-compose',
3234.2.3 by Alexander Belchenko
mail_client.py: provide new private method ExternalMailClient._get_compose_8bit_commandline to make bug #139318 testable (as Aaron requested).
72
            ("attachment='%s'," % urlutils.local_path_to_url('file%')) +
73
            "subject='Hi there!',to='jrandom@example.org'",
74
            ], cmdline)
75
        for item in cmdline:
76
            self.assertFalse(isinstance(item, unicode),
77
                'Command-line item %r is unicode!' % item)
78
2681.2.1 by Lukáš Lalinsky
Support for Evolution mail client.
79
3324.4.1 by Xavier Maillard
Replace mail-mode call with compose-mail from GNU Emacs.
80
class TestEmacsMail(tests.TestCase):
3302.6.1 by Xavier Maillard
Add mail-mode GNU Emacs mail package as a mail_client option.
81
82
    def test_commandline(self):
3324.4.1 by Xavier Maillard
Replace mail-mode call with compose-mail from GNU Emacs.
83
        eclient = mail_client.EmacsMail(None)
84
85
        commandline = eclient._get_compose_commandline(None, 'Hi there!', None)
86
        self.assertEqual(['--eval', '(compose-mail nil "Hi there!")'],
87
                         commandline)
3302.6.1 by Xavier Maillard
Add mail-mode GNU Emacs mail package as a mail_client option.
88
89
        commandline = eclient._get_compose_commandline('jrandom@example.org',
3324.4.1 by Xavier Maillard
Replace mail-mode call with compose-mail from GNU Emacs.
90
                                                       'Hi there!', None)
91
        self.assertEqual(['--eval',
92
                          '(compose-mail "jrandom@example.org" "Hi there!")'],
93
                         commandline)
94
95
        # We won't be able to know the temporary file name at this stage
96
        # so we can't raise an assertion with assertEqual
97
        cmdline = eclient._get_compose_commandline(None, None, 'file%')
98
        commandline = ' '.join(cmdline)
99
        self.assertContainsRe(commandline, '--eval')
100
        self.assertContainsRe(commandline, '(compose-mail nil nil)')
101
        self.assertContainsRe(commandline, '(load .*)')
102
        self.assertContainsRe(commandline, '(bzr-add-mime-att \"file%\")')
3302.6.1 by Xavier Maillard
Add mail-mode GNU Emacs mail package as a mail_client option.
103
104
    def test_commandline_is_8bit(self):
3324.4.1 by Xavier Maillard
Replace mail-mode call with compose-mail from GNU Emacs.
105
        eclient = mail_client.EmacsMail(None)
3302.6.1 by Xavier Maillard
Add mail-mode GNU Emacs mail package as a mail_client option.
106
        commandline = eclient._get_compose_commandline(u'jrandom@example.org',
107
            u'Hi there!', u'file%')
108
        for item in commandline:
109
            self.assertFalse(isinstance(item, unicode),
110
                'Command-line item %r is unicode!' % item)
111
112
2681.1.23 by Aaron Bentley
Add support for xdg-email
113
class TestXDGEmail(tests.TestCase):
114
115
    def test_commandline(self):
116
        xdg_email = mail_client.XDGEmail(None)
3042.1.2 by Lukáš Lalinský
Don't use None as address in TestXDGEmail and add a test to check if it raises NoMailAddressSpecified with None.
117
        self.assertRaises(errors.NoMailAddressSpecified,
118
                          xdg_email._get_compose_commandline,
119
                          None, None, 'file%')
120
        commandline = xdg_email._get_compose_commandline(
121
            'jrandom@example.org', None, 'file%')
122
        self.assertEqual(['jrandom@example.org', '--attach', 'file%'],
123
                         commandline)
2681.1.23 by Aaron Bentley
Add support for xdg-email
124
        commandline = xdg_email._get_compose_commandline(
4098.5.1 by Aaron Bentley
Allow specifying body for t-bird, evo and xdg
125
            'jrandom@example.org', 'Hi there!', None, "bo'dy")
126
        self.assertEqual(['jrandom@example.org', '--subject', 'Hi there!',
127
                          '--body', "bo'dy"], commandline)
2681.1.23 by Aaron Bentley
Add support for xdg-email
128
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
129
    def test_commandline_is_8bit(self):
130
        xdg_email = mail_client.XDGEmail(None)
131
        cmdline = xdg_email._get_compose_commandline(u'jrandom@example.org',
132
            u'Hi there!', u'file%')
133
        self.assertEqual(
134
            ['jrandom@example.org', '--subject', 'Hi there!',
135
             '--attach', 'file%'],
136
            cmdline)
137
        for item in cmdline:
138
            self.assertFalse(isinstance(item, unicode),
139
                'Command-line item %r is unicode!' % item)
140
2681.1.23 by Aaron Bentley
Add support for xdg-email
141
2681.2.1 by Lukáš Lalinsky
Support for Evolution mail client.
142
class TestEvolution(tests.TestCase):
143
144
    def test_commandline(self):
145
        evo = mail_client.Evolution(None)
146
        commandline = evo._get_compose_commandline(None, None, 'file%')
2681.1.18 by Aaron Bentley
Refactor to increase code sharing, allow multiple command names for tbird
147
        self.assertEqual(['mailto:?attach=file%25'], commandline)
2681.2.1 by Lukáš Lalinsky
Support for Evolution mail client.
148
        commandline = evo._get_compose_commandline('jrandom@example.org',
4098.5.1 by Aaron Bentley
Allow specifying body for t-bird, evo and xdg
149
                                                   'Hi there!', None, 'bo&dy')
150
        self.assertEqual(['mailto:jrandom@example.org?body=bo%26dy&'
151
                          'subject=Hi%20there%21'], commandline)
2681.1.21 by Aaron Bentley
Refactor prompt generation to make it testable, test it with unicode
152
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
153
    def test_commandline_is_8bit(self):
154
        evo = mail_client.Evolution(None)
155
        cmdline = evo._get_compose_commandline(u'jrandom@example.org',
156
            u'Hi there!', u'file%')
157
        self.assertEqual(
158
            ['mailto:jrandom@example.org?attach=file%25&subject=Hi%20there%21'
159
            ],
160
            cmdline)
161
        for item in cmdline:
162
            self.assertFalse(isinstance(item, unicode),
163
                'Command-line item %r is unicode!' % item)
164
2681.1.21 by Aaron Bentley
Refactor prompt generation to make it testable, test it with unicode
165
2681.5.2 by ghigo
update docs and tests
166
class TestKMail(tests.TestCase):
167
168
    def test_commandline(self):
2681.1.35 by Aaron Bentley
Rename test var evo => kmail
169
        kmail = mail_client.KMail(None)
170
        commandline = kmail._get_compose_commandline(None, None, 'file%')
2681.5.2 by ghigo
update docs and tests
171
        self.assertEqual(['--attach', 'file%'], commandline)
2681.1.35 by Aaron Bentley
Rename test var evo => kmail
172
        commandline = kmail._get_compose_commandline('jrandom@example.org',
173
                                                     'Hi there!', None)
2681.5.2 by ghigo
update docs and tests
174
        self.assertEqual(['-s', 'Hi there!', 'jrandom@example.org'],
175
                         commandline)
176
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
177
    def test_commandline_is_8bit(self):
178
        kmail = mail_client.KMail(None)
179
        cmdline = kmail._get_compose_commandline(u'jrandom@example.org',
180
            u'Hi there!', u'file%')
181
        self.assertEqual(
182
            ['-s', 'Hi there!', '--attach', 'file%', 'jrandom@example.org'],
183
            cmdline)
184
        for item in cmdline:
185
            self.assertFalse(isinstance(item, unicode),
186
                'Command-line item %r is unicode!' % item)
187
2681.5.2 by ghigo
update docs and tests
188
3921.2.3 by Gavin Panella
Tests for the Claws mail client.
189
class TestClaws(tests.TestCase):
190
191
    def test_commandline(self):
192
        claws = mail_client.Claws(None)
3921.2.4 by Gavin Panella
Use the --attach option, and don't specify a From: header.
193
        commandline = claws._get_compose_commandline(
4401.2.2 by Barry Warsaw
Much simpler approach to support From: in Claws, after discussion with
194
            'jrandom@example.org', None, 'file%')
3921.2.4 by Gavin Panella
Use the --attach option, and don't specify a From: header.
195
        self.assertEqual(
4401.2.2 by Barry Warsaw
Much simpler approach to support From: in Claws, after discussion with
196
            ['--compose', 'mailto:jrandom@example.org?', '--attach', 'file%'],
197
            commandline)
3921.2.4 by Gavin Panella
Use the --attach option, and don't specify a From: header.
198
        commandline = claws._get_compose_commandline(
199
            'jrandom@example.org', 'Hi there!', None)
200
        self.assertEqual(
201
            ['--compose',
202
             'mailto:jrandom@example.org?subject=Hi%20there%21'],
3921.2.3 by Gavin Panella
Tests for the Claws mail client.
203
            commandline)
204
205
    def test_commandline_is_8bit(self):
206
        claws = mail_client.Claws(None)
207
        cmdline = claws._get_compose_commandline(
3921.2.7 by Gavin Panella
Use a non-ascii character in test_commandline_is_8bit.
208
            u'jrandom@example.org', u'\xb5cosm of fun!', u'file%')
3921.2.9 by Aaron Bentley
Update test to pass under LANG=C
209
        subject_string = urllib.quote(
210
            u'\xb5cosm of fun!'.encode(osutils.get_user_encoding(), 'replace'))
3921.2.3 by Gavin Panella
Tests for the Claws mail client.
211
        self.assertEqual(
212
            ['--compose',
3921.2.9 by Aaron Bentley
Update test to pass under LANG=C
213
             'mailto:jrandom@example.org?subject=%s' % subject_string,
3921.2.4 by Gavin Panella
Use the --attach option, and don't specify a From: header.
214
             '--attach',
215
             'file%'],
3921.2.3 by Gavin Panella
Tests for the Claws mail client.
216
            cmdline)
217
        for item in cmdline:
218
            self.assertFalse(isinstance(item, unicode),
219
                'Command-line item %r is unicode!' % item)
220
4401.2.2 by Barry Warsaw
Much simpler approach to support From: in Claws, after discussion with
221
    def test_with_from(self):
222
        claws = mail_client.Claws(None)
223
        cmdline = claws._get_compose_commandline(
224
            u'jrandom@example.org', None, None, None, u'qrandom@example.com')
225
        self.assertEqual(
226
            ['--compose',
227
             'mailto:jrandom@example.org?from=qrandom%40example.com'],
228
            cmdline)
229
230
    def test_to_required(self):
231
        claws = mail_client.Claws(None)
232
        self.assertRaises(errors.NoMailAddressSpecified,
233
                          claws._get_compose_commandline,
234
                          None, None, 'file%')
235
4401.2.3 by Barry Warsaw
Add test for including body text for Claws.
236
    def test_with_body(self):
237
        claws = mail_client.Claws(None)
238
        cmdline = claws._get_compose_commandline(
239
            u'jrandom@example.org', None, None, 'This is some body text')
240
        self.assertEqual(
241
            ['--compose',
242
             'mailto:jrandom@example.org?body=This%20is%20some%20body%20text'],
243
            cmdline)
244
3921.2.3 by Gavin Panella
Tests for the Claws mail client.
245
2681.1.21 by Aaron Bentley
Refactor prompt generation to make it testable, test it with unicode
246
class TestEditor(tests.TestCase):
247
248
    def test_get_merge_prompt_unicode(self):
249
        """Prompt, to and subject are unicode, the attachement is binary"""
250
        editor = mail_client.Editor(None)
251
        prompt = editor._get_merge_prompt(u'foo\u1234',
252
                                        u'bar\u1234',
253
                                        u'baz\u1234',
254
                                        u'qux\u1234'.encode('utf-8'))
255
        self.assertContainsRe(prompt, u'foo\u1234(.|\n)*bar\u1234'
256
                              u'(.|\n)*baz\u1234(.|\n)*qux\u1234')
257
        editor._get_merge_prompt(u'foo', u'bar', u'baz', 'qux\xff')
3270.3.2 by James Westby
Add a smoke test to check that the DefaultMail client passes through
258
259
260
class DummyMailClient(object):
261
262
    def compose_merge_request(self, *args, **kwargs):
263
        self.args = args
264
        self.kwargs = kwargs
265
266
267
class DefaultMailDummyClient(mail_client.DefaultMail):
268
269
    def __init__(self):
270
        self.client = DummyMailClient()
271
272
    def _mail_client(self):
273
        return self.client
274
275
276
class TestDefaultMail(tests.TestCase):
277
278
    def test_compose_merge_request(self):
279
        client = DefaultMailDummyClient()
280
        to = "a@b.com"
281
        subject = "[MERGE]"
282
        directive = "directive",
283
        basename = "merge"
284
        client.compose_merge_request(to, subject, directive,
285
                                     basename=basename)
286
        dummy_client = client.client
287
        self.assertEqual(dummy_client.args, (to, subject, directive))
4098.5.10 by Aaron Bentley
Fix test case.
288
        self.assertEqual(dummy_client.kwargs,
289
                         {"basename": basename, 'body': None})