from test
import test_support
os
.unlink(test_support
.TESTFN
)
FROM_
= "From some.body@dummy.domain Sat Jul 24 13:43:35 2004\n"
From: some.body@dummy.domain
class MaildirTestCase(unittest
.TestCase
):
# create a new maildir mailbox to work with:
self
._dir
= test_support
.TESTFN
os
.mkdir(os
.path
.join(self
._dir
, "cur"))
os
.mkdir(os
.path
.join(self
._dir
, "tmp"))
os
.mkdir(os
.path
.join(self
._dir
, "new"))
map(os
.unlink
, self
._msgfiles
)
os
.rmdir(os
.path
.join(self
._dir
, "cur"))
os
.rmdir(os
.path
.join(self
._dir
, "tmp"))
os
.rmdir(os
.path
.join(self
._dir
, "new"))
def createMessage(self
, dir, mbox
=False):
t
= int(time
.time() % 1000000)
filename
= os
.extsep
.join((str(t
), str(pid
), "myhostname", "mydomain"))
tmpname
= os
.path
.join(self
._dir
, "tmp", filename
)
newname
= os
.path
.join(self
._dir
, dir, filename
)
self
._msgfiles
.append(tmpname
)
os
.link(tmpname
, newname
)
self
._msgfiles
.append(newname
)
def test_empty_maildir(self
):
"""Test an empty maildir mailbox"""
# Test for regression on bug #117490:
# Make sure the boxes attribute actually gets set.
self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
self
.assert_(hasattr(self
.mbox
, "boxes"))
self
.assert_(len(self
.mbox
.boxes
) == 0)
self
.assert_(self
.mbox
.next() is None)
self
.assert_(self
.mbox
.next() is None)
def test_nonempty_maildir_cur(self
):
self
.createMessage("cur")
self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
self
.assert_(len(self
.mbox
.boxes
) == 1)
self
.assert_(self
.mbox
.next() is not None)
self
.assert_(self
.mbox
.next() is None)
self
.assert_(self
.mbox
.next() is None)
def test_nonempty_maildir_new(self
):
self
.createMessage("new")
self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
self
.assert_(len(self
.mbox
.boxes
) == 1)
self
.assert_(self
.mbox
.next() is not None)
self
.assert_(self
.mbox
.next() is None)
self
.assert_(self
.mbox
.next() is None)
def test_nonempty_maildir_both(self
):
self
.createMessage("cur")
self
.createMessage("new")
self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
self
.assert_(len(self
.mbox
.boxes
) == 2)
self
.assert_(self
.mbox
.next() is not None)
self
.assert_(self
.mbox
.next() is not None)
self
.assert_(self
.mbox
.next() is None)
self
.assert_(self
.mbox
.next() is None)
def test_unix_mbox(self
):
fname
= self
.createMessage("cur", True)
for msg
in mailbox
.PortableUnixMailbox(open(fname
),
email
.Parser
.Parser().parse
):
self
.assertEqual(msg
["subject"], "Simple Test")
self
.assertEqual(len(str(msg
)), len(FROM_
)+len(DUMMY_MESSAGE
))
# XXX We still need more tests!
test_support
.run_unittest(MaildirTestCase
)
if __name__
== "__main__":