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