PageRenderTime 101ms CodeModel.GetById 35ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_old_mailbox.py

http://unladen-swallow.googlecode.com/
Python | 153 lines | 147 code | 3 blank | 3 comment | 0 complexity | 18a045475bfec80cba1c26796ef8727d MD5 | raw file
  1# This set of tests exercises the backward-compatibility class
  2# in mailbox.py (the ones without write support).
  3
  4import mailbox
  5import os
  6import time
  7import unittest
  8from test import test_support
  9
 10# cleanup earlier tests
 11try:
 12    os.unlink(test_support.TESTFN)
 13except os.error:
 14    pass
 15
 16FROM_ = "From some.body@dummy.domain  Sat Jul 24 13:43:35 2004\n"
 17DUMMY_MESSAGE = """\
 18From: some.body@dummy.domain
 19To: me@my.domain
 20Subject: Simple Test
 21
 22This is a dummy message.
 23"""
 24
 25class 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        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
 65
 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.assert_(len(self.mbox) == 0)
 71        self.assert_(self.mbox.next() is None)
 72        self.assert_(self.mbox.next() is None)
 73
 74    def test_nonempty_maildir_cur(self):
 75        self.createMessage("cur")
 76        self.mbox = mailbox.Maildir(test_support.TESTFN)
 77        self.assert_(len(self.mbox) == 1)
 78        self.assert_(self.mbox.next() is not None)
 79        self.assert_(self.mbox.next() is None)
 80        self.assert_(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.assert_(len(self.mbox) == 1)
 86        self.assert_(self.mbox.next() is not None)
 87        self.assert_(self.mbox.next() is None)
 88        self.assert_(self.mbox.next() is None)
 89
 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.assert_(len(self.mbox) == 2)
 95        self.assert_(self.mbox.next() is not None)
 96        self.assert_(self.mbox.next() is not None)
 97        self.assert_(self.mbox.next() is None)
 98        self.assert_(self.mbox.next() is None)
 99
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)
111
112class MboxTestCase(unittest.TestCase):
113    def setUp(self):
114        # create a new maildir mailbox to work with:
115        self._path = test_support.TESTFN
116
117    def tearDown(self):
118        os.unlink(self._path)
119
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
124Subject: message 1
125
126body1
127From fred@example.com Mon May 31 13:24:50 2004 -0200
128Subject: message 2
129
130body2
131From fred@example.com Mon May 31 13:24:50 2004
132Subject: message 3
133
134body3
135From fred@example.com Mon May 31 13:24:50 2004
136Subject: message 4
137
138body4
139""")
140        f.close()
141        box = mailbox.UnixMailbox(open(self._path, 'r'))
142        self.assert_(len(list(iter(box))) == 4)
143
144
145    # XXX We still need more tests!
146
147
148def test_main():
149    test_support.run_unittest(MaildirTestCase, MboxTestCase)
150
151
152if __name__ == "__main__":
153    test_main()