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