Home | History | Annotate | Download | only in test
      1 # This set of tests exercises the backward-compatibility class
      2 # in mailbox.py (the ones without write support).
      3 
      4 import mailbox
      5 import os
      6 import time
      7 import unittest
      8 from test import test_support
      9 
     10 # cleanup earlier tests
     11 try:
     12     os.unlink(test_support.TESTFN)
     13 except os.error:
     14     pass
     15 
     16 FROM_ = "From some.body (at] dummy.domain  Sat Jul 24 13:43:35 2004\n"
     17 DUMMY_MESSAGE = """\
     18 From: some.body (at] dummy.domain
     19 To: me (at] my.domain
     20 Subject: Simple Test
     21 
     22 This is a dummy message.
     23 """
     24 
     25 class MaildirTestCase(unittest.TestCase):
     26 
     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 = []
     36 
     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)
     43 
     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         with open(tmpname, "w") as fp:
     52             self._msgfiles.append(tmpname)
     53             if mbox:
     54                 fp.write(FROM_)
     55             fp.write(DUMMY_MESSAGE)
     56         if hasattr(os, "link"):
     57             os.link(tmpname, newname)
     58         else:
     59             with open(newname, "w") as fp:
     60                 fp.write(DUMMY_MESSAGE)
     61         self._msgfiles.append(newname)
     62         return tmpname
     63 
     64     def test_empty_maildir(self):
     65         """Test an empty maildir mailbox"""
     66         # Test for regression on bug #117490:
     67         self.mbox = mailbox.Maildir(test_support.TESTFN)
     68         self.assertTrue(len(self.mbox) == 0)
     69         self.assertTrue(self.mbox.next() is None)
     70         self.assertTrue(self.mbox.next() is None)
     71 
     72     def test_nonempty_maildir_cur(self):
     73         self.createMessage("cur")
     74         self.mbox = mailbox.Maildir(test_support.TESTFN)
     75         self.assertTrue(len(self.mbox) == 1)
     76         msg = self.mbox.next()
     77         self.assertTrue(msg is not None)
     78         msg.fp.close()
     79         self.assertTrue(self.mbox.next() is None)
     80         self.assertTrue(self.mbox.next() is None)
     81 
     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         msg = self.mbox.next()
     87         self.assertTrue(msg is not None)
     88         msg.fp.close()
     89         self.assertTrue(self.mbox.next() is None)
     90         self.assertTrue(self.mbox.next() is None)
     91 
     92     def test_nonempty_maildir_both(self):
     93         self.createMessage("cur")
     94         self.createMessage("new")
     95         self.mbox = mailbox.Maildir(test_support.TESTFN)
     96         self.assertTrue(len(self.mbox) == 2)
     97         msg = self.mbox.next()
     98         self.assertTrue(msg is not None)
     99         msg.fp.close()
    100         msg = self.mbox.next()
    101         self.assertTrue(msg is not None)
    102         msg.fp.close()
    103         self.assertTrue(self.mbox.next() is None)
    104         self.assertTrue(self.mbox.next() is None)
    105 
    106     def test_unix_mbox(self):
    107         ### should be better!
    108         import email.parser
    109         fname = self.createMessage("cur", True)
    110         n = 0
    111         with open(fname) as f:
    112             for msg in mailbox.PortableUnixMailbox(f,
    113                                                email.parser.Parser().parse):
    114                 n += 1
    115                 self.assertEqual(msg["subject"], "Simple Test")
    116                 self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
    117         self.assertEqual(n, 1)
    118 
    119 class MboxTestCase(unittest.TestCase):
    120     def setUp(self):
    121         # create a new maildir mailbox to work with:
    122         self._path = test_support.TESTFN
    123 
    124     def tearDown(self):
    125         os.unlink(self._path)
    126 
    127     def test_from_regex (self):
    128         # Testing new regex from bug #1633678
    129         with open(self._path, 'w') as f:
    130             f.write("""From fred (at] example.com Mon May 31 13:24:50 2004 +0200
    131 Subject: message 1
    132 
    133 body1
    134 From fred (at] example.com Mon May 31 13:24:50 2004 -0200
    135 Subject: message 2
    136 
    137 body2
    138 From fred (at] example.com Mon May 31 13:24:50 2004
    139 Subject: message 3
    140 
    141 body3
    142 From fred (at] example.com Mon May 31 13:24:50 2004
    143 Subject: message 4
    144 
    145 body4
    146 """)
    147         with open(self._path, 'r') as f:
    148             box = mailbox.UnixMailbox(f)
    149             self.assertTrue(len(list(iter(box))) == 4)
    150 
    151 
    152     # XXX We still need more tests!
    153 
    154 
    155 def test_main():
    156     test_support.run_unittest(MaildirTestCase, MboxTestCase)
    157 
    158 
    159 if __name__ == "__main__":
    160     test_main()
    161