Issue #7295: Do not use a hardcoded file name in test_tarfile.
[python.git] / Lib / test / test_old_mailbox.py
blobc1bebaf2a11bae7386de8855fbf193e48696ff03
1 # This set of tests exercises the backward-compatibility class
2 # in mailbox.py (the ones without write support).
4 import mailbox
5 import os
6 import time
7 import unittest
8 from test import test_support
10 # cleanup earlier tests
11 try:
12 os.unlink(test_support.TESTFN)
13 except os.error:
14 pass
16 FROM_ = "From some.body@dummy.domain Sat Jul 24 13:43:35 2004\n"
17 DUMMY_MESSAGE = """\
18 From: some.body@dummy.domain
19 To: me@my.domain
20 Subject: Simple Test
22 This is a dummy message.
23 """
25 class MaildirTestCase(unittest.TestCase):
27 def setUp(self):
28 # create a new maildir mailbox to work with:
29 self._dir = test_support.TESTFN
30 os.mkdir(self._dir)
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"))
34 self._counter = 1
35 self._msgfiles = []
37 def tearDown(self):
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"))
42 os.rmdir(self._dir)
44 def createMessage(self, dir, mbox=False):
45 t = int(time.time() % 1000000)
46 pid = self._counter
47 self._counter += 1
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)
53 if mbox:
54 fp.write(FROM_)
55 fp.write(DUMMY_MESSAGE)
56 fp.close()
57 if hasattr(os, "link"):
58 os.link(tmpname, newname)
59 else:
60 fp = open(newname, "w")
61 fp.write(DUMMY_MESSAGE)
62 fp.close()
63 self._msgfiles.append(newname)
64 return tmpname
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!
102 import email.parser
103 fname = self.createMessage("cur", True)
104 n = 0
105 for msg in mailbox.PortableUnixMailbox(open(fname),
106 email.parser.Parser().parse):
107 n += 1
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):
113 def setUp(self):
114 # create a new maildir mailbox to work with:
115 self._path = test_support.TESTFN
117 def tearDown(self):
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
124 Subject: message 1
126 body1
127 From fred@example.com Mon May 31 13:24:50 2004 -0200
128 Subject: message 2
130 body2
131 From fred@example.com Mon May 31 13:24:50 2004
132 Subject: message 3
134 body3
135 From fred@example.com Mon May 31 13:24:50 2004
136 Subject: message 4
138 body4
139 """)
140 f.close()
141 box = mailbox.UnixMailbox(open(self._path, 'r'))
142 self.assertTrue(len(list(iter(box))) == 4)
145 # XXX We still need more tests!
148 def test_main():
149 test_support.run_unittest(MaildirTestCase, MboxTestCase)
152 if __name__ == "__main__":
153 test_main()