Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 1 | import mailbox |
| 2 | import os |
| 3 | import test_support |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 4 | import time |
| 5 | import unittest |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 6 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 7 | # cleanup earlier tests |
Guido van Rossum | 75ebb29 | 2001-04-10 15:01:20 +0000 | [diff] [blame] | 8 | try: |
| 9 | os.unlink(test_support.TESTFN) |
| 10 | except os.error: |
| 11 | pass |
| 12 | |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 13 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 14 | DUMMY_MESSAGE = """\ |
| 15 | From: some.body@dummy.domain |
| 16 | To: me@my.domain |
| 17 | |
| 18 | This is a dummy message. |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | class MaildirTestCase(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
| 25 | # create a new maildir mailbox to work with: |
| 26 | self._dir = test_support.TESTFN |
| 27 | os.mkdir(self._dir) |
| 28 | os.mkdir(os.path.join(self._dir, "cur")) |
| 29 | os.mkdir(os.path.join(self._dir, "tmp")) |
| 30 | os.mkdir(os.path.join(self._dir, "new")) |
| 31 | self._counter = 1 |
| 32 | self._msgfiles = [] |
| 33 | |
| 34 | def tearDown(self): |
| 35 | map(os.unlink, self._msgfiles) |
| 36 | os.rmdir(os.path.join(self._dir, "cur")) |
| 37 | os.rmdir(os.path.join(self._dir, "tmp")) |
| 38 | os.rmdir(os.path.join(self._dir, "new")) |
| 39 | os.rmdir(self._dir) |
| 40 | |
| 41 | def createMessage(self, dir): |
Jack Jansen | 97df7b6 | 2001-06-19 20:20:05 +0000 | [diff] [blame] | 42 | t = int(time.time() % 1000000) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 43 | pid = self._counter |
| 44 | self._counter += 1 |
| 45 | filename = "%s.%s.myhostname.mydomain" % (t, pid) |
| 46 | tmpname = os.path.join(self._dir, "tmp", filename) |
| 47 | newname = os.path.join(self._dir, dir, filename) |
| 48 | fp = open(tmpname, "w") |
| 49 | self._msgfiles.append(tmpname) |
| 50 | fp.write(DUMMY_MESSAGE) |
| 51 | fp.close() |
Tim Peters | 6626c1f | 2001-05-22 16:29:01 +0000 | [diff] [blame] | 52 | if hasattr(os, "link"): |
| 53 | os.link(tmpname, newname) |
| 54 | else: |
| 55 | fp = open(newname, "w") |
| 56 | fp.write(DUMMY_MESSAGE) |
| 57 | fp.close() |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 58 | self._msgfiles.append(newname) |
| 59 | |
| 60 | def test_empty_maildir(self): |
| 61 | """Test an empty maildir mailbox""" |
| 62 | # Test for regression on bug #117490: |
| 63 | # Make sure the boxes attribute actually gets set. |
| 64 | self.mbox = mailbox.Maildir(test_support.TESTFN) |
| 65 | self.assert_(hasattr(self.mbox, "boxes")) |
| 66 | self.assert_(len(self.mbox.boxes) == 0) |
| 67 | self.assert_(self.mbox.next() is None) |
| 68 | self.assert_(self.mbox.next() is None) |
| 69 | |
| 70 | def test_nonempty_maildir_cur(self): |
| 71 | self.createMessage("cur") |
| 72 | self.mbox = mailbox.Maildir(test_support.TESTFN) |
| 73 | self.assert_(len(self.mbox.boxes) == 1) |
| 74 | self.assert_(self.mbox.next() is not None) |
| 75 | self.assert_(self.mbox.next() is None) |
| 76 | self.assert_(self.mbox.next() is None) |
| 77 | |
| 78 | def test_nonempty_maildir_new(self): |
| 79 | self.createMessage("new") |
| 80 | self.mbox = mailbox.Maildir(test_support.TESTFN) |
| 81 | self.assert_(len(self.mbox.boxes) == 1) |
| 82 | self.assert_(self.mbox.next() is not None) |
| 83 | self.assert_(self.mbox.next() is None) |
| 84 | self.assert_(self.mbox.next() is None) |
| 85 | |
| 86 | def test_nonempty_maildir_both(self): |
| 87 | self.createMessage("cur") |
| 88 | self.createMessage("new") |
| 89 | self.mbox = mailbox.Maildir(test_support.TESTFN) |
| 90 | self.assert_(len(self.mbox.boxes) == 2) |
| 91 | self.assert_(self.mbox.next() is not None) |
| 92 | self.assert_(self.mbox.next() is not None) |
| 93 | self.assert_(self.mbox.next() is None) |
| 94 | self.assert_(self.mbox.next() is None) |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 95 | |
| 96 | # XXX We still need more tests! |
| 97 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 98 | |
| 99 | test_support.run_unittest(MaildirTestCase) |