summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_email
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2016-09-09 22:39:18 (GMT)
committerR David Murray <rdmurray@bitdance.com>2016-09-09 22:39:18 (GMT)
commit06ed218ed0020003ac388572fbcf09b88075b664 (patch)
tree5a44ac4cb9d85a35fe9d8423e6700176f5de8352 /Lib/test/test_email
parent37df068e862c4bbab16da00de72655c4a737ea94 (diff)
downloadcpython-06ed218ed0020003ac388572fbcf09b88075b664.zip
cpython-06ed218ed0020003ac388572fbcf09b88075b664.tar.gz
cpython-06ed218ed0020003ac388572fbcf09b88075b664.tar.bz2
#20476: add a message_factory policy attribute to email.
Diffstat (limited to 'Lib/test/test_email')
-rw-r--r--Lib/test/test_email/test_parser.py91
-rw-r--r--Lib/test/test_email/test_policy.py42
2 files changed, 87 insertions, 46 deletions
diff --git a/Lib/test/test_email/test_parser.py b/Lib/test/test_email/test_parser.py
index 8ddc1763..06c8640 100644
--- a/Lib/test/test_email/test_parser.py
+++ b/Lib/test/test_email/test_parser.py
@@ -1,7 +1,7 @@
import io
import email
import unittest
-from email.message import Message
+from email.message import Message, EmailMessage
from email.policy import default
from test.test_email import TestEmailBase
@@ -39,38 +39,71 @@ class TestParserBase:
# The unicode line splitter splits on unicode linebreaks, which are
# more numerous than allowed by the email RFCs; make sure we are only
# splitting on those two.
- msg = self.parser(
- "Next-Line: not\x85broken\r\n"
- "Null: not\x00broken\r\n"
- "Vertical-Tab: not\vbroken\r\n"
- "Form-Feed: not\fbroken\r\n"
- "File-Separator: not\x1Cbroken\r\n"
- "Group-Separator: not\x1Dbroken\r\n"
- "Record-Separator: not\x1Ebroken\r\n"
- "Line-Separator: not\u2028broken\r\n"
- "Paragraph-Separator: not\u2029broken\r\n"
- "\r\n",
- policy=default,
- )
- self.assertEqual(msg.items(), [
- ("Next-Line", "not\x85broken"),
- ("Null", "not\x00broken"),
- ("Vertical-Tab", "not\vbroken"),
- ("Form-Feed", "not\fbroken"),
- ("File-Separator", "not\x1Cbroken"),
- ("Group-Separator", "not\x1Dbroken"),
- ("Record-Separator", "not\x1Ebroken"),
- ("Line-Separator", "not\u2028broken"),
- ("Paragraph-Separator", "not\u2029broken"),
- ])
- self.assertEqual(msg.get_payload(), "")
+ for parser in self.parsers:
+ with self.subTest(parser=parser.__name__):
+ msg = parser(
+ "Next-Line: not\x85broken\r\n"
+ "Null: not\x00broken\r\n"
+ "Vertical-Tab: not\vbroken\r\n"
+ "Form-Feed: not\fbroken\r\n"
+ "File-Separator: not\x1Cbroken\r\n"
+ "Group-Separator: not\x1Dbroken\r\n"
+ "Record-Separator: not\x1Ebroken\r\n"
+ "Line-Separator: not\u2028broken\r\n"
+ "Paragraph-Separator: not\u2029broken\r\n"
+ "\r\n",
+ policy=default,
+ )
+ self.assertEqual(msg.items(), [
+ ("Next-Line", "not\x85broken"),
+ ("Null", "not\x00broken"),
+ ("Vertical-Tab", "not\vbroken"),
+ ("Form-Feed", "not\fbroken"),
+ ("File-Separator", "not\x1Cbroken"),
+ ("Group-Separator", "not\x1Dbroken"),
+ ("Record-Separator", "not\x1Ebroken"),
+ ("Line-Separator", "not\u2028broken"),
+ ("Paragraph-Separator", "not\u2029broken"),
+ ])
+ self.assertEqual(msg.get_payload(), "")
+
+ class MyMessage(EmailMessage):
+ pass
+
+ def test_custom_message_factory_on_policy(self):
+ for parser in self.parsers:
+ with self.subTest(parser=parser.__name__):
+ MyPolicy = default.clone(message_factory=self.MyMessage)
+ msg = parser("To: foo\n\ntest", policy=MyPolicy)
+ self.assertIsInstance(msg, self.MyMessage)
+
+ def test_factory_arg_overrides_policy(self):
+ for parser in self.parsers:
+ with self.subTest(parser=parser.__name__):
+ MyPolicy = default.clone(message_factory=self.MyMessage)
+ msg = parser("To: foo\n\ntest", Message, policy=MyPolicy)
+ self.assertNotIsInstance(msg, self.MyMessage)
+ self.assertIsInstance(msg, Message)
+
+# Play some games to get nice output in subTest. This code could be clearer
+# if staticmethod supported __name__.
+
+def message_from_file(s, *args, **kw):
+ f = io.StringIO(s)
+ return email.message_from_file(f, *args, **kw)
class TestParser(TestParserBase, TestEmailBase):
- parser = staticmethod(email.message_from_string)
+ parsers = (email.message_from_string, message_from_file)
+
+def message_from_bytes(s, *args, **kw):
+ return email.message_from_bytes(s.encode(), *args, **kw)
+
+def message_from_binary_file(s, *args, **kw):
+ f = io.BytesIO(s.encode())
+ return email.message_from_binary_file(f, *args, **kw)
class TestBytesParser(TestParserBase, TestEmailBase):
- def parser(self, s, *args, **kw):
- return email.message_from_bytes(s.encode(), *args, **kw)
+ parsers = (message_from_bytes, message_from_binary_file)
if __name__ == '__main__':
diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py
index 70ac4db..1d95d03 100644
--- a/Lib/test/test_email/test_policy.py
+++ b/Lib/test/test_email/test_policy.py
@@ -5,6 +5,7 @@ import unittest
import email.policy
import email.parser
import email.generator
+import email.message
from email import headerregistry
def make_defaults(base_defaults, differences):
@@ -23,6 +24,7 @@ class PolicyAPITests(unittest.TestCase):
'cte_type': '8bit',
'raise_on_defect': False,
'mangle_from_': True,
+ 'message_factory': email.message.Message,
}
# These default values are the ones set on email.policy.default.
# If any of these defaults change, the docs must be updated.
@@ -34,6 +36,7 @@ class PolicyAPITests(unittest.TestCase):
'refold_source': 'long',
'content_manager': email.policy.EmailPolicy.content_manager,
'mangle_from_': False,
+ 'message_factory': email.message.EmailMessage,
})
# For each policy under test, we give here what we expect the defaults to
@@ -62,20 +65,22 @@ class PolicyAPITests(unittest.TestCase):
def test_defaults(self):
for policy, expected in self.policies.items():
for attr, value in expected.items():
- self.assertEqual(getattr(policy, attr), value,
- ("change {} docs/docstrings if defaults have "
- "changed").format(policy))
+ with self.subTest(policy=policy, attr=attr):
+ self.assertEqual(getattr(policy, attr), value,
+ ("change {} docs/docstrings if defaults have "
+ "changed").format(policy))
def test_all_attributes_covered(self):
for policy, expected in self.policies.items():
for attr in dir(policy):
- if (attr.startswith('_') or
- isinstance(getattr(email.policy.EmailPolicy, attr),
- types.FunctionType)):
- continue
- else:
- self.assertIn(attr, expected,
- "{} is not fully tested".format(attr))
+ with self.subTest(policy=policy, attr=attr):
+ if (attr.startswith('_') or
+ isinstance(getattr(email.policy.EmailPolicy, attr),
+ types.FunctionType)):
+ continue
+ else:
+ self.assertIn(attr, expected,
+ "{} is not fully tested".format(attr))
def test_abc(self):
with self.assertRaises(TypeError) as cm:
@@ -237,6 +242,9 @@ class PolicyAPITests(unittest.TestCase):
# wins), but that the order still works (right overrides left).
+class TestException(Exception):
+ pass
+
class TestPolicyPropagation(unittest.TestCase):
# The abstract methods are used by the parser but not by the wrapper
@@ -244,40 +252,40 @@ class TestPolicyPropagation(unittest.TestCase):
# policy was actually propagated all the way to feedparser.
class MyPolicy(email.policy.Policy):
def badmethod(self, *args, **kw):
- raise Exception("test")
+ raise TestException("test")
fold = fold_binary = header_fetch_parser = badmethod
header_source_parse = header_store_parse = badmethod
def test_message_from_string(self):
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_string("Subject: test\n\n",
policy=self.MyPolicy)
def test_message_from_bytes(self):
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_bytes(b"Subject: test\n\n",
policy=self.MyPolicy)
def test_message_from_file(self):
f = io.StringIO('Subject: test\n\n')
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_file(f, policy=self.MyPolicy)
def test_message_from_binary_file(self):
f = io.BytesIO(b'Subject: test\n\n')
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
email.message_from_binary_file(f, policy=self.MyPolicy)
# These are redundant, but we need them for black-box completeness.
def test_parser(self):
p = email.parser.Parser(policy=self.MyPolicy)
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
p.parsestr('Subject: test\n\n')
def test_bytes_parser(self):
p = email.parser.BytesParser(policy=self.MyPolicy)
- with self.assertRaisesRegex(Exception, "^test$"):
+ with self.assertRaisesRegex(TestException, "^test$"):
p.parsebytes(b'Subject: test\n\n')
# Now that we've established that all the parse methods get the