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