blob: dc89fd8a712642c4d4794554e3f157d0aa9be064 [file] [log] [blame]
Fred Drake0aaed272000-10-23 13:39:15 +00001import mailbox
2import os
Fred Drakec02bc3e2001-05-21 20:23:21 +00003import time
4import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_support
Fred Drake0aaed272000-10-23 13:39:15 +00006
Fred Drakec02bc3e2001-05-21 20:23:21 +00007# cleanup earlier tests
Guido van Rossum75ebb292001-04-10 15:01:20 +00008try:
9 os.unlink(test_support.TESTFN)
10except os.error:
11 pass
12
Fred Drake0aaed272000-10-23 13:39:15 +000013
Fred Drakec02bc3e2001-05-21 20:23:21 +000014DUMMY_MESSAGE = """\
15From: some.body@dummy.domain
16To: me@my.domain
17
18This is a dummy message.
19"""
20
21
22class 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 Jansen97df7b62001-06-19 20:20:05 +000042 t = int(time.time() % 1000000)
Fred Drakec02bc3e2001-05-21 20:23:21 +000043 pid = self._counter
44 self._counter += 1
Guido van Rossume2ae77b2001-10-24 20:42:55 +000045 filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
Fred Drakec02bc3e2001-05-21 20:23:21 +000046 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 Peters6626c1f2001-05-22 16:29:01 +000052 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 Drakec02bc3e2001-05-21 20:23:21 +000058 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 Drake0aaed272000-10-23 13:39:15 +000095
96 # XXX We still need more tests!
97
Fred Drakec02bc3e2001-05-21 20:23:21 +000098
Fred Drake2e2be372001-09-20 21:33:42 +000099def test_main():
100 test_support.run_unittest(MaildirTestCase)
101
102
103if __name__ == "__main__":
104 test_main()