~bzr-pqm/bzr/bzr.dev

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
221
222
223
224
225
# Copyright (C) 2007 Canonical Ltd
#
# 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 bzrlib import (
    errors,
    mail_client,
    tests,
    urlutils,
    )

class TestMutt(tests.TestCase):

    def test_commandline(self):
        mutt = mail_client.Mutt(None)
        commandline = mutt._get_compose_commandline(None, None, 'file%')
        self.assertEqual(['-a', 'file%'], commandline)
        commandline = mutt._get_compose_commandline('jrandom@example.org',
                                                     'Hi there!', None)
        self.assertEqual(['-s', 'Hi there!', 'jrandom@example.org'],
                         commandline)

    def test_commandline_is_8bit(self):
        mutt = mail_client.Mutt(None)
        cmdline = mutt._get_compose_commandline(u'jrandom@example.org',
            u'Hi there!', u'file%')
        self.assertEqual(
            ['-s', 'Hi there!', '-a', 'file%', 'jrandom@example.org'],
            cmdline)
        for item in cmdline:
            self.assertFalse(isinstance(item, unicode),
                'Command-line item %r is unicode!' % item)


class TestThunderbird(tests.TestCase):

    def test_commandline(self):
        tbird = mail_client.Thunderbird(None)
        commandline = tbird._get_compose_commandline(None, None,
                                                     'file%')
        self.assertEqual(['-compose', "attachment='%s'" %
                          urlutils.local_path_to_url('file%')], commandline)
        commandline = tbird._get_compose_commandline('jrandom@example.org',
                                                     'Hi there!', None)
        self.assertEqual(['-compose', "subject='Hi there!',"
                                      "to='jrandom@example.org'"], commandline)

    def test_commandline_is_8bit(self):
        # test for bug #139318
        tbird = mail_client.Thunderbird(None)
        cmdline = tbird._get_compose_commandline(u'jrandom@example.org',
            u'Hi there!', u'file%')
        self.assertEqual(['-compose',
            ("attachment='%s'," % urlutils.local_path_to_url('file%')) +
            "subject='Hi there!',to='jrandom@example.org'",
            ], cmdline)
        for item in cmdline:
            self.assertFalse(isinstance(item, unicode),
                'Command-line item %r is unicode!' % item)


class TestEmacsMail(tests.TestCase):

    def test_commandline(self):
        eclient = mail_client.EmacsMail(None)

        commandline = eclient._get_compose_commandline(None, 'Hi there!', None)
        self.assertEqual(['--eval', '(compose-mail nil "Hi there!")'],
                         commandline)

        commandline = eclient._get_compose_commandline('jrandom@example.org',
                                                       'Hi there!', None)
        self.assertEqual(['--eval',
                          '(compose-mail "jrandom@example.org" "Hi there!")'],
                         commandline)

        # We won't be able to know the temporary file name at this stage
        # so we can't raise an assertion with assertEqual
        cmdline = eclient._get_compose_commandline(None, None, 'file%')
        commandline = ' '.join(cmdline)
        self.assertContainsRe(commandline, '--eval')
        self.assertContainsRe(commandline, '(compose-mail nil nil)')
        self.assertContainsRe(commandline, '(load .*)')
        self.assertContainsRe(commandline, '(bzr-add-mime-att \"file%\")')

    def test_commandline_is_8bit(self):
        eclient = mail_client.EmacsMail(None)
        commandline = eclient._get_compose_commandline(u'jrandom@example.org',
            u'Hi there!', u'file%')
        for item in commandline:
            self.assertFalse(isinstance(item, unicode),
                'Command-line item %r is unicode!' % item)


class TestXDGEmail(tests.TestCase):

    def test_commandline(self):
        xdg_email = mail_client.XDGEmail(None)
        self.assertRaises(errors.NoMailAddressSpecified,
                          xdg_email._get_compose_commandline,
                          None, None, 'file%')
        commandline = xdg_email._get_compose_commandline(
            'jrandom@example.org', None, 'file%')
        self.assertEqual(['jrandom@example.org', '--attach', 'file%'],
                         commandline)
        commandline = xdg_email._get_compose_commandline(
            'jrandom@example.org', 'Hi there!', None)
        self.assertEqual(['jrandom@example.org', '--subject', 'Hi there!'],
                         commandline)

    def test_commandline_is_8bit(self):
        xdg_email = mail_client.XDGEmail(None)
        cmdline = xdg_email._get_compose_commandline(u'jrandom@example.org',
            u'Hi there!', u'file%')
        self.assertEqual(
            ['jrandom@example.org', '--subject', 'Hi there!',
             '--attach', 'file%'],
            cmdline)
        for item in cmdline:
            self.assertFalse(isinstance(item, unicode),
                'Command-line item %r is unicode!' % item)


class TestEvolution(tests.TestCase):

    def test_commandline(self):
        evo = mail_client.Evolution(None)
        commandline = evo._get_compose_commandline(None, None, 'file%')
        self.assertEqual(['mailto:?attach=file%25'], commandline)
        commandline = evo._get_compose_commandline('jrandom@example.org',
                                                   'Hi there!', None)
        self.assertEqual(['mailto:jrandom@example.org?subject=Hi%20there%21'],
                         commandline)

    def test_commandline_is_8bit(self):
        evo = mail_client.Evolution(None)
        cmdline = evo._get_compose_commandline(u'jrandom@example.org',
            u'Hi there!', u'file%')
        self.assertEqual(
            ['mailto:jrandom@example.org?attach=file%25&subject=Hi%20there%21'
            ],
            cmdline)
        for item in cmdline:
            self.assertFalse(isinstance(item, unicode),
                'Command-line item %r is unicode!' % item)


class TestKMail(tests.TestCase):

    def test_commandline(self):
        kmail = mail_client.KMail(None)
        commandline = kmail._get_compose_commandline(None, None, 'file%')
        self.assertEqual(['--attach', 'file%'], commandline)
        commandline = kmail._get_compose_commandline('jrandom@example.org',
                                                     'Hi there!', None)
        self.assertEqual(['-s', 'Hi there!', 'jrandom@example.org'],
                         commandline)

    def test_commandline_is_8bit(self):
        kmail = mail_client.KMail(None)
        cmdline = kmail._get_compose_commandline(u'jrandom@example.org',
            u'Hi there!', u'file%')
        self.assertEqual(
            ['-s', 'Hi there!', '--attach', 'file%', 'jrandom@example.org'],
            cmdline)
        for item in cmdline:
            self.assertFalse(isinstance(item, unicode),
                'Command-line item %r is unicode!' % item)


class TestEditor(tests.TestCase):

    def test_get_merge_prompt_unicode(self):
        """Prompt, to and subject are unicode, the attachement is binary"""
        editor = mail_client.Editor(None)
        prompt = editor._get_merge_prompt(u'foo\u1234',
                                        u'bar\u1234',
                                        u'baz\u1234',
                                        u'qux\u1234'.encode('utf-8'))
        self.assertContainsRe(prompt, u'foo\u1234(.|\n)*bar\u1234'
                              u'(.|\n)*baz\u1234(.|\n)*qux\u1234')
        editor._get_merge_prompt(u'foo', u'bar', u'baz', 'qux\xff')


class DummyMailClient(object):

    def compose_merge_request(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs


class DefaultMailDummyClient(mail_client.DefaultMail):

    def __init__(self):
        self.client = DummyMailClient()

    def _mail_client(self):
        return self.client


class TestDefaultMail(tests.TestCase):

    def test_compose_merge_request(self):
        client = DefaultMailDummyClient()
        to = "a@b.com"
        subject = "[MERGE]"
        directive = "directive",
        basename = "merge"
        client.compose_merge_request(to, subject, directive,
                                     basename=basename)
        dummy_client = client.client
        self.assertEqual(dummy_client.args, (to, subject, directive))
        self.assertEqual(dummy_client.kwargs, {"basename":basename})