diff options
author | Petri Lehtinen <petri@digip.org> | 2012-09-25 19:03:35 (GMT) |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2012-09-25 19:06:07 (GMT) |
commit | f6b30385ccd7be11628670357d8434e16226594c (patch) | |
tree | 73f4462f4bca5f1c7308b33da1954befce1dd96f /Lib/test/test_mailbox.py | |
parent | 15c88490011743585134436c7ca0f5186e5dd2a3 (diff) | |
parent | f39884bb5a01c3fa9db74833d2d8a05bf0530315 (diff) | |
download | cpython-f6b30385ccd7be11628670357d8434e16226594c.zip cpython-f6b30385ccd7be11628670357d8434e16226594c.tar.gz cpython-f6b30385ccd7be11628670357d8434e16226594c.tar.bz2 |
#15222: Merge 3.2
Diffstat (limited to 'Lib/test/test_mailbox.py')
-rw-r--r-- | Lib/test/test_mailbox.py | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index fd7f319..6b1e933 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -53,7 +53,7 @@ class TestMailbox(TestBase): maxDiff = None _factory = None # Overridden by subclasses to reuse tests - _template = 'From: foo\n\n%s' + _template = 'From: foo\n\n%s\n' def setUp(self): self._path = support.TESTFN @@ -232,7 +232,7 @@ class TestMailbox(TestBase): key0 = self._box.add(self._template % 0) msg = self._box.get(key0) self.assertEqual(msg['from'], 'foo') - self.assertEqual(msg.get_payload(), '0') + self.assertEqual(msg.get_payload(), '0\n') self.assertIs(self._box.get('foo'), None) self.assertIs(self._box.get('foo', False), False) self._box.close() @@ -240,14 +240,14 @@ class TestMailbox(TestBase): key1 = self._box.add(self._template % 1) msg = self._box.get(key1) self.assertEqual(msg['from'], 'foo') - self.assertEqual(msg.get_payload(), '1') + self.assertEqual(msg.get_payload(), '1\n') def test_getitem(self): # Retrieve message using __getitem__() key0 = self._box.add(self._template % 0) msg = self._box[key0] self.assertEqual(msg['from'], 'foo') - self.assertEqual(msg.get_payload(), '0') + self.assertEqual(msg.get_payload(), '0\n') self.assertRaises(KeyError, lambda: self._box['foo']) self._box.discard(key0) self.assertRaises(KeyError, lambda: self._box[key0]) @@ -259,7 +259,7 @@ class TestMailbox(TestBase): msg0 = self._box.get_message(key0) self.assertIsInstance(msg0, mailbox.Message) self.assertEqual(msg0['from'], 'foo') - self.assertEqual(msg0.get_payload(), '0') + self.assertEqual(msg0.get_payload(), '0\n') self._check_sample(self._box.get_message(key1)) def test_get_bytes(self): @@ -432,15 +432,15 @@ class TestMailbox(TestBase): self.assertIn(key0, self._box) key1 = self._box.add(self._template % 1) self.assertIn(key1, self._box) - self.assertEqual(self._box.pop(key0).get_payload(), '0') + self.assertEqual(self._box.pop(key0).get_payload(), '0\n') self.assertNotIn(key0, self._box) self.assertIn(key1, self._box) key2 = self._box.add(self._template % 2) self.assertIn(key2, self._box) - self.assertEqual(self._box.pop(key2).get_payload(), '2') + self.assertEqual(self._box.pop(key2).get_payload(), '2\n') self.assertNotIn(key2, self._box) self.assertIn(key1, self._box) - self.assertEqual(self._box.pop(key1).get_payload(), '1') + self.assertEqual(self._box.pop(key1).get_payload(), '1\n') self.assertNotIn(key1, self._box) self.assertEqual(len(self._box), 0) @@ -635,7 +635,7 @@ class TestMaildir(TestMailbox, unittest.TestCase): msg_returned = self._box.get_message(key) self.assertEqual(msg_returned.get_subdir(), 'new') self.assertEqual(msg_returned.get_flags(), '') - self.assertEqual(msg_returned.get_payload(), '1') + self.assertEqual(msg_returned.get_payload(), '1\n') msg2 = mailbox.MaildirMessage(self._template % 2) msg2.set_info('2,S') self._box[key] = msg2 @@ -643,7 +643,7 @@ class TestMaildir(TestMailbox, unittest.TestCase): msg_returned = self._box.get_message(key) self.assertEqual(msg_returned.get_subdir(), 'new') self.assertEqual(msg_returned.get_flags(), 'S') - self.assertEqual(msg_returned.get_payload(), '3') + self.assertEqual(msg_returned.get_payload(), '3\n') def test_consistent_factory(self): # Add a message. @@ -996,20 +996,20 @@ class _TestMboxMMDF(_TestSingleFile): def test_add_from_string(self): # Add a string starting with 'From ' to the mailbox - key = self._box.add('From foo@bar blah\nFrom: foo\n\n0') + key = self._box.add('From foo@bar blah\nFrom: foo\n\n0\n') self.assertEqual(self._box[key].get_from(), 'foo@bar blah') - self.assertEqual(self._box[key].get_payload(), '0') + self.assertEqual(self._box[key].get_payload(), '0\n') def test_add_from_bytes(self): # Add a byte string starting with 'From ' to the mailbox - key = self._box.add(b'From foo@bar blah\nFrom: foo\n\n0') + key = self._box.add(b'From foo@bar blah\nFrom: foo\n\n0\n') self.assertEqual(self._box[key].get_from(), 'foo@bar blah') - self.assertEqual(self._box[key].get_payload(), '0') + self.assertEqual(self._box[key].get_payload(), '0\n') def test_add_mbox_or_mmdf_message(self): # Add an mboxMessage or MMDFMessage for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): - msg = class_('From foo@bar blah\nFrom: foo\n\n0') + msg = class_('From foo@bar blah\nFrom: foo\n\n0\n') key = self._box.add(msg) def test_open_close_open(self): @@ -1116,6 +1116,29 @@ class TestMbox(_TestMboxMMDF, unittest.TestCase): perms = st.st_mode self.assertFalse((perms & 0o111)) # Execute bits should all be off. + def test_terminating_newline(self): + message = email.message.Message() + message['From'] = 'john@example.com' + message.set_payload('No newline at the end') + i = self._box.add(message) + + # A newline should have been appended to the payload + message = self._box.get(i) + self.assertEqual(message.get_payload(), 'No newline at the end\n') + + def test_message_separator(self): + # Check there's always a single blank line after each message + self._box.add('From: foo\n\n0') # No newline at the end + with open(self._path) as f: + data = f.read() + self.assertEqual(data[-3:], '0\n\n') + + self._box.add('From: foo\n\n0\n') # Newline at the end + with open(self._path) as f: + data = f.read() + self.assertEqual(data[-3:], '0\n\n') + + class TestMMDF(_TestMboxMMDF, unittest.TestCase): _factory = lambda self, path, factory=None: mailbox.MMDF(path, factory) |