Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 1 | import os |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2 | import sys |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 3 | import time |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4 | import stat |
| 5 | import socket |
| 6 | import email |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 7 | import email.message |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8 | import re |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 9 | import io |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import support |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11 | import unittest |
| 12 | import mailbox |
| 13 | import glob |
Guido van Rossum | 75ebb29 | 2001-04-10 15:01:20 +0000 | [diff] [blame] | 14 | try: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15 | import fcntl |
| 16 | except ImportError: |
Guido van Rossum | 75ebb29 | 2001-04-10 15:01:20 +0000 | [diff] [blame] | 17 | pass |
| 18 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | |
| 20 | class TestBase(unittest.TestCase): |
| 21 | |
| 22 | def _check_sample(self, msg): |
| 23 | # Inspect a mailbox.Message representation of the sample message |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 24 | self.assertIsInstance(msg, email.message.Message) |
| 25 | self.assertIsInstance(msg, mailbox.Message) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 26 | for key, value in _sample_headers.items(): |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 27 | self.assertIn(value, msg.get_all(key)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 28 | self.assertTrue(msg.is_multipart()) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 29 | self.assertEqual(len(msg.get_payload()), len(_sample_payloads)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 30 | for i, payload in enumerate(_sample_payloads): |
| 31 | part = msg.get_payload(i) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 32 | self.assertIsInstance(part, email.message.Message) |
| 33 | self.assertNotIsInstance(part, mailbox.Message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 34 | self.assertEqual(part.get_payload(), payload) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 35 | |
| 36 | def _delete_recursively(self, target): |
| 37 | # Delete a file or delete a directory recursively |
| 38 | if os.path.isdir(target): |
| 39 | for path, dirs, files in os.walk(target, topdown=False): |
| 40 | for name in files: |
| 41 | os.remove(os.path.join(path, name)) |
| 42 | for name in dirs: |
| 43 | os.rmdir(os.path.join(path, name)) |
| 44 | os.rmdir(target) |
| 45 | elif os.path.exists(target): |
| 46 | os.remove(target) |
| 47 | |
| 48 | |
| 49 | class TestMailbox(TestBase): |
| 50 | |
| 51 | _factory = None # Overridden by subclasses to reuse tests |
| 52 | _template = 'From: foo\n\n%s' |
| 53 | |
| 54 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 55 | self._path = support.TESTFN |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 56 | self._delete_recursively(self._path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 57 | self._box = self._factory(self._path) |
| 58 | |
| 59 | def tearDown(self): |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 60 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 61 | self._delete_recursively(self._path) |
| 62 | |
| 63 | def test_add(self): |
| 64 | # Add copies of a sample message |
| 65 | keys = [] |
| 66 | keys.append(self._box.add(self._template % 0)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 67 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 68 | keys.append(self._box.add(mailbox.Message(_sample_message))) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 69 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 70 | keys.append(self._box.add(email.message_from_string(_sample_message))) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 71 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 72 | keys.append(self._box.add(io.StringIO(_sample_message))) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 73 | self.assertEqual(len(self._box), 4) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | keys.append(self._box.add(_sample_message)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 75 | self.assertEqual(len(self._box), 5) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 76 | self.assertEqual(self._box.get_string(keys[0]), self._template % 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 77 | for i in (1, 2, 3, 4): |
| 78 | self._check_sample(self._box[keys[i]]) |
| 79 | |
| 80 | def test_remove(self): |
| 81 | # Remove messages using remove() |
| 82 | self._test_remove_or_delitem(self._box.remove) |
| 83 | |
| 84 | def test_delitem(self): |
| 85 | # Remove messages using __delitem__() |
| 86 | self._test_remove_or_delitem(self._box.__delitem__) |
| 87 | |
| 88 | def _test_remove_or_delitem(self, method): |
| 89 | # (Used by test_remove() and test_delitem().) |
| 90 | key0 = self._box.add(self._template % 0) |
| 91 | key1 = self._box.add(self._template % 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 92 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 93 | method(key0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 94 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 96 | self.assertRaises(KeyError, lambda: method(key0)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 97 | self.assertEqual(self._box.get_string(key1), self._template % 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 98 | key2 = self._box.add(self._template % 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 99 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 100 | method(key2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 101 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 102 | self.assertRaises(KeyError, lambda: self._box[key2]) |
| 103 | self.assertRaises(KeyError, lambda: method(key2)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 104 | self.assertEqual(self._box.get_string(key1), self._template % 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 105 | method(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 106 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 107 | self.assertRaises(KeyError, lambda: self._box[key1]) |
| 108 | self.assertRaises(KeyError, lambda: method(key1)) |
| 109 | |
| 110 | def test_discard(self, repetitions=10): |
| 111 | # Discard messages |
| 112 | key0 = self._box.add(self._template % 0) |
| 113 | key1 = self._box.add(self._template % 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 114 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 115 | self._box.discard(key0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 116 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 117 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 118 | self._box.discard(key0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 119 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 120 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 121 | |
| 122 | def test_get(self): |
| 123 | # Retrieve messages using get() |
| 124 | key0 = self._box.add(self._template % 0) |
| 125 | msg = self._box.get(key0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 126 | self.assertEqual(msg['from'], 'foo') |
| 127 | self.assertEqual(msg.get_payload(), '0') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 128 | self.assertIs(self._box.get('foo'), None) |
| 129 | self.assertIs(self._box.get('foo', False), False) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 130 | self._box.close() |
Georg Brandl | fb16cf1 | 2008-05-11 21:31:44 +0000 | [diff] [blame] | 131 | self._box = self._factory(self._path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 132 | key1 = self._box.add(self._template % 1) |
| 133 | msg = self._box.get(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 134 | self.assertEqual(msg['from'], 'foo') |
Georg Brandl | fb16cf1 | 2008-05-11 21:31:44 +0000 | [diff] [blame] | 135 | self.assertEqual(msg.get_payload(), '1') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 136 | |
| 137 | def test_getitem(self): |
| 138 | # Retrieve message using __getitem__() |
| 139 | key0 = self._box.add(self._template % 0) |
| 140 | msg = self._box[key0] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 141 | self.assertEqual(msg['from'], 'foo') |
| 142 | self.assertEqual(msg.get_payload(), '0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 143 | self.assertRaises(KeyError, lambda: self._box['foo']) |
| 144 | self._box.discard(key0) |
| 145 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 146 | |
| 147 | def test_get_message(self): |
| 148 | # Get Message representations of messages |
| 149 | key0 = self._box.add(self._template % 0) |
| 150 | key1 = self._box.add(_sample_message) |
| 151 | msg0 = self._box.get_message(key0) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 152 | self.assertIsInstance(msg0, mailbox.Message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 153 | self.assertEqual(msg0['from'], 'foo') |
| 154 | self.assertEqual(msg0.get_payload(), '0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 155 | self._check_sample(self._box.get_message(key1)) |
| 156 | |
| 157 | def test_get_string(self): |
| 158 | # Get string representations of messages |
| 159 | key0 = self._box.add(self._template % 0) |
| 160 | key1 = self._box.add(_sample_message) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 161 | self.assertEqual(self._box.get_string(key0), self._template % 0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 162 | self.assertEqual(self._box.get_string(key1), _sample_message) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 163 | |
| 164 | def test_get_file(self): |
| 165 | # Get file representations of messages |
| 166 | key0 = self._box.add(self._template % 0) |
| 167 | key1 = self._box.add(_sample_message) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 168 | data0 = self._box.get_file(key0).read() |
| 169 | data1 = self._box.get_file(key1).read() |
| 170 | self.assertEqual(data0.replace(os.linesep, '\n'), |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 171 | self._template % 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 172 | self.assertEqual(data1.replace(os.linesep, '\n'), |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 173 | _sample_message) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 174 | |
| 175 | def test_iterkeys(self): |
| 176 | # Get keys using iterkeys() |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 177 | self._check_iteration(self._box.keys, do_keys=True, do_values=False) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 178 | |
| 179 | def test_keys(self): |
| 180 | # Get keys using keys() |
| 181 | self._check_iteration(self._box.keys, do_keys=True, do_values=False) |
| 182 | |
| 183 | def test_itervalues(self): |
| 184 | # Get values using itervalues() |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 185 | self._check_iteration(self._box.values, do_keys=False, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 186 | do_values=True) |
| 187 | |
| 188 | def test_iter(self): |
| 189 | # Get values using __iter__() |
| 190 | self._check_iteration(self._box.__iter__, do_keys=False, |
| 191 | do_values=True) |
| 192 | |
| 193 | def test_values(self): |
| 194 | # Get values using values() |
| 195 | self._check_iteration(self._box.values, do_keys=False, do_values=True) |
| 196 | |
| 197 | def test_iteritems(self): |
| 198 | # Get keys and values using iteritems() |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 199 | self._check_iteration(self._box.items, do_keys=True, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | do_values=True) |
| 201 | |
| 202 | def test_items(self): |
| 203 | # Get keys and values using items() |
| 204 | self._check_iteration(self._box.items, do_keys=True, do_values=True) |
| 205 | |
| 206 | def _check_iteration(self, method, do_keys, do_values, repetitions=10): |
| 207 | for value in method(): |
| 208 | self.fail("Not empty") |
| 209 | keys, values = [], [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 210 | for i in range(repetitions): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 211 | keys.append(self._box.add(self._template % i)) |
| 212 | values.append(self._template % i) |
| 213 | if do_keys and not do_values: |
| 214 | returned_keys = list(method()) |
| 215 | elif do_values and not do_keys: |
| 216 | returned_values = list(method()) |
| 217 | else: |
| 218 | returned_keys, returned_values = [], [] |
| 219 | for key, value in method(): |
| 220 | returned_keys.append(key) |
| 221 | returned_values.append(value) |
| 222 | if do_keys: |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 223 | self.assertEqual(len(keys), len(returned_keys)) |
| 224 | self.assertEqual(set(keys), set(returned_keys)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | if do_values: |
| 226 | count = 0 |
| 227 | for value in returned_values: |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 228 | self.assertEqual(value['from'], 'foo') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 229 | self.assertLess(int(value.get_payload()), repetitions) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 230 | count += 1 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 231 | self.assertEqual(len(values), count) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 232 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 233 | def test_contains(self): |
| 234 | # Check existence of keys using __contains__() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 235 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 236 | key0 = self._box.add(self._template % 0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 237 | self.assertIn(key0, self._box) |
| 238 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 239 | key1 = self._box.add(self._template % 1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 240 | self.assertIn(key1, self._box) |
| 241 | self.assertIn(key0, self._box) |
| 242 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | self._box.remove(key0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 244 | self.assertNotIn(key0, self._box) |
| 245 | self.assertIn(key1, self._box) |
| 246 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 247 | self._box.remove(key1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 248 | self.assertNotIn(key1, self._box) |
| 249 | self.assertNotIn(key0, self._box) |
| 250 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 251 | |
| 252 | def test_len(self, repetitions=10): |
| 253 | # Get message count |
| 254 | keys = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 255 | for i in range(repetitions): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 256 | self.assertEqual(len(self._box), i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | keys.append(self._box.add(self._template % i)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 258 | self.assertEqual(len(self._box), i + 1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 259 | for i in range(repetitions): |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 260 | self.assertEqual(len(self._box), repetitions - i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 261 | self._box.remove(keys[i]) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 262 | self.assertEqual(len(self._box), repetitions - i - 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 263 | |
| 264 | def test_set_item(self): |
| 265 | # Modify messages using __setitem__() |
| 266 | key0 = self._box.add(self._template % 'original 0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 267 | self.assertEqual(self._box.get_string(key0), |
| 268 | self._template % 'original 0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | key1 = self._box.add(self._template % 'original 1') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 270 | self.assertEqual(self._box.get_string(key1), |
| 271 | self._template % 'original 1') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 272 | self._box[key0] = self._template % 'changed 0' |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 273 | self.assertEqual(self._box.get_string(key0), |
| 274 | self._template % 'changed 0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 275 | self._box[key1] = self._template % 'changed 1' |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 276 | self.assertEqual(self._box.get_string(key1), |
| 277 | self._template % 'changed 1') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 278 | self._box[key0] = _sample_message |
| 279 | self._check_sample(self._box[key0]) |
| 280 | self._box[key1] = self._box[key0] |
| 281 | self._check_sample(self._box[key1]) |
| 282 | self._box[key0] = self._template % 'original 0' |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 283 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 284 | self._template % 'original 0') |
| 285 | self._check_sample(self._box[key1]) |
| 286 | self.assertRaises(KeyError, |
| 287 | lambda: self._box.__setitem__('foo', 'bar')) |
| 288 | self.assertRaises(KeyError, lambda: self._box['foo']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 289 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 290 | |
| 291 | def test_clear(self, iterations=10): |
| 292 | # Remove all messages using clear() |
| 293 | keys = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 294 | for i in range(iterations): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 295 | self._box.add(self._template % i) |
| 296 | for i, key in enumerate(keys): |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 297 | self.assertEqual(self._box.get_string(key), self._template % i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 298 | self._box.clear() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 299 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 300 | for i, key in enumerate(keys): |
| 301 | self.assertRaises(KeyError, lambda: self._box.get_string(key)) |
| 302 | |
| 303 | def test_pop(self): |
| 304 | # Get and remove a message using pop() |
| 305 | key0 = self._box.add(self._template % 0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 306 | self.assertIn(key0, self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 307 | key1 = self._box.add(self._template % 1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 308 | self.assertIn(key1, self._box) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 309 | self.assertEqual(self._box.pop(key0).get_payload(), '0') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 310 | self.assertNotIn(key0, self._box) |
| 311 | self.assertIn(key1, self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 312 | key2 = self._box.add(self._template % 2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 313 | self.assertIn(key2, self._box) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 314 | self.assertEqual(self._box.pop(key2).get_payload(), '2') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 315 | self.assertNotIn(key2, self._box) |
| 316 | self.assertIn(key1, self._box) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 317 | self.assertEqual(self._box.pop(key1).get_payload(), '1') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 318 | self.assertNotIn(key1, self._box) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 319 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 320 | |
| 321 | def test_popitem(self, iterations=10): |
| 322 | # Get and remove an arbitrary (key, message) using popitem() |
| 323 | keys = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 324 | for i in range(10): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | keys.append(self._box.add(self._template % i)) |
| 326 | seen = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 327 | for i in range(10): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 328 | key, msg = self._box.popitem() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 329 | self.assertIn(key, keys) |
| 330 | self.assertNotIn(key, seen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | seen.append(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 332 | self.assertEqual(int(msg.get_payload()), keys.index(key)) |
| 333 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 334 | for key in keys: |
| 335 | self.assertRaises(KeyError, lambda: self._box[key]) |
| 336 | |
| 337 | def test_update(self): |
| 338 | # Modify multiple messages using update() |
| 339 | key0 = self._box.add(self._template % 'original 0') |
| 340 | key1 = self._box.add(self._template % 'original 1') |
| 341 | key2 = self._box.add(self._template % 'original 2') |
| 342 | self._box.update({key0: self._template % 'changed 0', |
| 343 | key2: _sample_message}) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 344 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 345 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 346 | self._template % 'changed 0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 347 | self.assertEqual(self._box.get_string(key1), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 348 | self._template % 'original 1') |
| 349 | self._check_sample(self._box[key2]) |
| 350 | self._box.update([(key2, self._template % 'changed 2'), |
| 351 | (key1, self._template % 'changed 1'), |
| 352 | (key0, self._template % 'original 0')]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 353 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 354 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 355 | self._template % 'original 0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 356 | self.assertEqual(self._box.get_string(key1), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 357 | self._template % 'changed 1') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 358 | self.assertEqual(self._box.get_string(key2), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 359 | self._template % 'changed 2') |
| 360 | self.assertRaises(KeyError, |
| 361 | lambda: self._box.update({'foo': 'bar', |
| 362 | key0: self._template % "changed 0"})) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 363 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 364 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 365 | self._template % "changed 0") |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 366 | self.assertEqual(self._box.get_string(key1), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 367 | self._template % "changed 1") |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 368 | self.assertEqual(self._box.get_string(key2), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 369 | self._template % "changed 2") |
| 370 | |
| 371 | def test_flush(self): |
| 372 | # Write changes to disk |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 373 | self._test_flush_or_close(self._box.flush, True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 374 | |
| 375 | def test_lock_unlock(self): |
| 376 | # Lock and unlock the mailbox |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 377 | self.assertFalse(os.path.exists(self._get_lock_path())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 378 | self._box.lock() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 379 | self.assertTrue(os.path.exists(self._get_lock_path())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 380 | self._box.unlock() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 381 | self.assertFalse(os.path.exists(self._get_lock_path())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 382 | |
| 383 | def test_close(self): |
| 384 | # Close mailbox and flush changes to disk |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 385 | self._test_flush_or_close(self._box.close, False) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 386 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 387 | def _test_flush_or_close(self, method, should_call_close): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 388 | contents = [self._template % i for i in range(3)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 389 | self._box.add(contents[0]) |
| 390 | self._box.add(contents[1]) |
| 391 | self._box.add(contents[2]) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 392 | oldbox = self._box |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 393 | method() |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 394 | if should_call_close: |
| 395 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 396 | self._box = self._factory(self._path) |
| 397 | keys = self._box.keys() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 398 | self.assertEqual(len(keys), 3) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 399 | for key in keys: |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 400 | self.assertIn(self._box.get_string(key), contents) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 401 | oldbox.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 402 | |
| 403 | def test_dump_message(self): |
| 404 | # Write message representations to disk |
| 405 | for input in (email.message_from_string(_sample_message), |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 406 | _sample_message, io.StringIO(_sample_message)): |
| 407 | output = io.StringIO() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 408 | self._box._dump_message(input, output) |
Amaury Forgeot d'Arc | 9edef04 | 2008-03-26 19:49:26 +0000 | [diff] [blame] | 409 | self.assertEqual(output.getvalue(), _sample_message) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 410 | output = io.StringIO() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 411 | self.assertRaises(TypeError, |
| 412 | lambda: self._box._dump_message(None, output)) |
| 413 | |
| 414 | def _get_lock_path(self): |
| 415 | # Return the path of the dot lock file. May be overridden. |
| 416 | return self._path + '.lock' |
| 417 | |
| 418 | |
| 419 | class TestMailboxSuperclass(TestBase): |
| 420 | |
| 421 | def test_notimplemented(self): |
| 422 | # Test that all Mailbox methods raise NotImplementedException. |
| 423 | box = mailbox.Mailbox('path') |
| 424 | self.assertRaises(NotImplementedError, lambda: box.add('')) |
| 425 | self.assertRaises(NotImplementedError, lambda: box.remove('')) |
| 426 | self.assertRaises(NotImplementedError, lambda: box.__delitem__('')) |
| 427 | self.assertRaises(NotImplementedError, lambda: box.discard('')) |
| 428 | self.assertRaises(NotImplementedError, lambda: box.__setitem__('', '')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 429 | self.assertRaises(NotImplementedError, lambda: box.keys()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 430 | self.assertRaises(NotImplementedError, lambda: box.keys()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 431 | self.assertRaises(NotImplementedError, lambda: box.values().__next__()) |
| 432 | self.assertRaises(NotImplementedError, lambda: box.__iter__().__next__()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 433 | self.assertRaises(NotImplementedError, lambda: box.values()) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 434 | self.assertRaises(NotImplementedError, lambda: box.items().next()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 435 | self.assertRaises(NotImplementedError, lambda: box.items()) |
| 436 | self.assertRaises(NotImplementedError, lambda: box.get('')) |
| 437 | self.assertRaises(NotImplementedError, lambda: box.__getitem__('')) |
| 438 | self.assertRaises(NotImplementedError, lambda: box.get_message('')) |
| 439 | self.assertRaises(NotImplementedError, lambda: box.get_string('')) |
| 440 | self.assertRaises(NotImplementedError, lambda: box.get_file('')) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 441 | self.assertRaises(NotImplementedError, lambda: '' in box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 442 | self.assertRaises(NotImplementedError, lambda: box.__contains__('')) |
| 443 | self.assertRaises(NotImplementedError, lambda: box.__len__()) |
| 444 | self.assertRaises(NotImplementedError, lambda: box.clear()) |
| 445 | self.assertRaises(NotImplementedError, lambda: box.pop('')) |
| 446 | self.assertRaises(NotImplementedError, lambda: box.popitem()) |
| 447 | self.assertRaises(NotImplementedError, lambda: box.update((('', ''),))) |
| 448 | self.assertRaises(NotImplementedError, lambda: box.flush()) |
| 449 | self.assertRaises(NotImplementedError, lambda: box.lock()) |
| 450 | self.assertRaises(NotImplementedError, lambda: box.unlock()) |
| 451 | self.assertRaises(NotImplementedError, lambda: box.close()) |
| 452 | |
| 453 | |
| 454 | class TestMaildir(TestMailbox): |
| 455 | |
| 456 | _factory = lambda self, path, factory=None: mailbox.Maildir(path, factory) |
| 457 | |
| 458 | def setUp(self): |
| 459 | TestMailbox.setUp(self) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 460 | if os.name in ('nt', 'os2') or sys.platform == 'cygwin': |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 461 | self._box.colon = '!' |
| 462 | |
| 463 | def test_add_MM(self): |
| 464 | # Add a MaildirMessage instance |
| 465 | msg = mailbox.MaildirMessage(self._template % 0) |
| 466 | msg.set_subdir('cur') |
| 467 | msg.set_info('foo') |
| 468 | key = self._box.add(msg) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 469 | self.assertTrue(os.path.exists(os.path.join(self._path, 'cur', '%s%sfoo' % |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 470 | (key, self._box.colon)))) |
| 471 | |
| 472 | def test_get_MM(self): |
| 473 | # Get a MaildirMessage instance |
| 474 | msg = mailbox.MaildirMessage(self._template % 0) |
| 475 | msg.set_subdir('cur') |
| 476 | msg.set_flags('RF') |
| 477 | key = self._box.add(msg) |
| 478 | msg_returned = self._box.get_message(key) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 479 | self.assertIsInstance(msg_returned, mailbox.MaildirMessage) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 480 | self.assertEqual(msg_returned.get_subdir(), 'cur') |
| 481 | self.assertEqual(msg_returned.get_flags(), 'FR') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 482 | |
| 483 | def test_set_MM(self): |
| 484 | # Set with a MaildirMessage instance |
| 485 | msg0 = mailbox.MaildirMessage(self._template % 0) |
| 486 | msg0.set_flags('TP') |
| 487 | key = self._box.add(msg0) |
| 488 | msg_returned = self._box.get_message(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 489 | self.assertEqual(msg_returned.get_subdir(), 'new') |
| 490 | self.assertEqual(msg_returned.get_flags(), 'PT') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 491 | msg1 = mailbox.MaildirMessage(self._template % 1) |
| 492 | self._box[key] = msg1 |
| 493 | msg_returned = self._box.get_message(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 494 | self.assertEqual(msg_returned.get_subdir(), 'new') |
| 495 | self.assertEqual(msg_returned.get_flags(), '') |
| 496 | self.assertEqual(msg_returned.get_payload(), '1') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 497 | msg2 = mailbox.MaildirMessage(self._template % 2) |
| 498 | msg2.set_info('2,S') |
| 499 | self._box[key] = msg2 |
| 500 | self._box[key] = self._template % 3 |
| 501 | msg_returned = self._box.get_message(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 502 | self.assertEqual(msg_returned.get_subdir(), 'new') |
| 503 | self.assertEqual(msg_returned.get_flags(), 'S') |
| 504 | self.assertEqual(msg_returned.get_payload(), '3') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 505 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 506 | def test_consistent_factory(self): |
| 507 | # Add a message. |
| 508 | msg = mailbox.MaildirMessage(self._template % 0) |
| 509 | msg.set_subdir('cur') |
| 510 | msg.set_flags('RF') |
| 511 | key = self._box.add(msg) |
| 512 | |
| 513 | # Create new mailbox with |
| 514 | class FakeMessage(mailbox.MaildirMessage): |
| 515 | pass |
| 516 | box = mailbox.Maildir(self._path, factory=FakeMessage) |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 517 | box.colon = self._box.colon |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 518 | msg2 = box.get_message(key) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 519 | self.assertIsInstance(msg2, FakeMessage) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 520 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 521 | def test_initialize_new(self): |
| 522 | # Initialize a non-existent mailbox |
| 523 | self.tearDown() |
| 524 | self._box = mailbox.Maildir(self._path) |
Georg Brandl | fb16cf1 | 2008-05-11 21:31:44 +0000 | [diff] [blame] | 525 | self._check_basics() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 526 | self._delete_recursively(self._path) |
| 527 | self._box = self._factory(self._path, factory=None) |
| 528 | self._check_basics() |
| 529 | |
| 530 | def test_initialize_existing(self): |
| 531 | # Initialize an existing mailbox |
| 532 | self.tearDown() |
| 533 | for subdir in '', 'tmp', 'new', 'cur': |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 534 | os.mkdir(os.path.normpath(os.path.join(self._path, subdir))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 535 | self._box = mailbox.Maildir(self._path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 536 | self._check_basics() |
| 537 | |
| 538 | def _check_basics(self, factory=None): |
| 539 | # (Used by test_open_new() and test_open_existing().) |
| 540 | self.assertEqual(self._box._path, os.path.abspath(self._path)) |
| 541 | self.assertEqual(self._box._factory, factory) |
| 542 | for subdir in '', 'tmp', 'new', 'cur': |
| 543 | path = os.path.join(self._path, subdir) |
| 544 | mode = os.stat(path)[stat.ST_MODE] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 545 | self.assertTrue(stat.S_ISDIR(mode), "Not a directory: '%s'" % path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | |
| 547 | def test_list_folders(self): |
| 548 | # List folders |
| 549 | self._box.add_folder('one') |
| 550 | self._box.add_folder('two') |
| 551 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 552 | self.assertEqual(len(self._box.list_folders()), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 553 | self.assertEqual(set(self._box.list_folders()), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 554 | set(('one', 'two', 'three'))) |
| 555 | |
| 556 | def test_get_folder(self): |
| 557 | # Open folders |
| 558 | self._box.add_folder('foo.bar') |
| 559 | folder0 = self._box.get_folder('foo.bar') |
| 560 | folder0.add(self._template % 'bar') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 561 | self.assertTrue(os.path.isdir(os.path.join(self._path, '.foo.bar'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 562 | folder1 = self._box.get_folder('foo.bar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 563 | self.assertEqual(folder1.get_string(folder1.keys()[0]), |
| 564 | self._template % 'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 565 | |
| 566 | def test_add_and_remove_folders(self): |
| 567 | # Delete folders |
| 568 | self._box.add_folder('one') |
| 569 | self._box.add_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 570 | self.assertEqual(len(self._box.list_folders()), 2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 571 | self.assertEqual(set(self._box.list_folders()), set(('one', 'two'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 572 | self._box.remove_folder('one') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 573 | self.assertEqual(len(self._box.list_folders()), 1) |
| 574 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 575 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 576 | self.assertEqual(len(self._box.list_folders()), 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 577 | self.assertEqual(set(self._box.list_folders()), set(('two', 'three'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 578 | self._box.remove_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 579 | self.assertEqual(len(self._box.list_folders()), 1) |
| 580 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 581 | self._box.remove_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 582 | self.assertEqual(len(self._box.list_folders()), 0) |
| 583 | self.assertEqual(self._box.list_folders(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 584 | |
| 585 | def test_clean(self): |
| 586 | # Remove old files from 'tmp' |
| 587 | foo_path = os.path.join(self._path, 'tmp', 'foo') |
| 588 | bar_path = os.path.join(self._path, 'tmp', 'bar') |
| 589 | f = open(foo_path, 'w') |
| 590 | f.write("@") |
| 591 | f.close() |
| 592 | f = open(bar_path, 'w') |
| 593 | f.write("@") |
| 594 | f.close() |
| 595 | self._box.clean() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 596 | self.assertTrue(os.path.exists(foo_path)) |
| 597 | self.assertTrue(os.path.exists(bar_path)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 598 | foo_stat = os.stat(foo_path) |
| 599 | os.utime(foo_path, (time.time() - 129600 - 2, |
| 600 | foo_stat.st_mtime)) |
| 601 | self._box.clean() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 602 | self.assertFalse(os.path.exists(foo_path)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 603 | self.assertTrue(os.path.exists(bar_path)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 604 | |
| 605 | def test_create_tmp(self, repetitions=10): |
| 606 | # Create files in tmp directory |
| 607 | hostname = socket.gethostname() |
| 608 | if '/' in hostname: |
| 609 | hostname = hostname.replace('/', r'\057') |
| 610 | if ':' in hostname: |
| 611 | hostname = hostname.replace(':', r'\072') |
| 612 | pid = os.getpid() |
| 613 | pattern = re.compile(r"(?P<time>\d+)\.M(?P<M>\d{1,6})P(?P<P>\d+)" |
| 614 | r"Q(?P<Q>\d+)\.(?P<host>[^:/]+)") |
| 615 | previous_groups = None |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 616 | for x in range(repetitions): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 617 | tmp_file = self._box._create_tmp() |
| 618 | head, tail = os.path.split(tmp_file.name) |
| 619 | self.assertEqual(head, os.path.abspath(os.path.join(self._path, |
| 620 | "tmp")), |
| 621 | "File in wrong location: '%s'" % head) |
| 622 | match = pattern.match(tail) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 623 | self.assertIsNot(match, None, "Invalid file name: '%s'" % tail) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 624 | groups = match.groups() |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 625 | if previous_groups is not None: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 626 | self.assertTrue(int(groups[0] >= previous_groups[0]), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 627 | "Non-monotonic seconds: '%s' before '%s'" % |
| 628 | (previous_groups[0], groups[0])) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 629 | self.assertTrue(int(groups[1] >= previous_groups[1]) or |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 630 | groups[0] != groups[1], |
| 631 | "Non-monotonic milliseconds: '%s' before '%s'" % |
| 632 | (previous_groups[1], groups[1])) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 633 | self.assertEqual(int(groups[2]), pid, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 634 | "Process ID mismatch: '%s' should be '%s'" % |
| 635 | (groups[2], pid)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 636 | self.assertEqual(int(groups[3]), int(previous_groups[3]) + 1, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 637 | "Non-sequential counter: '%s' before '%s'" % |
| 638 | (previous_groups[3], groups[3])) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 639 | self.assertEqual(groups[4], hostname, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 640 | "Host name mismatch: '%s' should be '%s'" % |
| 641 | (groups[4], hostname)) |
| 642 | previous_groups = groups |
| 643 | tmp_file.write(_sample_message) |
| 644 | tmp_file.seek(0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 645 | self.assertEqual(tmp_file.read(), _sample_message) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 646 | tmp_file.close() |
| 647 | file_count = len(os.listdir(os.path.join(self._path, "tmp"))) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 648 | self.assertEqual(file_count, repetitions, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 649 | "Wrong file count: '%s' should be '%s'" % |
| 650 | (file_count, repetitions)) |
| 651 | |
| 652 | def test_refresh(self): |
| 653 | # Update the table of contents |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 654 | self.assertEqual(self._box._toc, {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 655 | key0 = self._box.add(self._template % 0) |
| 656 | key1 = self._box.add(self._template % 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 657 | self.assertEqual(self._box._toc, {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 658 | self._box._refresh() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 659 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0), |
| 660 | key1: os.path.join('new', key1)}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 661 | key2 = self._box.add(self._template % 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 662 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0), |
| 663 | key1: os.path.join('new', key1)}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 664 | self._box._refresh() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 665 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0), |
| 666 | key1: os.path.join('new', key1), |
| 667 | key2: os.path.join('new', key2)}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 668 | |
| 669 | def test_lookup(self): |
| 670 | # Look up message subpaths in the TOC |
| 671 | self.assertRaises(KeyError, lambda: self._box._lookup('foo')) |
| 672 | key0 = self._box.add(self._template % 0) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 673 | self.assertEqual(self._box._lookup(key0), os.path.join('new', key0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 674 | os.remove(os.path.join(self._path, 'new', key0)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 675 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0)}) |
Antoine Pitrou | 03f1344 | 2009-11-02 11:36:51 +0000 | [diff] [blame] | 676 | # Be sure that the TOC is read back from disk (see issue #6896 |
| 677 | # about bad mtime behaviour on some systems). |
| 678 | self._box.flush() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 679 | self.assertRaises(KeyError, lambda: self._box._lookup(key0)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 680 | self.assertEqual(self._box._toc, {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 681 | |
| 682 | def test_lock_unlock(self): |
| 683 | # Lock and unlock the mailbox. For Maildir, this does nothing. |
| 684 | self._box.lock() |
| 685 | self._box.unlock() |
| 686 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 687 | def test_folder (self): |
| 688 | # Test for bug #1569790: verify that folders returned by .get_folder() |
| 689 | # use the same factory function. |
| 690 | def dummy_factory (s): |
| 691 | return None |
| 692 | box = self._factory(self._path, factory=dummy_factory) |
| 693 | folder = box.add_folder('folder1') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 694 | self.assertIs(folder._factory, dummy_factory) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 695 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 696 | folder1_alias = box.get_folder('folder1') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 697 | self.assertIs(folder1_alias._factory, dummy_factory) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 699 | def test_directory_in_folder (self): |
| 700 | # Test that mailboxes still work if there's a stray extra directory |
| 701 | # in a folder. |
| 702 | for i in range(10): |
| 703 | self._box.add(mailbox.Message(_sample_message)) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 704 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 705 | # Create a stray directory |
| 706 | os.mkdir(os.path.join(self._path, 'cur', 'stray-dir')) |
| 707 | |
| 708 | # Check that looping still works with the directory present. |
| 709 | for msg in self._box: |
| 710 | pass |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 711 | |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 712 | def test_file_permissions(self): |
| 713 | # Verify that message files are created without execute permissions |
| 714 | if not hasattr(os, "stat") or not hasattr(os, "umask"): |
| 715 | return |
| 716 | msg = mailbox.MaildirMessage(self._template % 0) |
| 717 | orig_umask = os.umask(0) |
| 718 | try: |
| 719 | key = self._box.add(msg) |
| 720 | finally: |
| 721 | os.umask(orig_umask) |
| 722 | path = os.path.join(self._path, self._box._lookup(key)) |
| 723 | mode = os.stat(path).st_mode |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 724 | self.assertFalse(mode & 0o111) |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 725 | |
| 726 | def test_folder_file_perms(self): |
| 727 | # From bug #3228, we want to verify that the file created inside a Maildir |
| 728 | # subfolder isn't marked as executable. |
| 729 | if not hasattr(os, "stat") or not hasattr(os, "umask"): |
| 730 | return |
| 731 | |
| 732 | orig_umask = os.umask(0) |
| 733 | try: |
| 734 | subfolder = self._box.add_folder('subfolder') |
| 735 | finally: |
| 736 | os.umask(orig_umask) |
| 737 | |
| 738 | path = os.path.join(subfolder._path, 'maildirfolder') |
| 739 | st = os.stat(path) |
| 740 | perms = st.st_mode |
| 741 | self.assertFalse((perms & 0o111)) # Execute bits should all be off. |
| 742 | |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 743 | def test_reread(self): |
| 744 | # Wait for 2 seconds |
| 745 | time.sleep(2) |
| 746 | |
| 747 | # Initially, the mailbox has not been read and the time is null. |
| 748 | assert getattr(self._box, '_last_read', None) is None |
| 749 | |
| 750 | # Refresh mailbox; the times should now be set to something. |
| 751 | self._box._refresh() |
| 752 | assert getattr(self._box, '_last_read', None) is not None |
| 753 | |
| 754 | # Try calling _refresh() again; the modification times shouldn't have |
| 755 | # changed, so the mailbox should not be re-reading. Re-reading causes |
| 756 | # the ._toc attribute to be assigned a new dictionary object, so |
| 757 | # we'll check that the ._toc attribute isn't a different object. |
| 758 | orig_toc = self._box._toc |
| 759 | def refreshed(): |
| 760 | return self._box._toc is not orig_toc |
| 761 | |
| 762 | time.sleep(1) # Wait 1sec to ensure time.time()'s value changes |
| 763 | self._box._refresh() |
| 764 | assert not refreshed() |
| 765 | |
| 766 | # Now, write something into cur and remove it. This changes |
| 767 | # the mtime and should cause a re-read. |
| 768 | filename = os.path.join(self._path, 'cur', 'stray-file') |
| 769 | f = open(filename, 'w') |
| 770 | f.close() |
| 771 | os.unlink(filename) |
| 772 | self._box._refresh() |
| 773 | assert refreshed() |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 774 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 775 | class _TestMboxMMDF(TestMailbox): |
| 776 | |
| 777 | def tearDown(self): |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 778 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 779 | self._delete_recursively(self._path) |
| 780 | for lock_remnant in glob.glob(self._path + '.*'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 781 | support.unlink(lock_remnant) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 782 | |
| 783 | def test_add_from_string(self): |
| 784 | # Add a string starting with 'From ' to the mailbox |
| 785 | key = self._box.add('From foo@bar blah\nFrom: foo\n\n0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 786 | self.assertEqual(self._box[key].get_from(), 'foo@bar blah') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 787 | self.assertEqual(self._box[key].get_payload(), '0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 788 | |
| 789 | def test_add_mbox_or_mmdf_message(self): |
| 790 | # Add an mboxMessage or MMDFMessage |
| 791 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 792 | msg = class_('From foo@bar blah\nFrom: foo\n\n0') |
| 793 | key = self._box.add(msg) |
| 794 | |
| 795 | def test_open_close_open(self): |
| 796 | # Open and inspect previously-created mailbox |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 797 | values = [self._template % i for i in range(3)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 798 | for value in values: |
| 799 | self._box.add(value) |
| 800 | self._box.close() |
| 801 | mtime = os.path.getmtime(self._path) |
| 802 | self._box = self._factory(self._path) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 803 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 804 | for key in self._box.keys(): |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 805 | self.assertIn(self._box.get_string(key), values) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 806 | self._box.close() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 807 | self.assertEqual(mtime, os.path.getmtime(self._path)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 808 | |
| 809 | def test_add_and_close(self): |
| 810 | # Verifying that closing a mailbox doesn't change added items |
| 811 | self._box.add(_sample_message) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 812 | for i in range(3): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 813 | self._box.add(self._template % i) |
| 814 | self._box.add(_sample_message) |
| 815 | self._box._file.flush() |
| 816 | self._box._file.seek(0) |
| 817 | contents = self._box._file.read() |
| 818 | self._box.close() |
Amaury Forgeot d'Arc | 9edef04 | 2008-03-26 19:49:26 +0000 | [diff] [blame] | 819 | self.assertEqual(contents, open(self._path, 'r', newline='').read()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 820 | self._box = self._factory(self._path) |
| 821 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 822 | def test_lock_conflict(self): |
| 823 | # Fork off a subprocess that will lock the file for 2 seconds, |
| 824 | # unlock it, and then exit. |
| 825 | if not hasattr(os, 'fork'): |
| 826 | return |
| 827 | pid = os.fork() |
| 828 | if pid == 0: |
| 829 | # In the child, lock the mailbox. |
Guido van Rossum | 62cbc8a | 2007-04-09 00:41:25 +0000 | [diff] [blame] | 830 | try: |
| 831 | self._box.lock() |
| 832 | time.sleep(2) |
| 833 | self._box.unlock() |
| 834 | finally: |
| 835 | os._exit(0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 836 | |
| 837 | # In the parent, sleep a bit to give the child time to acquire |
| 838 | # the lock. |
| 839 | time.sleep(0.5) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 840 | try: |
| 841 | self.assertRaises(mailbox.ExternalClashError, |
| 842 | self._box.lock) |
| 843 | finally: |
| 844 | # Wait for child to exit. Locking should now succeed. |
| 845 | exited_pid, status = os.waitpid(pid, 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 846 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 847 | self._box.lock() |
| 848 | self._box.unlock() |
| 849 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 850 | def test_relock(self): |
| 851 | # Test case for bug #1575506: the mailbox class was locking the |
| 852 | # wrong file object in its flush() method. |
| 853 | msg = "Subject: sub\n\nbody\n" |
| 854 | key1 = self._box.add(msg) |
| 855 | self._box.flush() |
| 856 | self._box.close() |
| 857 | |
| 858 | self._box = self._factory(self._path) |
| 859 | self._box.lock() |
| 860 | key2 = self._box.add(msg) |
| 861 | self._box.flush() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 862 | self.assertTrue(self._box._locked) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 863 | self._box.close() |
| 864 | |
| 865 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 866 | class TestMbox(_TestMboxMMDF): |
| 867 | |
| 868 | _factory = lambda self, path, factory=None: mailbox.mbox(path, factory) |
| 869 | |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 870 | def test_file_perms(self): |
| 871 | # From bug #3228, we want to verify that the mailbox file isn't executable, |
| 872 | # even if the umask is set to something that would leave executable bits set. |
| 873 | # We only run this test on platforms that support umask. |
| 874 | if hasattr(os, 'umask') and hasattr(os, 'stat'): |
| 875 | try: |
| 876 | old_umask = os.umask(0o077) |
| 877 | self._box.close() |
| 878 | os.unlink(self._path) |
| 879 | self._box = mailbox.mbox(self._path, create=True) |
| 880 | self._box.add('') |
| 881 | self._box.close() |
| 882 | finally: |
| 883 | os.umask(old_umask) |
| 884 | |
| 885 | st = os.stat(self._path) |
| 886 | perms = st.st_mode |
| 887 | self.assertFalse((perms & 0o111)) # Execute bits should all be off. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 888 | |
| 889 | class TestMMDF(_TestMboxMMDF): |
| 890 | |
| 891 | _factory = lambda self, path, factory=None: mailbox.MMDF(path, factory) |
| 892 | |
| 893 | |
| 894 | class TestMH(TestMailbox): |
| 895 | |
| 896 | _factory = lambda self, path, factory=None: mailbox.MH(path, factory) |
| 897 | |
| 898 | def test_list_folders(self): |
| 899 | # List folders |
| 900 | self._box.add_folder('one') |
| 901 | self._box.add_folder('two') |
| 902 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 903 | self.assertEqual(len(self._box.list_folders()), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 904 | self.assertEqual(set(self._box.list_folders()), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 905 | set(('one', 'two', 'three'))) |
| 906 | |
| 907 | def test_get_folder(self): |
| 908 | # Open folders |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 909 | def dummy_factory (s): |
| 910 | return None |
| 911 | self._box = self._factory(self._path, dummy_factory) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 912 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 913 | new_folder = self._box.add_folder('foo.bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 914 | folder0 = self._box.get_folder('foo.bar') |
| 915 | folder0.add(self._template % 'bar') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 916 | self.assertTrue(os.path.isdir(os.path.join(self._path, 'foo.bar'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 917 | folder1 = self._box.get_folder('foo.bar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 918 | self.assertEqual(folder1.get_string(folder1.keys()[0]), |
| 919 | self._template % 'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 920 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 921 | # Test for bug #1569790: verify that folders returned by .get_folder() |
| 922 | # use the same factory function. |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 923 | self.assertIs(new_folder._factory, self._box._factory) |
| 924 | self.assertIs(folder0._factory, self._box._factory) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 925 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 926 | def test_add_and_remove_folders(self): |
| 927 | # Delete folders |
| 928 | self._box.add_folder('one') |
| 929 | self._box.add_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 930 | self.assertEqual(len(self._box.list_folders()), 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 931 | self.assertEqual(set(self._box.list_folders()), set(('one', 'two'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 932 | self._box.remove_folder('one') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 933 | self.assertEqual(len(self._box.list_folders()), 1) |
| 934 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 935 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 936 | self.assertEqual(len(self._box.list_folders()), 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 937 | self.assertEqual(set(self._box.list_folders()), set(('two', 'three'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 938 | self._box.remove_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 939 | self.assertEqual(len(self._box.list_folders()), 1) |
| 940 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 941 | self._box.remove_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 942 | self.assertEqual(len(self._box.list_folders()), 0) |
| 943 | self.assertEqual(self._box.list_folders(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 944 | |
| 945 | def test_sequences(self): |
| 946 | # Get and set sequences |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 947 | self.assertEqual(self._box.get_sequences(), {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 948 | msg0 = mailbox.MHMessage(self._template % 0) |
| 949 | msg0.add_sequence('foo') |
| 950 | key0 = self._box.add(msg0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 951 | self.assertEqual(self._box.get_sequences(), {'foo':[key0]}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 952 | msg1 = mailbox.MHMessage(self._template % 1) |
| 953 | msg1.set_sequences(['bar', 'replied', 'foo']) |
| 954 | key1 = self._box.add(msg1) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 955 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | {'foo':[key0, key1], 'bar':[key1], 'replied':[key1]}) |
| 957 | msg0.set_sequences(['flagged']) |
| 958 | self._box[key0] = msg0 |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 959 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 960 | {'foo':[key1], 'bar':[key1], 'replied':[key1], |
| 961 | 'flagged':[key0]}) |
| 962 | self._box.remove(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 963 | self.assertEqual(self._box.get_sequences(), {'flagged':[key0]}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 964 | |
R. David Murray | 548ac41 | 2009-04-02 19:44:43 +0000 | [diff] [blame] | 965 | def test_issue2625(self): |
| 966 | msg0 = mailbox.MHMessage(self._template % 0) |
| 967 | msg0.add_sequence('foo') |
| 968 | key0 = self._box.add(msg0) |
| 969 | refmsg0 = self._box.get_message(key0) |
| 970 | |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame^] | 971 | def test_issue7627(self): |
| 972 | msg0 = mailbox.MHMessage(self._template % 0) |
| 973 | key0 = self._box.add(msg0) |
| 974 | self._box.lock() |
| 975 | self._box.remove(key0) |
| 976 | self._box.unlock() |
| 977 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 978 | def test_pack(self): |
| 979 | # Pack the contents of the mailbox |
| 980 | msg0 = mailbox.MHMessage(self._template % 0) |
| 981 | msg1 = mailbox.MHMessage(self._template % 1) |
| 982 | msg2 = mailbox.MHMessage(self._template % 2) |
| 983 | msg3 = mailbox.MHMessage(self._template % 3) |
| 984 | msg0.set_sequences(['foo', 'unseen']) |
| 985 | msg1.set_sequences(['foo']) |
| 986 | msg2.set_sequences(['foo', 'flagged']) |
| 987 | msg3.set_sequences(['foo', 'bar', 'replied']) |
| 988 | key0 = self._box.add(msg0) |
| 989 | key1 = self._box.add(msg1) |
| 990 | key2 = self._box.add(msg2) |
| 991 | key3 = self._box.add(msg3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 992 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 993 | {'foo':[key0,key1,key2,key3], 'unseen':[key0], |
| 994 | 'flagged':[key2], 'bar':[key3], 'replied':[key3]}) |
| 995 | self._box.remove(key2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 996 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 997 | {'foo':[key0,key1,key3], 'unseen':[key0], 'bar':[key3], |
| 998 | 'replied':[key3]}) |
| 999 | self._box.pack() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1000 | self.assertEqual(self._box.keys(), [1, 2, 3]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1001 | key0 = key0 |
| 1002 | key1 = key0 + 1 |
| 1003 | key2 = key1 + 1 |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1004 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1005 | {'foo':[1, 2, 3], 'unseen':[1], 'bar':[3], 'replied':[3]}) |
| 1006 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1007 | # Test case for packing while holding the mailbox locked. |
| 1008 | key0 = self._box.add(msg1) |
| 1009 | key1 = self._box.add(msg1) |
| 1010 | key2 = self._box.add(msg1) |
| 1011 | key3 = self._box.add(msg1) |
| 1012 | |
| 1013 | self._box.remove(key0) |
| 1014 | self._box.remove(key2) |
| 1015 | self._box.lock() |
| 1016 | self._box.pack() |
| 1017 | self._box.unlock() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1018 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1019 | {'foo':[1, 2, 3, 4, 5], |
| 1020 | 'unseen':[1], 'bar':[3], 'replied':[3]}) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1021 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1022 | def _get_lock_path(self): |
| 1023 | return os.path.join(self._path, '.mh_sequences.lock') |
| 1024 | |
| 1025 | |
| 1026 | class TestBabyl(TestMailbox): |
| 1027 | |
| 1028 | _factory = lambda self, path, factory=None: mailbox.Babyl(path, factory) |
| 1029 | |
| 1030 | def tearDown(self): |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 1031 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1032 | self._delete_recursively(self._path) |
| 1033 | for lock_remnant in glob.glob(self._path + '.*'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1034 | support.unlink(lock_remnant) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1035 | |
| 1036 | def test_labels(self): |
| 1037 | # Get labels from the mailbox |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1038 | self.assertEqual(self._box.get_labels(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1039 | msg0 = mailbox.BabylMessage(self._template % 0) |
| 1040 | msg0.add_label('foo') |
| 1041 | key0 = self._box.add(msg0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1042 | self.assertEqual(self._box.get_labels(), ['foo']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1043 | msg1 = mailbox.BabylMessage(self._template % 1) |
| 1044 | msg1.set_labels(['bar', 'answered', 'foo']) |
| 1045 | key1 = self._box.add(msg1) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1046 | self.assertEqual(set(self._box.get_labels()), set(['foo', 'bar'])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1047 | msg0.set_labels(['blah', 'filed']) |
| 1048 | self._box[key0] = msg0 |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1049 | self.assertEqual(set(self._box.get_labels()), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1050 | set(['foo', 'bar', 'blah'])) |
| 1051 | self._box.remove(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1052 | self.assertEqual(set(self._box.get_labels()), set(['blah'])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
| 1055 | class TestMessage(TestBase): |
| 1056 | |
| 1057 | _factory = mailbox.Message # Overridden by subclasses to reuse tests |
| 1058 | |
| 1059 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1060 | self._path = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1061 | |
| 1062 | def tearDown(self): |
| 1063 | self._delete_recursively(self._path) |
| 1064 | |
| 1065 | def test_initialize_with_eMM(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1066 | # Initialize based on email.message.Message instance |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1067 | eMM = email.message_from_string(_sample_message) |
| 1068 | msg = self._factory(eMM) |
| 1069 | self._post_initialize_hook(msg) |
| 1070 | self._check_sample(msg) |
| 1071 | |
| 1072 | def test_initialize_with_string(self): |
| 1073 | # Initialize based on string |
| 1074 | msg = self._factory(_sample_message) |
| 1075 | self._post_initialize_hook(msg) |
| 1076 | self._check_sample(msg) |
| 1077 | |
| 1078 | def test_initialize_with_file(self): |
| 1079 | # Initialize based on contents of file |
| 1080 | f = open(self._path, 'w+') |
| 1081 | f.write(_sample_message) |
| 1082 | f.seek(0) |
| 1083 | msg = self._factory(f) |
| 1084 | self._post_initialize_hook(msg) |
| 1085 | self._check_sample(msg) |
| 1086 | f.close() |
| 1087 | |
| 1088 | def test_initialize_with_nothing(self): |
| 1089 | # Initialize without arguments |
| 1090 | msg = self._factory() |
| 1091 | self._post_initialize_hook(msg) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 1092 | self.assertIsInstance(msg, email.message.Message) |
| 1093 | self.assertIsInstance(msg, mailbox.Message) |
| 1094 | self.assertIsInstance(msg, self._factory) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1095 | self.assertEqual(msg.keys(), []) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1096 | self.assertFalse(msg.is_multipart()) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1097 | self.assertEqual(msg.get_payload(), None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1098 | |
| 1099 | def test_initialize_incorrectly(self): |
| 1100 | # Initialize with invalid argument |
| 1101 | self.assertRaises(TypeError, lambda: self._factory(object())) |
| 1102 | |
| 1103 | def test_become_message(self): |
| 1104 | # Take on the state of another message |
| 1105 | eMM = email.message_from_string(_sample_message) |
| 1106 | msg = self._factory() |
| 1107 | msg._become_message(eMM) |
| 1108 | self._check_sample(msg) |
| 1109 | |
| 1110 | def test_explain_to(self): |
| 1111 | # Copy self's format-specific data to other message formats. |
| 1112 | # This test is superficial; better ones are in TestMessageConversion. |
| 1113 | msg = self._factory() |
| 1114 | for class_ in (mailbox.Message, mailbox.MaildirMessage, |
| 1115 | mailbox.mboxMessage, mailbox.MHMessage, |
| 1116 | mailbox.BabylMessage, mailbox.MMDFMessage): |
| 1117 | other_msg = class_() |
| 1118 | msg._explain_to(other_msg) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1119 | other_msg = email.message.Message() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1120 | self.assertRaises(TypeError, lambda: msg._explain_to(other_msg)) |
| 1121 | |
| 1122 | def _post_initialize_hook(self, msg): |
| 1123 | # Overridden by subclasses to check extra things after initialization |
| 1124 | pass |
| 1125 | |
| 1126 | |
| 1127 | class TestMaildirMessage(TestMessage): |
| 1128 | |
| 1129 | _factory = mailbox.MaildirMessage |
| 1130 | |
| 1131 | def _post_initialize_hook(self, msg): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1132 | self.assertEqual(msg._subdir, 'new') |
| 1133 | self.assertEqual(msg._info, '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1134 | |
| 1135 | def test_subdir(self): |
| 1136 | # Use get_subdir() and set_subdir() |
| 1137 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1138 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1139 | msg.set_subdir('cur') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1140 | self.assertEqual(msg.get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1141 | msg.set_subdir('new') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1142 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1143 | self.assertRaises(ValueError, lambda: msg.set_subdir('tmp')) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1144 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1145 | msg.set_subdir('new') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1146 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1147 | self._check_sample(msg) |
| 1148 | |
| 1149 | def test_flags(self): |
| 1150 | # Use get_flags(), set_flags(), add_flag(), remove_flag() |
| 1151 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1152 | self.assertEqual(msg.get_flags(), '') |
| 1153 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1154 | msg.set_flags('F') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1155 | self.assertEqual(msg.get_subdir(), 'new') |
| 1156 | self.assertEqual(msg.get_flags(), 'F') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1157 | msg.set_flags('SDTP') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1158 | self.assertEqual(msg.get_flags(), 'DPST') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1159 | msg.add_flag('FT') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1160 | self.assertEqual(msg.get_flags(), 'DFPST') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1161 | msg.remove_flag('TDRP') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1162 | self.assertEqual(msg.get_flags(), 'FS') |
| 1163 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1164 | self._check_sample(msg) |
| 1165 | |
| 1166 | def test_date(self): |
| 1167 | # Use get_date() and set_date() |
| 1168 | msg = mailbox.MaildirMessage(_sample_message) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1169 | self.assertLess(abs(msg.get_date() - time.time()), 60) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1170 | msg.set_date(0.0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1171 | self.assertEqual(msg.get_date(), 0.0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1172 | |
| 1173 | def test_info(self): |
| 1174 | # Use get_info() and set_info() |
| 1175 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1176 | self.assertEqual(msg.get_info(), '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1177 | msg.set_info('1,foo=bar') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1178 | self.assertEqual(msg.get_info(), '1,foo=bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1179 | self.assertRaises(TypeError, lambda: msg.set_info(None)) |
| 1180 | self._check_sample(msg) |
| 1181 | |
| 1182 | def test_info_and_flags(self): |
| 1183 | # Test interaction of info and flag methods |
| 1184 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1185 | self.assertEqual(msg.get_info(), '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1186 | msg.set_flags('SF') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1187 | self.assertEqual(msg.get_flags(), 'FS') |
| 1188 | self.assertEqual(msg.get_info(), '2,FS') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1189 | msg.set_info('1,') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1190 | self.assertEqual(msg.get_flags(), '') |
| 1191 | self.assertEqual(msg.get_info(), '1,') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1192 | msg.remove_flag('RPT') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1193 | self.assertEqual(msg.get_flags(), '') |
| 1194 | self.assertEqual(msg.get_info(), '1,') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1195 | msg.add_flag('D') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1196 | self.assertEqual(msg.get_flags(), 'D') |
| 1197 | self.assertEqual(msg.get_info(), '2,D') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1198 | self._check_sample(msg) |
| 1199 | |
| 1200 | |
| 1201 | class _TestMboxMMDFMessage(TestMessage): |
| 1202 | |
| 1203 | _factory = mailbox._mboxMMDFMessage |
| 1204 | |
| 1205 | def _post_initialize_hook(self, msg): |
| 1206 | self._check_from(msg) |
| 1207 | |
| 1208 | def test_initialize_with_unixfrom(self): |
| 1209 | # Initialize with a message that already has a _unixfrom attribute |
| 1210 | msg = mailbox.Message(_sample_message) |
| 1211 | msg.set_unixfrom('From foo@bar blah') |
| 1212 | msg = mailbox.mboxMessage(msg) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1213 | self.assertEqual(msg.get_from(), 'foo@bar blah', msg.get_from()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1214 | |
| 1215 | def test_from(self): |
| 1216 | # Get and set "From " line |
| 1217 | msg = mailbox.mboxMessage(_sample_message) |
| 1218 | self._check_from(msg) |
| 1219 | msg.set_from('foo bar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1220 | self.assertEqual(msg.get_from(), 'foo bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1221 | msg.set_from('foo@bar', True) |
| 1222 | self._check_from(msg, 'foo@bar') |
| 1223 | msg.set_from('blah@temp', time.localtime()) |
| 1224 | self._check_from(msg, 'blah@temp') |
| 1225 | |
| 1226 | def test_flags(self): |
| 1227 | # Use get_flags(), set_flags(), add_flag(), remove_flag() |
| 1228 | msg = mailbox.mboxMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1229 | self.assertEqual(msg.get_flags(), '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1230 | msg.set_flags('F') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1231 | self.assertEqual(msg.get_flags(), 'F') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1232 | msg.set_flags('XODR') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1233 | self.assertEqual(msg.get_flags(), 'RODX') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1234 | msg.add_flag('FA') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1235 | self.assertEqual(msg.get_flags(), 'RODFAX') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1236 | msg.remove_flag('FDXA') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1237 | self.assertEqual(msg.get_flags(), 'RO') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1238 | self._check_sample(msg) |
| 1239 | |
| 1240 | def _check_from(self, msg, sender=None): |
| 1241 | # Check contents of "From " line |
| 1242 | if sender is None: |
| 1243 | sender = "MAILER-DAEMON" |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1244 | self.assertTrue(re.match(sender + r" \w{3} \w{3} [\d ]\d [\d ]\d:\d{2}:" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1245 | r"\d{2} \d{4}", msg.get_from()) is not None) |
| 1246 | |
| 1247 | |
| 1248 | class TestMboxMessage(_TestMboxMMDFMessage): |
| 1249 | |
| 1250 | _factory = mailbox.mboxMessage |
| 1251 | |
| 1252 | |
| 1253 | class TestMHMessage(TestMessage): |
| 1254 | |
| 1255 | _factory = mailbox.MHMessage |
| 1256 | |
| 1257 | def _post_initialize_hook(self, msg): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1258 | self.assertEqual(msg._sequences, []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1259 | |
| 1260 | def test_sequences(self): |
| 1261 | # Get, set, join, and leave sequences |
| 1262 | msg = mailbox.MHMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1263 | self.assertEqual(msg.get_sequences(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1264 | msg.set_sequences(['foobar']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1265 | self.assertEqual(msg.get_sequences(), ['foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1266 | msg.set_sequences([]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1267 | self.assertEqual(msg.get_sequences(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1268 | msg.add_sequence('unseen') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1269 | self.assertEqual(msg.get_sequences(), ['unseen']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1270 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1271 | self.assertEqual(msg.get_sequences(), ['unseen', 'flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1272 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1273 | self.assertEqual(msg.get_sequences(), ['unseen', 'flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1274 | msg.remove_sequence('unseen') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1275 | self.assertEqual(msg.get_sequences(), ['flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1276 | msg.add_sequence('foobar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1277 | self.assertEqual(msg.get_sequences(), ['flagged', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1278 | msg.remove_sequence('replied') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1279 | self.assertEqual(msg.get_sequences(), ['flagged', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1280 | msg.set_sequences(['foobar', 'replied']) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1281 | self.assertEqual(msg.get_sequences(), ['foobar', 'replied']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1282 | |
| 1283 | |
| 1284 | class TestBabylMessage(TestMessage): |
| 1285 | |
| 1286 | _factory = mailbox.BabylMessage |
| 1287 | |
| 1288 | def _post_initialize_hook(self, msg): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1289 | self.assertEqual(msg._labels, []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1290 | |
| 1291 | def test_labels(self): |
| 1292 | # Get, set, join, and leave labels |
| 1293 | msg = mailbox.BabylMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1294 | self.assertEqual(msg.get_labels(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1295 | msg.set_labels(['foobar']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1296 | self.assertEqual(msg.get_labels(), ['foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1297 | msg.set_labels([]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1298 | self.assertEqual(msg.get_labels(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1299 | msg.add_label('filed') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1300 | self.assertEqual(msg.get_labels(), ['filed']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1301 | msg.add_label('resent') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1302 | self.assertEqual(msg.get_labels(), ['filed', 'resent']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1303 | msg.add_label('resent') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1304 | self.assertEqual(msg.get_labels(), ['filed', 'resent']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1305 | msg.remove_label('filed') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1306 | self.assertEqual(msg.get_labels(), ['resent']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1307 | msg.add_label('foobar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1308 | self.assertEqual(msg.get_labels(), ['resent', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1309 | msg.remove_label('unseen') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1310 | self.assertEqual(msg.get_labels(), ['resent', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1311 | msg.set_labels(['foobar', 'answered']) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1312 | self.assertEqual(msg.get_labels(), ['foobar', 'answered']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1313 | |
| 1314 | def test_visible(self): |
| 1315 | # Get, set, and update visible headers |
| 1316 | msg = mailbox.BabylMessage(_sample_message) |
| 1317 | visible = msg.get_visible() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1318 | self.assertEqual(visible.keys(), []) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1319 | self.assertIs(visible.get_payload(), None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1320 | visible['User-Agent'] = 'FooBar 1.0' |
| 1321 | visible['X-Whatever'] = 'Blah' |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1322 | self.assertEqual(msg.get_visible().keys(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1323 | msg.set_visible(visible) |
| 1324 | visible = msg.get_visible() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1325 | self.assertTrue(visible.keys() == ['User-Agent', 'X-Whatever']) |
| 1326 | self.assertTrue(visible['User-Agent'] == 'FooBar 1.0') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1327 | self.assertEqual(visible['X-Whatever'], 'Blah') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1328 | self.assertIs(visible.get_payload(), None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1329 | msg.update_visible() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1330 | self.assertEqual(visible.keys(), ['User-Agent', 'X-Whatever']) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1331 | self.assertIs(visible.get_payload(), None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1332 | visible = msg.get_visible() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1333 | self.assertEqual(visible.keys(), ['User-Agent', 'Date', 'From', 'To', |
| 1334 | 'Subject']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1335 | for header in ('User-Agent', 'Date', 'From', 'To', 'Subject'): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1336 | self.assertEqual(visible[header], msg[header]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1337 | |
| 1338 | |
| 1339 | class TestMMDFMessage(_TestMboxMMDFMessage): |
| 1340 | |
| 1341 | _factory = mailbox.MMDFMessage |
| 1342 | |
| 1343 | |
| 1344 | class TestMessageConversion(TestBase): |
| 1345 | |
| 1346 | def test_plain_to_x(self): |
| 1347 | # Convert Message to all formats |
| 1348 | for class_ in (mailbox.Message, mailbox.MaildirMessage, |
| 1349 | mailbox.mboxMessage, mailbox.MHMessage, |
| 1350 | mailbox.BabylMessage, mailbox.MMDFMessage): |
| 1351 | msg_plain = mailbox.Message(_sample_message) |
| 1352 | msg = class_(msg_plain) |
| 1353 | self._check_sample(msg) |
| 1354 | |
| 1355 | def test_x_to_plain(self): |
| 1356 | # Convert all formats to Message |
| 1357 | for class_ in (mailbox.Message, mailbox.MaildirMessage, |
| 1358 | mailbox.mboxMessage, mailbox.MHMessage, |
| 1359 | mailbox.BabylMessage, mailbox.MMDFMessage): |
| 1360 | msg = class_(_sample_message) |
| 1361 | msg_plain = mailbox.Message(msg) |
| 1362 | self._check_sample(msg_plain) |
| 1363 | |
| 1364 | def test_x_to_invalid(self): |
| 1365 | # Convert all formats to an invalid format |
| 1366 | for class_ in (mailbox.Message, mailbox.MaildirMessage, |
| 1367 | mailbox.mboxMessage, mailbox.MHMessage, |
| 1368 | mailbox.BabylMessage, mailbox.MMDFMessage): |
| 1369 | self.assertRaises(TypeError, lambda: class_(False)) |
| 1370 | |
| 1371 | def test_maildir_to_maildir(self): |
| 1372 | # Convert MaildirMessage to MaildirMessage |
| 1373 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1374 | msg_maildir.set_flags('DFPRST') |
| 1375 | msg_maildir.set_subdir('cur') |
| 1376 | date = msg_maildir.get_date() |
| 1377 | msg = mailbox.MaildirMessage(msg_maildir) |
| 1378 | self._check_sample(msg) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1379 | self.assertEqual(msg.get_flags(), 'DFPRST') |
| 1380 | self.assertEqual(msg.get_subdir(), 'cur') |
| 1381 | self.assertEqual(msg.get_date(), date) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1382 | |
| 1383 | def test_maildir_to_mboxmmdf(self): |
| 1384 | # Convert MaildirMessage to mboxmessage and MMDFMessage |
| 1385 | pairs = (('D', ''), ('F', 'F'), ('P', ''), ('R', 'A'), ('S', 'R'), |
| 1386 | ('T', 'D'), ('DFPRST', 'RDFA')) |
| 1387 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1388 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1389 | msg_maildir.set_date(0.0) |
| 1390 | for setting, result in pairs: |
| 1391 | msg_maildir.set_flags(setting) |
| 1392 | msg = class_(msg_maildir) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1393 | self.assertEqual(msg.get_flags(), result) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1394 | self.assertEqual(msg.get_from(), 'MAILER-DAEMON %s' % |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1395 | time.asctime(time.gmtime(0.0))) |
| 1396 | msg_maildir.set_subdir('cur') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1397 | self.assertEqual(class_(msg_maildir).get_flags(), 'RODFA') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1398 | |
| 1399 | def test_maildir_to_mh(self): |
| 1400 | # Convert MaildirMessage to MHMessage |
| 1401 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1402 | pairs = (('D', ['unseen']), ('F', ['unseen', 'flagged']), |
| 1403 | ('P', ['unseen']), ('R', ['unseen', 'replied']), ('S', []), |
| 1404 | ('T', ['unseen']), ('DFPRST', ['replied', 'flagged'])) |
| 1405 | for setting, result in pairs: |
| 1406 | msg_maildir.set_flags(setting) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1407 | self.assertEqual(mailbox.MHMessage(msg_maildir).get_sequences(), |
| 1408 | result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1409 | |
| 1410 | def test_maildir_to_babyl(self): |
| 1411 | # Convert MaildirMessage to Babyl |
| 1412 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1413 | pairs = (('D', ['unseen']), ('F', ['unseen']), |
| 1414 | ('P', ['unseen', 'forwarded']), ('R', ['unseen', 'answered']), |
| 1415 | ('S', []), ('T', ['unseen', 'deleted']), |
| 1416 | ('DFPRST', ['deleted', 'answered', 'forwarded'])) |
| 1417 | for setting, result in pairs: |
| 1418 | msg_maildir.set_flags(setting) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1419 | self.assertEqual(mailbox.BabylMessage(msg_maildir).get_labels(), |
| 1420 | result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1421 | |
| 1422 | def test_mboxmmdf_to_maildir(self): |
| 1423 | # Convert mboxMessage and MMDFMessage to MaildirMessage |
| 1424 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1425 | msg_mboxMMDF = class_(_sample_message) |
| 1426 | msg_mboxMMDF.set_from('foo@bar', time.gmtime(0.0)) |
| 1427 | pairs = (('R', 'S'), ('O', ''), ('D', 'T'), ('F', 'F'), ('A', 'R'), |
| 1428 | ('RODFA', 'FRST')) |
| 1429 | for setting, result in pairs: |
| 1430 | msg_mboxMMDF.set_flags(setting) |
| 1431 | msg = mailbox.MaildirMessage(msg_mboxMMDF) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1432 | self.assertEqual(msg.get_flags(), result) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1433 | self.assertEqual(msg.get_date(), 0.0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1434 | msg_mboxMMDF.set_flags('O') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1435 | self.assertEqual(mailbox.MaildirMessage(msg_mboxMMDF).get_subdir(), |
| 1436 | 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1437 | |
| 1438 | def test_mboxmmdf_to_mboxmmdf(self): |
| 1439 | # Convert mboxMessage and MMDFMessage to mboxMessage and MMDFMessage |
| 1440 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1441 | msg_mboxMMDF = class_(_sample_message) |
| 1442 | msg_mboxMMDF.set_flags('RODFA') |
| 1443 | msg_mboxMMDF.set_from('foo@bar') |
| 1444 | for class2_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1445 | msg2 = class2_(msg_mboxMMDF) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1446 | self.assertEqual(msg2.get_flags(), 'RODFA') |
| 1447 | self.assertEqual(msg2.get_from(), 'foo@bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1448 | |
| 1449 | def test_mboxmmdf_to_mh(self): |
| 1450 | # Convert mboxMessage and MMDFMessage to MHMessage |
| 1451 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1452 | msg_mboxMMDF = class_(_sample_message) |
| 1453 | pairs = (('R', []), ('O', ['unseen']), ('D', ['unseen']), |
| 1454 | ('F', ['unseen', 'flagged']), |
| 1455 | ('A', ['unseen', 'replied']), |
| 1456 | ('RODFA', ['replied', 'flagged'])) |
| 1457 | for setting, result in pairs: |
| 1458 | msg_mboxMMDF.set_flags(setting) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1459 | self.assertEqual(mailbox.MHMessage(msg_mboxMMDF).get_sequences(), |
| 1460 | result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1461 | |
| 1462 | def test_mboxmmdf_to_babyl(self): |
| 1463 | # Convert mboxMessage and MMDFMessage to BabylMessage |
| 1464 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1465 | msg = class_(_sample_message) |
| 1466 | pairs = (('R', []), ('O', ['unseen']), |
| 1467 | ('D', ['unseen', 'deleted']), ('F', ['unseen']), |
| 1468 | ('A', ['unseen', 'answered']), |
| 1469 | ('RODFA', ['deleted', 'answered'])) |
| 1470 | for setting, result in pairs: |
| 1471 | msg.set_flags(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1472 | self.assertEqual(mailbox.BabylMessage(msg).get_labels(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1473 | |
| 1474 | def test_mh_to_maildir(self): |
| 1475 | # Convert MHMessage to MaildirMessage |
| 1476 | pairs = (('unseen', ''), ('replied', 'RS'), ('flagged', 'FS')) |
| 1477 | for setting, result in pairs: |
| 1478 | msg = mailbox.MHMessage(_sample_message) |
| 1479 | msg.add_sequence(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1480 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), result) |
| 1481 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1482 | msg = mailbox.MHMessage(_sample_message) |
| 1483 | msg.add_sequence('unseen') |
| 1484 | msg.add_sequence('replied') |
| 1485 | msg.add_sequence('flagged') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1486 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), 'FR') |
| 1487 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1488 | |
| 1489 | def test_mh_to_mboxmmdf(self): |
| 1490 | # Convert MHMessage to mboxMessage and MMDFMessage |
| 1491 | pairs = (('unseen', 'O'), ('replied', 'ROA'), ('flagged', 'ROF')) |
| 1492 | for setting, result in pairs: |
| 1493 | msg = mailbox.MHMessage(_sample_message) |
| 1494 | msg.add_sequence(setting) |
| 1495 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1496 | self.assertEqual(class_(msg).get_flags(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1497 | msg = mailbox.MHMessage(_sample_message) |
| 1498 | msg.add_sequence('unseen') |
| 1499 | msg.add_sequence('replied') |
| 1500 | msg.add_sequence('flagged') |
| 1501 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1502 | self.assertEqual(class_(msg).get_flags(), 'OFA') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1503 | |
| 1504 | def test_mh_to_mh(self): |
| 1505 | # Convert MHMessage to MHMessage |
| 1506 | msg = mailbox.MHMessage(_sample_message) |
| 1507 | msg.add_sequence('unseen') |
| 1508 | msg.add_sequence('replied') |
| 1509 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1510 | self.assertEqual(mailbox.MHMessage(msg).get_sequences(), |
| 1511 | ['unseen', 'replied', 'flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1512 | |
| 1513 | def test_mh_to_babyl(self): |
| 1514 | # Convert MHMessage to BabylMessage |
| 1515 | pairs = (('unseen', ['unseen']), ('replied', ['answered']), |
| 1516 | ('flagged', [])) |
| 1517 | for setting, result in pairs: |
| 1518 | msg = mailbox.MHMessage(_sample_message) |
| 1519 | msg.add_sequence(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1520 | self.assertEqual(mailbox.BabylMessage(msg).get_labels(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1521 | msg = mailbox.MHMessage(_sample_message) |
| 1522 | msg.add_sequence('unseen') |
| 1523 | msg.add_sequence('replied') |
| 1524 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1525 | self.assertEqual(mailbox.BabylMessage(msg).get_labels(), |
| 1526 | ['unseen', 'answered']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1527 | |
| 1528 | def test_babyl_to_maildir(self): |
| 1529 | # Convert BabylMessage to MaildirMessage |
| 1530 | pairs = (('unseen', ''), ('deleted', 'ST'), ('filed', 'S'), |
| 1531 | ('answered', 'RS'), ('forwarded', 'PS'), ('edited', 'S'), |
| 1532 | ('resent', 'PS')) |
| 1533 | for setting, result in pairs: |
| 1534 | msg = mailbox.BabylMessage(_sample_message) |
| 1535 | msg.add_label(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1536 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), result) |
| 1537 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1538 | msg = mailbox.BabylMessage(_sample_message) |
| 1539 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1540 | 'edited', 'resent'): |
| 1541 | msg.add_label(label) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1542 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), 'PRT') |
| 1543 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1544 | |
| 1545 | def test_babyl_to_mboxmmdf(self): |
| 1546 | # Convert BabylMessage to mboxMessage and MMDFMessage |
| 1547 | pairs = (('unseen', 'O'), ('deleted', 'ROD'), ('filed', 'RO'), |
| 1548 | ('answered', 'ROA'), ('forwarded', 'RO'), ('edited', 'RO'), |
| 1549 | ('resent', 'RO')) |
| 1550 | for setting, result in pairs: |
| 1551 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1552 | msg = mailbox.BabylMessage(_sample_message) |
| 1553 | msg.add_label(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1554 | self.assertEqual(class_(msg).get_flags(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1555 | msg = mailbox.BabylMessage(_sample_message) |
| 1556 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1557 | 'edited', 'resent'): |
| 1558 | msg.add_label(label) |
| 1559 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1560 | self.assertEqual(class_(msg).get_flags(), 'ODA') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1561 | |
| 1562 | def test_babyl_to_mh(self): |
| 1563 | # Convert BabylMessage to MHMessage |
| 1564 | pairs = (('unseen', ['unseen']), ('deleted', []), ('filed', []), |
| 1565 | ('answered', ['replied']), ('forwarded', []), ('edited', []), |
| 1566 | ('resent', [])) |
| 1567 | for setting, result in pairs: |
| 1568 | msg = mailbox.BabylMessage(_sample_message) |
| 1569 | msg.add_label(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1570 | self.assertEqual(mailbox.MHMessage(msg).get_sequences(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1571 | msg = mailbox.BabylMessage(_sample_message) |
| 1572 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1573 | 'edited', 'resent'): |
| 1574 | msg.add_label(label) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1575 | self.assertEqual(mailbox.MHMessage(msg).get_sequences(), |
| 1576 | ['unseen', 'replied']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1577 | |
| 1578 | def test_babyl_to_babyl(self): |
| 1579 | # Convert BabylMessage to BabylMessage |
| 1580 | msg = mailbox.BabylMessage(_sample_message) |
| 1581 | msg.update_visible() |
| 1582 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1583 | 'edited', 'resent'): |
| 1584 | msg.add_label(label) |
| 1585 | msg2 = mailbox.BabylMessage(msg) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1586 | self.assertEqual(msg2.get_labels(), ['unseen', 'deleted', 'filed', |
| 1587 | 'answered', 'forwarded', 'edited', |
| 1588 | 'resent']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1589 | self.assertEqual(msg.get_visible().keys(), msg2.get_visible().keys()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1590 | for key in msg.get_visible().keys(): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1591 | self.assertEqual(msg.get_visible()[key], msg2.get_visible()[key]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1592 | |
| 1593 | |
| 1594 | class TestProxyFileBase(TestBase): |
| 1595 | |
| 1596 | def _test_read(self, proxy): |
| 1597 | # Read by byte |
| 1598 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1599 | self.assertEqual(proxy.read(), b'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1600 | proxy.seek(1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1601 | self.assertEqual(proxy.read(), b'ar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1602 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1603 | self.assertEqual(proxy.read(2), b'ba') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1604 | proxy.seek(1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1605 | self.assertEqual(proxy.read(-1), b'ar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1606 | proxy.seek(2) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1607 | self.assertEqual(proxy.read(1000), b'r') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1608 | |
| 1609 | def _test_readline(self, proxy): |
| 1610 | # Read by line |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1611 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1612 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1613 | self.assertEqual(proxy.readline(), b'foo' + linesep) |
| 1614 | self.assertEqual(proxy.readline(), b'bar' + linesep) |
| 1615 | self.assertEqual(proxy.readline(), b'fred' + linesep) |
| 1616 | self.assertEqual(proxy.readline(), b'bob') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1617 | proxy.seek(2) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1618 | self.assertEqual(proxy.readline(), b'o' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1619 | proxy.seek(6 + 2 * len(os.linesep)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1620 | self.assertEqual(proxy.readline(), b'fred' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1621 | proxy.seek(6 + 2 * len(os.linesep)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1622 | self.assertEqual(proxy.readline(2), b'fr') |
| 1623 | self.assertEqual(proxy.readline(-10), b'ed' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1624 | |
| 1625 | def _test_readlines(self, proxy): |
| 1626 | # Read multiple lines |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1627 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1628 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1629 | self.assertEqual(proxy.readlines(), [b'foo' + linesep, |
| 1630 | b'bar' + linesep, |
| 1631 | b'fred' + linesep, b'bob']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1632 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1633 | self.assertEqual(proxy.readlines(2), [b'foo' + linesep]) |
| 1634 | proxy.seek(3 + len(linesep)) |
| 1635 | self.assertEqual(proxy.readlines(4 + len(linesep)), |
| 1636 | [b'bar' + linesep, b'fred' + linesep]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1637 | proxy.seek(3) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1638 | self.assertEqual(proxy.readlines(1000), [linesep, b'bar' + linesep, |
| 1639 | b'fred' + linesep, b'bob']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1640 | |
| 1641 | def _test_iteration(self, proxy): |
| 1642 | # Iterate by line |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1643 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1644 | proxy.seek(0) |
| 1645 | iterator = iter(proxy) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1646 | self.assertEqual(next(iterator), b'foo' + linesep) |
| 1647 | self.assertEqual(next(iterator), b'bar' + linesep) |
| 1648 | self.assertEqual(next(iterator), b'fred' + linesep) |
| 1649 | self.assertEqual(next(iterator), b'bob') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1650 | self.assertRaises(StopIteration, next, iterator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1651 | |
| 1652 | def _test_seek_and_tell(self, proxy): |
| 1653 | # Seek and use tell to check position |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1654 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1655 | proxy.seek(3) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1656 | self.assertEqual(proxy.tell(), 3) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1657 | self.assertEqual(proxy.read(len(linesep)), linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1658 | proxy.seek(2, 1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1659 | self.assertEqual(proxy.read(1 + len(linesep)), b'r' + linesep) |
| 1660 | proxy.seek(-3 - len(linesep), 2) |
| 1661 | self.assertEqual(proxy.read(3), b'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1662 | proxy.seek(2, 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1663 | self.assertEqual(proxy.read(), b'o' + linesep + b'bar' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1664 | proxy.seek(100) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1665 | self.assertFalse(proxy.read()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1666 | |
| 1667 | def _test_close(self, proxy): |
| 1668 | # Close a file |
| 1669 | proxy.close() |
| 1670 | self.assertRaises(AttributeError, lambda: proxy.close()) |
| 1671 | |
| 1672 | |
| 1673 | class TestProxyFile(TestProxyFileBase): |
| 1674 | |
| 1675 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1676 | self._path = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1677 | self._file = open(self._path, 'wb+') |
| 1678 | |
| 1679 | def tearDown(self): |
| 1680 | self._file.close() |
| 1681 | self._delete_recursively(self._path) |
| 1682 | |
| 1683 | def test_initialize(self): |
| 1684 | # Initialize and check position |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1685 | self._file.write(b'foo') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1686 | pos = self._file.tell() |
| 1687 | proxy0 = mailbox._ProxyFile(self._file) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1688 | self.assertEqual(proxy0.tell(), pos) |
| 1689 | self.assertEqual(self._file.tell(), pos) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1690 | proxy1 = mailbox._ProxyFile(self._file, 0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1691 | self.assertEqual(proxy1.tell(), 0) |
| 1692 | self.assertEqual(self._file.tell(), pos) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1693 | |
| 1694 | def test_read(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1695 | self._file.write(b'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1696 | self._test_read(mailbox._ProxyFile(self._file)) |
| 1697 | |
| 1698 | def test_readline(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1699 | self._file.write(bytes('foo%sbar%sfred%sbob' % (os.linesep, os.linesep, |
| 1700 | os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1701 | self._test_readline(mailbox._ProxyFile(self._file)) |
| 1702 | |
| 1703 | def test_readlines(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1704 | self._file.write(bytes('foo%sbar%sfred%sbob' % (os.linesep, os.linesep, |
| 1705 | os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1706 | self._test_readlines(mailbox._ProxyFile(self._file)) |
| 1707 | |
| 1708 | def test_iteration(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1709 | self._file.write(bytes('foo%sbar%sfred%sbob' % (os.linesep, os.linesep, |
| 1710 | os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1711 | self._test_iteration(mailbox._ProxyFile(self._file)) |
| 1712 | |
| 1713 | def test_seek_and_tell(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1714 | self._file.write(bytes('foo%sbar%s' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1715 | self._test_seek_and_tell(mailbox._ProxyFile(self._file)) |
| 1716 | |
| 1717 | def test_close(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1718 | self._file.write(bytes('foo%sbar%s' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1719 | self._test_close(mailbox._ProxyFile(self._file)) |
| 1720 | |
| 1721 | |
| 1722 | class TestPartialFile(TestProxyFileBase): |
| 1723 | |
| 1724 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1725 | self._path = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1726 | self._file = open(self._path, 'wb+') |
| 1727 | |
| 1728 | def tearDown(self): |
| 1729 | self._file.close() |
| 1730 | self._delete_recursively(self._path) |
| 1731 | |
| 1732 | def test_initialize(self): |
| 1733 | # Initialize and check position |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1734 | self._file.write(bytes('foo' + os.linesep + 'bar', 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1735 | pos = self._file.tell() |
| 1736 | proxy = mailbox._PartialFile(self._file, 2, 5) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1737 | self.assertEqual(proxy.tell(), 0) |
| 1738 | self.assertEqual(self._file.tell(), pos) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1739 | |
| 1740 | def test_read(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1741 | self._file.write(bytes('***bar***', 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1742 | self._test_read(mailbox._PartialFile(self._file, 3, 6)) |
| 1743 | |
| 1744 | def test_readline(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1745 | self._file.write(bytes('!!!!!foo%sbar%sfred%sbob!!!!!' % |
| 1746 | (os.linesep, os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1747 | self._test_readline(mailbox._PartialFile(self._file, 5, |
| 1748 | 18 + 3 * len(os.linesep))) |
| 1749 | |
| 1750 | def test_readlines(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1751 | self._file.write(bytes('foo%sbar%sfred%sbob?????' % |
| 1752 | (os.linesep, os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1753 | self._test_readlines(mailbox._PartialFile(self._file, 0, |
| 1754 | 13 + 3 * len(os.linesep))) |
| 1755 | |
| 1756 | def test_iteration(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1757 | self._file.write(bytes('____foo%sbar%sfred%sbob####' % |
| 1758 | (os.linesep, os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1759 | self._test_iteration(mailbox._PartialFile(self._file, 4, |
| 1760 | 17 + 3 * len(os.linesep))) |
| 1761 | |
| 1762 | def test_seek_and_tell(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1763 | self._file.write(bytes('(((foo%sbar%s$$$' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1764 | self._test_seek_and_tell(mailbox._PartialFile(self._file, 3, |
| 1765 | 9 + 2 * len(os.linesep))) |
| 1766 | |
| 1767 | def test_close(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 1768 | self._file.write(bytes('&foo%sbar%s^' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1769 | self._test_close(mailbox._PartialFile(self._file, 1, |
| 1770 | 6 + 3 * len(os.linesep))) |
| 1771 | |
| 1772 | |
| 1773 | ## Start: tests from the original module (for backward compatibility). |
| 1774 | |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 1775 | FROM_ = "From some.body@dummy.domain Sat Jul 24 13:43:35 2004\n" |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1776 | DUMMY_MESSAGE = """\ |
| 1777 | From: some.body@dummy.domain |
| 1778 | To: me@my.domain |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 1779 | Subject: Simple Test |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1780 | |
| 1781 | This is a dummy message. |
| 1782 | """ |
| 1783 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1784 | class MaildirTestCase(unittest.TestCase): |
| 1785 | |
| 1786 | def setUp(self): |
| 1787 | # create a new maildir mailbox to work with: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1788 | self._dir = support.TESTFN |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1789 | os.mkdir(self._dir) |
| 1790 | os.mkdir(os.path.join(self._dir, "cur")) |
| 1791 | os.mkdir(os.path.join(self._dir, "tmp")) |
| 1792 | os.mkdir(os.path.join(self._dir, "new")) |
| 1793 | self._counter = 1 |
| 1794 | self._msgfiles = [] |
| 1795 | |
| 1796 | def tearDown(self): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1797 | list(map(os.unlink, self._msgfiles)) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1798 | os.rmdir(os.path.join(self._dir, "cur")) |
| 1799 | os.rmdir(os.path.join(self._dir, "tmp")) |
| 1800 | os.rmdir(os.path.join(self._dir, "new")) |
| 1801 | os.rmdir(self._dir) |
| 1802 | |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 1803 | def createMessage(self, dir, mbox=False): |
Jack Jansen | 97df7b6 | 2001-06-19 20:20:05 +0000 | [diff] [blame] | 1804 | t = int(time.time() % 1000000) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1805 | pid = self._counter |
| 1806 | self._counter += 1 |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 1807 | filename = ".".join((str(t), str(pid), "myhostname", "mydomain")) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1808 | tmpname = os.path.join(self._dir, "tmp", filename) |
| 1809 | newname = os.path.join(self._dir, dir, filename) |
| 1810 | fp = open(tmpname, "w") |
| 1811 | self._msgfiles.append(tmpname) |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 1812 | if mbox: |
| 1813 | fp.write(FROM_) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1814 | fp.write(DUMMY_MESSAGE) |
| 1815 | fp.close() |
Tim Peters | 6626c1f | 2001-05-22 16:29:01 +0000 | [diff] [blame] | 1816 | if hasattr(os, "link"): |
| 1817 | os.link(tmpname, newname) |
| 1818 | else: |
| 1819 | fp = open(newname, "w") |
| 1820 | fp.write(DUMMY_MESSAGE) |
| 1821 | fp.close() |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1822 | self._msgfiles.append(newname) |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 1823 | return tmpname |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1824 | |
| 1825 | def test_empty_maildir(self): |
| 1826 | """Test an empty maildir mailbox""" |
| 1827 | # Test for regression on bug #117490: |
| 1828 | # Make sure the boxes attribute actually gets set. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1829 | self.mbox = mailbox.Maildir(support.TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1830 | #self.assertTrue(hasattr(self.mbox, "boxes")) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1831 | #self.assertEqual(len(self.mbox.boxes), 0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1832 | self.assertIs(self.mbox.next(), None) |
| 1833 | self.assertIs(self.mbox.next(), None) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1834 | |
| 1835 | def test_nonempty_maildir_cur(self): |
| 1836 | self.createMessage("cur") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1837 | self.mbox = mailbox.Maildir(support.TESTFN) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1838 | #self.assertEqual(len(self.mbox.boxes), 1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1839 | self.assertIsNot(self.mbox.next(), None) |
| 1840 | self.assertIs(self.mbox.next(), None) |
| 1841 | self.assertIs(self.mbox.next(), None) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1842 | |
| 1843 | def test_nonempty_maildir_new(self): |
| 1844 | self.createMessage("new") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1845 | self.mbox = mailbox.Maildir(support.TESTFN) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1846 | #self.assertEqual(len(self.mbox.boxes), 1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1847 | self.assertIsNot(self.mbox.next(), None) |
| 1848 | self.assertIs(self.mbox.next(), None) |
| 1849 | self.assertIs(self.mbox.next(), None) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1850 | |
| 1851 | def test_nonempty_maildir_both(self): |
| 1852 | self.createMessage("cur") |
| 1853 | self.createMessage("new") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1854 | self.mbox = mailbox.Maildir(support.TESTFN) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1855 | #self.assertEqual(len(self.mbox.boxes), 2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1856 | self.assertIsNot(self.mbox.next(), None) |
| 1857 | self.assertIsNot(self.mbox.next(), None) |
| 1858 | self.assertIs(self.mbox.next(), None) |
| 1859 | self.assertIs(self.mbox.next(), None) |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 1860 | |
Georg Brandl | edb978f | 2008-05-11 08:59:40 +0000 | [diff] [blame] | 1861 | ## End: tests from the original module (for backward compatibility). |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1862 | |
| 1863 | |
| 1864 | _sample_message = """\ |
| 1865 | Return-Path: <gkj@gregorykjohnson.com> |
| 1866 | X-Original-To: gkj+person@localhost |
| 1867 | Delivered-To: gkj+person@localhost |
| 1868 | Received: from localhost (localhost [127.0.0.1]) |
| 1869 | by andy.gregorykjohnson.com (Postfix) with ESMTP id 356ED9DD17 |
| 1870 | for <gkj+person@localhost>; Wed, 13 Jul 2005 17:23:16 -0400 (EDT) |
| 1871 | Delivered-To: gkj@sundance.gregorykjohnson.com |
| 1872 | Received: from localhost [127.0.0.1] |
| 1873 | by localhost with POP3 (fetchmail-6.2.5) |
| 1874 | for gkj+person@localhost (single-drop); Wed, 13 Jul 2005 17:23:16 -0400 (EDT) |
| 1875 | Received: from andy.gregorykjohnson.com (andy.gregorykjohnson.com [64.32.235.228]) |
| 1876 | by sundance.gregorykjohnson.com (Postfix) with ESMTP id 5B056316746 |
| 1877 | for <gkj@gregorykjohnson.com>; Wed, 13 Jul 2005 17:23:11 -0400 (EDT) |
| 1878 | Received: by andy.gregorykjohnson.com (Postfix, from userid 1000) |
| 1879 | id 490CD9DD17; Wed, 13 Jul 2005 17:23:11 -0400 (EDT) |
| 1880 | Date: Wed, 13 Jul 2005 17:23:11 -0400 |
| 1881 | From: "Gregory K. Johnson" <gkj@gregorykjohnson.com> |
| 1882 | To: gkj@gregorykjohnson.com |
| 1883 | Subject: Sample message |
| 1884 | Message-ID: <20050713212311.GC4701@andy.gregorykjohnson.com> |
| 1885 | Mime-Version: 1.0 |
| 1886 | Content-Type: multipart/mixed; boundary="NMuMz9nt05w80d4+" |
| 1887 | Content-Disposition: inline |
| 1888 | User-Agent: Mutt/1.5.9i |
| 1889 | |
| 1890 | |
| 1891 | --NMuMz9nt05w80d4+ |
| 1892 | Content-Type: text/plain; charset=us-ascii |
| 1893 | Content-Disposition: inline |
| 1894 | |
| 1895 | This is a sample message. |
| 1896 | |
| 1897 | -- |
| 1898 | Gregory K. Johnson |
| 1899 | |
| 1900 | --NMuMz9nt05w80d4+ |
| 1901 | Content-Type: application/octet-stream |
| 1902 | Content-Disposition: attachment; filename="text.gz" |
| 1903 | Content-Transfer-Encoding: base64 |
| 1904 | |
| 1905 | H4sICM2D1UIAA3RleHQAC8nILFYAokSFktSKEoW0zJxUPa7wzJIMhZLyfIWczLzUYj0uAHTs |
| 1906 | 3FYlAAAA |
| 1907 | |
| 1908 | --NMuMz9nt05w80d4+-- |
| 1909 | """ |
| 1910 | |
| 1911 | _sample_headers = { |
| 1912 | "Return-Path":"<gkj@gregorykjohnson.com>", |
| 1913 | "X-Original-To":"gkj+person@localhost", |
| 1914 | "Delivered-To":"gkj+person@localhost", |
| 1915 | "Received":"""from localhost (localhost [127.0.0.1]) |
| 1916 | by andy.gregorykjohnson.com (Postfix) with ESMTP id 356ED9DD17 |
| 1917 | for <gkj+person@localhost>; Wed, 13 Jul 2005 17:23:16 -0400 (EDT)""", |
| 1918 | "Delivered-To":"gkj@sundance.gregorykjohnson.com", |
| 1919 | "Received":"""from localhost [127.0.0.1] |
| 1920 | by localhost with POP3 (fetchmail-6.2.5) |
| 1921 | for gkj+person@localhost (single-drop); Wed, 13 Jul 2005 17:23:16 -0400 (EDT)""", |
| 1922 | "Received":"""from andy.gregorykjohnson.com (andy.gregorykjohnson.com [64.32.235.228]) |
| 1923 | by sundance.gregorykjohnson.com (Postfix) with ESMTP id 5B056316746 |
| 1924 | for <gkj@gregorykjohnson.com>; Wed, 13 Jul 2005 17:23:11 -0400 (EDT)""", |
| 1925 | "Received":"""by andy.gregorykjohnson.com (Postfix, from userid 1000) |
| 1926 | id 490CD9DD17; Wed, 13 Jul 2005 17:23:11 -0400 (EDT)""", |
| 1927 | "Date":"Wed, 13 Jul 2005 17:23:11 -0400", |
| 1928 | "From":""""Gregory K. Johnson" <gkj@gregorykjohnson.com>""", |
| 1929 | "To":"gkj@gregorykjohnson.com", |
| 1930 | "Subject":"Sample message", |
| 1931 | "Mime-Version":"1.0", |
| 1932 | "Content-Type":"""multipart/mixed; boundary="NMuMz9nt05w80d4+\"""", |
| 1933 | "Content-Disposition":"inline", |
| 1934 | "User-Agent": "Mutt/1.5.9i" } |
| 1935 | |
| 1936 | _sample_payloads = ("""This is a sample message. |
| 1937 | |
| 1938 | -- |
| 1939 | Gregory K. Johnson |
| 1940 | """, |
| 1941 | """H4sICM2D1UIAA3RleHQAC8nILFYAokSFktSKEoW0zJxUPa7wzJIMhZLyfIWczLzUYj0uAHTs |
| 1942 | 3FYlAAAA |
| 1943 | """) |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 1944 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 1945 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1946 | def test_main(): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1947 | tests = (TestMailboxSuperclass, TestMaildir, TestMbox, TestMMDF, TestMH, |
| 1948 | TestBabyl, TestMessage, TestMaildirMessage, TestMboxMessage, |
| 1949 | TestMHMessage, TestBabylMessage, TestMMDFMessage, |
| 1950 | TestMessageConversion, TestProxyFile, TestPartialFile, |
| 1951 | MaildirTestCase) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1952 | support.run_unittest(*tests) |
| 1953 | support.reap_children() |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1954 | |
| 1955 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1956 | if __name__ == '__main__': |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1957 | test_main() |