/apps/autodock_mgl.bak/autodock_mgl_1.125_i686-pc-linux-gnu/test/lib/python2.5/test/test_old_mailbox.py

https://github.com/jackygrahamez/DrugDiscovery-Home · Python · 153 lines · 122 code · 21 blank · 10 comment · 7 complexity · 3098b14fef7921ee831237d4b9cd24ae MD5 · raw file

  1. # This set of tests exercises the backward-compatibility class
  2. # in mailbox.py (the ones without write support).
  3. import mailbox
  4. import os
  5. import time
  6. import unittest
  7. from test import test_support
  8. # cleanup earlier tests
  9. try:
  10. os.unlink(test_support.TESTFN)
  11. except os.error:
  12. pass
  13. FROM_ = "From some.body@dummy.domain Sat Jul 24 13:43:35 2004\n"
  14. DUMMY_MESSAGE = """\
  15. From: some.body@dummy.domain
  16. To: me@my.domain
  17. Subject: Simple Test
  18. This is a dummy message.
  19. """
  20. class MaildirTestCase(unittest.TestCase):
  21. def setUp(self):
  22. # create a new maildir mailbox to work with:
  23. self._dir = test_support.TESTFN
  24. os.mkdir(self._dir)
  25. os.mkdir(os.path.join(self._dir, "cur"))
  26. os.mkdir(os.path.join(self._dir, "tmp"))
  27. os.mkdir(os.path.join(self._dir, "new"))
  28. self._counter = 1
  29. self._msgfiles = []
  30. def tearDown(self):
  31. map(os.unlink, self._msgfiles)
  32. os.rmdir(os.path.join(self._dir, "cur"))
  33. os.rmdir(os.path.join(self._dir, "tmp"))
  34. os.rmdir(os.path.join(self._dir, "new"))
  35. os.rmdir(self._dir)
  36. def createMessage(self, dir, mbox=False):
  37. t = int(time.time() % 1000000)
  38. pid = self._counter
  39. self._counter += 1
  40. filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
  41. tmpname = os.path.join(self._dir, "tmp", filename)
  42. newname = os.path.join(self._dir, dir, filename)
  43. fp = open(tmpname, "w")
  44. self._msgfiles.append(tmpname)
  45. if mbox:
  46. fp.write(FROM_)
  47. fp.write(DUMMY_MESSAGE)
  48. fp.close()
  49. if hasattr(os, "link"):
  50. os.link(tmpname, newname)
  51. else:
  52. fp = open(newname, "w")
  53. fp.write(DUMMY_MESSAGE)
  54. fp.close()
  55. self._msgfiles.append(newname)
  56. return tmpname
  57. def test_empty_maildir(self):
  58. """Test an empty maildir mailbox"""
  59. # Test for regression on bug #117490:
  60. self.mbox = mailbox.Maildir(test_support.TESTFN)
  61. self.assert_(len(self.mbox) == 0)
  62. self.assert_(self.mbox.next() is None)
  63. self.assert_(self.mbox.next() is None)
  64. def test_nonempty_maildir_cur(self):
  65. self.createMessage("cur")
  66. self.mbox = mailbox.Maildir(test_support.TESTFN)
  67. self.assert_(len(self.mbox) == 1)
  68. self.assert_(self.mbox.next() is not None)
  69. self.assert_(self.mbox.next() is None)
  70. self.assert_(self.mbox.next() is None)
  71. def test_nonempty_maildir_new(self):
  72. self.createMessage("new")
  73. self.mbox = mailbox.Maildir(test_support.TESTFN)
  74. self.assert_(len(self.mbox) == 1)
  75. self.assert_(self.mbox.next() is not None)
  76. self.assert_(self.mbox.next() is None)
  77. self.assert_(self.mbox.next() is None)
  78. def test_nonempty_maildir_both(self):
  79. self.createMessage("cur")
  80. self.createMessage("new")
  81. self.mbox = mailbox.Maildir(test_support.TESTFN)
  82. self.assert_(len(self.mbox) == 2)
  83. self.assert_(self.mbox.next() is not None)
  84. self.assert_(self.mbox.next() is not None)
  85. self.assert_(self.mbox.next() is None)
  86. self.assert_(self.mbox.next() is None)
  87. def test_unix_mbox(self):
  88. ### should be better!
  89. import email.Parser
  90. fname = self.createMessage("cur", True)
  91. n = 0
  92. for msg in mailbox.PortableUnixMailbox(open(fname),
  93. email.Parser.Parser().parse):
  94. n += 1
  95. self.assertEqual(msg["subject"], "Simple Test")
  96. self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
  97. self.assertEqual(n, 1)
  98. class MboxTestCase(unittest.TestCase):
  99. def setUp(self):
  100. # create a new maildir mailbox to work with:
  101. self._path = test_support.TESTFN
  102. def tearDown(self):
  103. os.unlink(self._path)
  104. def test_from_regex (self):
  105. # Testing new regex from bug #1633678
  106. f = open(self._path, 'w')
  107. f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
  108. Subject: message 1
  109. body1
  110. From fred@example.com Mon May 31 13:24:50 2004 -0200
  111. Subject: message 2
  112. body2
  113. From fred@example.com Mon May 31 13:24:50 2004
  114. Subject: message 3
  115. body3
  116. From fred@example.com Mon May 31 13:24:50 2004
  117. Subject: message 4
  118. body4
  119. """)
  120. f.close()
  121. box = mailbox.UnixMailbox(open(self._path, 'r'))
  122. self.assert_(len(list(iter(box))) == 4)
  123. # XXX We still need more tests!
  124. def test_main():
  125. test_support.run_unittest(MaildirTestCase, MboxTestCase)
  126. if __name__ == "__main__":
  127. test_main()