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