1 # This set of tests exercises the backward-compatibility class
2 # in mailbox.py (the ones without write support).
8 from test
import test_support
10 # cleanup earlier tests
12 os
.unlink(test_support
.TESTFN
)
16 FROM_
= "From some.body@dummy.domain Sat Jul 24 13:43:35 2004\n"
18 From: some.body@dummy.domain
22 This is a dummy message.
25 class MaildirTestCase(unittest
.TestCase
):
28 # create a new maildir mailbox to work with:
29 self
._dir
= test_support
.TESTFN
31 os
.mkdir(os
.path
.join(self
._dir
, "cur"))
32 os
.mkdir(os
.path
.join(self
._dir
, "tmp"))
33 os
.mkdir(os
.path
.join(self
._dir
, "new"))
38 map(os
.unlink
, self
._msgfiles
)
39 os
.rmdir(os
.path
.join(self
._dir
, "cur"))
40 os
.rmdir(os
.path
.join(self
._dir
, "tmp"))
41 os
.rmdir(os
.path
.join(self
._dir
, "new"))
44 def createMessage(self
, dir, mbox
=False):
45 t
= int(time
.time() % 1000000)
48 filename
= os
.extsep
.join((str(t
), str(pid
), "myhostname", "mydomain"))
49 tmpname
= os
.path
.join(self
._dir
, "tmp", filename
)
50 newname
= os
.path
.join(self
._dir
, dir, filename
)
51 fp
= open(tmpname
, "w")
52 self
._msgfiles
.append(tmpname
)
55 fp
.write(DUMMY_MESSAGE
)
57 if hasattr(os
, "link"):
58 os
.link(tmpname
, newname
)
60 fp
= open(newname
, "w")
61 fp
.write(DUMMY_MESSAGE
)
63 self
._msgfiles
.append(newname
)
66 def test_empty_maildir(self
):
67 """Test an empty maildir mailbox"""
68 # Test for regression on bug #117490:
69 self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
70 self
.assertTrue(len(self
.mbox
) == 0)
71 self
.assertTrue(self
.mbox
.next() is None)
72 self
.assertTrue(self
.mbox
.next() is None)
74 def test_nonempty_maildir_cur(self
):
75 self
.createMessage("cur")
76 self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
77 self
.assertTrue(len(self
.mbox
) == 1)
78 self
.assertTrue(self
.mbox
.next() is not None)
79 self
.assertTrue(self
.mbox
.next() is None)
80 self
.assertTrue(self
.mbox
.next() is None)
82 def test_nonempty_maildir_new(self
):
83 self
.createMessage("new")
84 self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
85 self
.assertTrue(len(self
.mbox
) == 1)
86 self
.assertTrue(self
.mbox
.next() is not None)
87 self
.assertTrue(self
.mbox
.next() is None)
88 self
.assertTrue(self
.mbox
.next() is None)
90 def test_nonempty_maildir_both(self
):
91 self
.createMessage("cur")
92 self
.createMessage("new")
93 self
.mbox
= mailbox
.Maildir(test_support
.TESTFN
)
94 self
.assertTrue(len(self
.mbox
) == 2)
95 self
.assertTrue(self
.mbox
.next() is not None)
96 self
.assertTrue(self
.mbox
.next() is not None)
97 self
.assertTrue(self
.mbox
.next() is None)
98 self
.assertTrue(self
.mbox
.next() is None)
100 def test_unix_mbox(self
):
101 ### should be better!
103 fname
= self
.createMessage("cur", True)
105 for msg
in mailbox
.PortableUnixMailbox(open(fname
),
106 email
.parser
.Parser().parse
):
108 self
.assertEqual(msg
["subject"], "Simple Test")
109 self
.assertEqual(len(str(msg
)), len(FROM_
)+len(DUMMY_MESSAGE
))
110 self
.assertEqual(n
, 1)
112 class MboxTestCase(unittest
.TestCase
):
114 # create a new maildir mailbox to work with:
115 self
._path
= test_support
.TESTFN
118 os
.unlink(self
._path
)
120 def test_from_regex (self
):
121 # Testing new regex from bug #1633678
122 f
= open(self
._path
, 'w')
123 f
.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
127 From fred@example.com Mon May 31 13:24:50 2004 -0200
131 From fred@example.com Mon May 31 13:24:50 2004
135 From fred@example.com Mon May 31 13:24:50 2004
141 box
= mailbox
.UnixMailbox(open(self
._path
, 'r'))
142 self
.assertTrue(len(list(iter(box
))) == 4)
145 # XXX We still need more tests!
149 test_support
.run_unittest(MaildirTestCase
, MboxTestCase
)
152 if __name__
== "__main__":