summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--alot/commands/globals.py7
-rw-r--r--tests/commands/global_test.py28
2 files changed, 33 insertions, 2 deletions
diff --git a/alot/commands/globals.py b/alot/commands/globals.py
index 98021f3b..4c678afc 100644
--- a/alot/commands/globals.py
+++ b/alot/commands/globals.py
@@ -775,8 +775,11 @@ class ComposeCommand(Command):
priority='error')
return
try:
- with open(path) as f:
- self.envelope.parse_template(f.read())
+ with open(path, 'rb') as f:
+ blob = f.read()
+ encoding = helper.guess_encoding(blob)
+ logging.debug('template encoding: `%s`' % encoding)
+ self.envelope.parse_template(blob.decode(encoding))
except Exception as e:
ui.notify(str(e), priority='error')
return
diff --git a/tests/commands/global_test.py b/tests/commands/global_test.py
index 9489c909..96cba2d7 100644
--- a/tests/commands/global_test.py
+++ b/tests/commands/global_test.py
@@ -18,6 +18,7 @@
from __future__ import absolute_import
import os
+import tempfile
from twisted.trial import unittest
from twisted.internet.defer import inlineCallbacks
@@ -118,6 +119,33 @@ class TestComposeCommand(unittest.TestCase):
self.assertFalse(envelope.sign)
self.assertIs(envelope.sign_key, None)
+ @inlineCallbacks
+ def test_decode_template_on_loading(self):
+ subject = u'This is a täßϑ subject.'
+ to = u'recipient@mail.com'
+ _from = u'foo.bar@mail.fr'
+ body = u'Body\n地初店会継思識棋御招告外児山望掲領環。\n€mail body €nd.'
+ with tempfile.NamedTemporaryFile('wb', delete=False) as f:
+ txt = u'Subject: {}\nTo: {}\nFrom: {}\n{}'.format(subject, to,
+ _from, body)
+ f.write(txt.encode('utf-8'))
+ self.addCleanup(os.unlink, f.name)
+
+ cmd = g_commands.ComposeCommand(template=f.name)
+
+ # Crutch to exit the giant `apply` method early.
+ with mock.patch('alot.commands.globals.settings.get_account_by_address',
+ mock.Mock(side_effect=Stop)):
+ try:
+ yield cmd.apply(mock.Mock())
+ except Stop:
+ pass
+
+ self.assertEqual({'To': [to],
+ 'From': [_from],
+ 'Subject': [subject]}, cmd.envelope.headers)
+ self.assertEqual(body, cmd.envelope.body)
+
class TestExternalCommand(unittest.TestCase):