~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_mail_client.py

  • Committer: John Arbash Meinel
  • Date: 2005-11-30 15:43:57 UTC
  • mto: (1185.50.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1518.
  • Revision ID: john@arbash-meinel.com-20051130154357-614206b3a7b83cd0
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
import urllib
18
 
 
19
 
from bzrlib import (
20
 
    errors,
21
 
    mail_client,
22
 
    tests,
23
 
    urlutils,
24
 
    osutils,
25
 
    )
26
 
 
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)
35
 
        self.assertEqual(['-s', 'Hi there!', 'jrandom@example.org'],
36
 
                         commandline)
37
 
 
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(
43
 
            ['-s', 'Hi there!', '-a', 'file%', 'jrandom@example.org'],
44
 
            cmdline)
45
 
        for item in cmdline:
46
 
            self.assertFalse(isinstance(item, unicode),
47
 
                'Command-line item %r is unicode!' % item)
48
 
 
49
 
 
50
 
class TestThunderbird(tests.TestCase):
51
 
 
52
 
    def test_commandline(self):
53
 
        tbird = mail_client.Thunderbird(None)
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',
59
 
                                                     'Hi there!', None)
60
 
        self.assertEqual(['-compose', "subject='Hi there!',"
61
 
                                      "to='jrandom@example.org'"], commandline)
62
 
 
63
 
    def test_commandline_is_8bit(self):
64
 
        # test for bug #139318
65
 
        tbird = mail_client.Thunderbird(None)
66
 
        cmdline = tbird._get_compose_commandline(u'jrandom@example.org',
67
 
            u'Hi there!', u'file%')
68
 
        self.assertEqual(['-compose',
69
 
            ("attachment='%s'," % urlutils.local_path_to_url('file%')) +
70
 
            "subject='Hi there!',to='jrandom@example.org'",
71
 
            ], cmdline)
72
 
        for item in cmdline:
73
 
            self.assertFalse(isinstance(item, unicode),
74
 
                'Command-line item %r is unicode!' % item)
75
 
 
76
 
 
77
 
class TestEmacsMail(tests.TestCase):
78
 
 
79
 
    def test_commandline(self):
80
 
        eclient = mail_client.EmacsMail(None)
81
 
 
82
 
        commandline = eclient._get_compose_commandline(None, 'Hi there!', None)
83
 
        self.assertEqual(['--eval', '(compose-mail nil "Hi there!")'],
84
 
                         commandline)
85
 
 
86
 
        commandline = eclient._get_compose_commandline('jrandom@example.org',
87
 
                                                       'Hi there!', None)
88
 
        self.assertEqual(['--eval',
89
 
                          '(compose-mail "jrandom@example.org" "Hi there!")'],
90
 
                         commandline)
91
 
 
92
 
        # We won't be able to know the temporary file name at this stage
93
 
        # so we can't raise an assertion with assertEqual
94
 
        cmdline = eclient._get_compose_commandline(None, None, 'file%')
95
 
        commandline = ' '.join(cmdline)
96
 
        self.assertContainsRe(commandline, '--eval')
97
 
        self.assertContainsRe(commandline, '(compose-mail nil nil)')
98
 
        self.assertContainsRe(commandline, '(load .*)')
99
 
        self.assertContainsRe(commandline, '(bzr-add-mime-att \"file%\")')
100
 
 
101
 
    def test_commandline_is_8bit(self):
102
 
        eclient = mail_client.EmacsMail(None)
103
 
        commandline = eclient._get_compose_commandline(u'jrandom@example.org',
104
 
            u'Hi there!', u'file%')
105
 
        for item in commandline:
106
 
            self.assertFalse(isinstance(item, unicode),
107
 
                'Command-line item %r is unicode!' % item)
108
 
 
109
 
 
110
 
class TestXDGEmail(tests.TestCase):
111
 
 
112
 
    def test_commandline(self):
113
 
        xdg_email = mail_client.XDGEmail(None)
114
 
        self.assertRaises(errors.NoMailAddressSpecified,
115
 
                          xdg_email._get_compose_commandline,
116
 
                          None, None, 'file%')
117
 
        commandline = xdg_email._get_compose_commandline(
118
 
            'jrandom@example.org', None, 'file%')
119
 
        self.assertEqual(['jrandom@example.org', '--attach', 'file%'],
120
 
                         commandline)
121
 
        commandline = xdg_email._get_compose_commandline(
122
 
            'jrandom@example.org', 'Hi there!', None)
123
 
        self.assertEqual(['jrandom@example.org', '--subject', 'Hi there!'],
124
 
                         commandline)
125
 
 
126
 
    def test_commandline_is_8bit(self):
127
 
        xdg_email = mail_client.XDGEmail(None)
128
 
        cmdline = xdg_email._get_compose_commandline(u'jrandom@example.org',
129
 
            u'Hi there!', u'file%')
130
 
        self.assertEqual(
131
 
            ['jrandom@example.org', '--subject', 'Hi there!',
132
 
             '--attach', 'file%'],
133
 
            cmdline)
134
 
        for item in cmdline:
135
 
            self.assertFalse(isinstance(item, unicode),
136
 
                'Command-line item %r is unicode!' % item)
137
 
 
138
 
 
139
 
class TestEvolution(tests.TestCase):
140
 
 
141
 
    def test_commandline(self):
142
 
        evo = mail_client.Evolution(None)
143
 
        commandline = evo._get_compose_commandline(None, None, 'file%')
144
 
        self.assertEqual(['mailto:?attach=file%25'], commandline)
145
 
        commandline = evo._get_compose_commandline('jrandom@example.org',
146
 
                                                   'Hi there!', None)
147
 
        self.assertEqual(['mailto:jrandom@example.org?subject=Hi%20there%21'],
148
 
                         commandline)
149
 
 
150
 
    def test_commandline_is_8bit(self):
151
 
        evo = mail_client.Evolution(None)
152
 
        cmdline = evo._get_compose_commandline(u'jrandom@example.org',
153
 
            u'Hi there!', u'file%')
154
 
        self.assertEqual(
155
 
            ['mailto:jrandom@example.org?attach=file%25&subject=Hi%20there%21'
156
 
            ],
157
 
            cmdline)
158
 
        for item in cmdline:
159
 
            self.assertFalse(isinstance(item, unicode),
160
 
                'Command-line item %r is unicode!' % item)
161
 
 
162
 
 
163
 
class TestKMail(tests.TestCase):
164
 
 
165
 
    def test_commandline(self):
166
 
        kmail = mail_client.KMail(None)
167
 
        commandline = kmail._get_compose_commandline(None, None, 'file%')
168
 
        self.assertEqual(['--attach', 'file%'], commandline)
169
 
        commandline = kmail._get_compose_commandline('jrandom@example.org',
170
 
                                                     'Hi there!', None)
171
 
        self.assertEqual(['-s', 'Hi there!', 'jrandom@example.org'],
172
 
                         commandline)
173
 
 
174
 
    def test_commandline_is_8bit(self):
175
 
        kmail = mail_client.KMail(None)
176
 
        cmdline = kmail._get_compose_commandline(u'jrandom@example.org',
177
 
            u'Hi there!', u'file%')
178
 
        self.assertEqual(
179
 
            ['-s', 'Hi there!', '--attach', 'file%', 'jrandom@example.org'],
180
 
            cmdline)
181
 
        for item in cmdline:
182
 
            self.assertFalse(isinstance(item, unicode),
183
 
                'Command-line item %r is unicode!' % item)
184
 
 
185
 
 
186
 
class TestClaws(tests.TestCase):
187
 
 
188
 
    def test_commandline(self):
189
 
        claws = mail_client.Claws(None)
190
 
        commandline = claws._get_compose_commandline(
191
 
            None, None, 'file%')
192
 
        self.assertEqual(
193
 
            ['--compose', 'mailto:?', '--attach', 'file%'], commandline)
194
 
        commandline = claws._get_compose_commandline(
195
 
            'jrandom@example.org', 'Hi there!', None)
196
 
        self.assertEqual(
197
 
            ['--compose',
198
 
             'mailto:jrandom@example.org?subject=Hi%20there%21'],
199
 
            commandline)
200
 
 
201
 
    def test_commandline_is_8bit(self):
202
 
        claws = mail_client.Claws(None)
203
 
        cmdline = claws._get_compose_commandline(
204
 
            u'jrandom@example.org', u'\xb5cosm of fun!', u'file%')
205
 
        subject_string = urllib.quote(
206
 
            u'\xb5cosm of fun!'.encode(osutils.get_user_encoding(), 'replace'))
207
 
        self.assertEqual(
208
 
            ['--compose',
209
 
             'mailto:jrandom@example.org?subject=%s' % subject_string,
210
 
             '--attach',
211
 
             'file%'],
212
 
            cmdline)
213
 
        for item in cmdline:
214
 
            self.assertFalse(isinstance(item, unicode),
215
 
                'Command-line item %r is unicode!' % item)
216
 
 
217
 
 
218
 
class TestEditor(tests.TestCase):
219
 
 
220
 
    def test_get_merge_prompt_unicode(self):
221
 
        """Prompt, to and subject are unicode, the attachement is binary"""
222
 
        editor = mail_client.Editor(None)
223
 
        prompt = editor._get_merge_prompt(u'foo\u1234',
224
 
                                        u'bar\u1234',
225
 
                                        u'baz\u1234',
226
 
                                        u'qux\u1234'.encode('utf-8'))
227
 
        self.assertContainsRe(prompt, u'foo\u1234(.|\n)*bar\u1234'
228
 
                              u'(.|\n)*baz\u1234(.|\n)*qux\u1234')
229
 
        editor._get_merge_prompt(u'foo', u'bar', u'baz', 'qux\xff')
230
 
 
231
 
 
232
 
class DummyMailClient(object):
233
 
 
234
 
    def compose_merge_request(self, *args, **kwargs):
235
 
        self.args = args
236
 
        self.kwargs = kwargs
237
 
 
238
 
 
239
 
class DefaultMailDummyClient(mail_client.DefaultMail):
240
 
 
241
 
    def __init__(self):
242
 
        self.client = DummyMailClient()
243
 
 
244
 
    def _mail_client(self):
245
 
        return self.client
246
 
 
247
 
 
248
 
class TestDefaultMail(tests.TestCase):
249
 
 
250
 
    def test_compose_merge_request(self):
251
 
        client = DefaultMailDummyClient()
252
 
        to = "a@b.com"
253
 
        subject = "[MERGE]"
254
 
        directive = "directive",
255
 
        basename = "merge"
256
 
        client.compose_merge_request(to, subject, directive,
257
 
                                     basename=basename)
258
 
        dummy_client = client.client
259
 
        self.assertEqual(dummy_client.args, (to, subject, directive))
260
 
        self.assertEqual(dummy_client.kwargs, {"basename":basename})