/edk2/AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_old_mailbox.py

https://gitlab.com/envieidoc/Clover · Python · 152 lines · 121 code · 21 blank · 10 comment · 12 complexity · 4cf0e533c6c9ff669830f4ef017b23b0 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. with open(tmpname, "w") as fp:
  44. self._msgfiles.append(tmpname)
  45. if mbox:
  46. fp.write(FROM_)
  47. fp.write(DUMMY_MESSAGE)
  48. if hasattr(os, "link"):
  49. os.link(tmpname, newname)
  50. else:
  51. with open(newname, "w") as fp:
  52. fp.write(DUMMY_MESSAGE)
  53. self._msgfiles.append(newname)
  54. return tmpname
  55. def test_empty_maildir(self):
  56. """Test an empty maildir mailbox"""
  57. # Test for regression on bug #117490:
  58. self.mbox = mailbox.Maildir(test_support.TESTFN)
  59. self.assertTrue(len(self.mbox) == 0)
  60. self.assertTrue(self.mbox.next() is None)
  61. self.assertTrue(self.mbox.next() is None)
  62. def test_nonempty_maildir_cur(self):
  63. self.createMessage("cur")
  64. self.mbox = mailbox.Maildir(test_support.TESTFN)
  65. self.assertTrue(len(self.mbox) == 1)
  66. self.assertTrue(self.mbox.next() is not None)
  67. self.assertTrue(self.mbox.next() is None)
  68. self.assertTrue(self.mbox.next() is None)
  69. def test_nonempty_maildir_new(self):
  70. self.createMessage("new")
  71. self.mbox = mailbox.Maildir(test_support.TESTFN)
  72. self.assertTrue(len(self.mbox) == 1)
  73. self.assertTrue(self.mbox.next() is not None)
  74. self.assertTrue(self.mbox.next() is None)
  75. self.assertTrue(self.mbox.next() is None)
  76. def test_nonempty_maildir_both(self):
  77. self.createMessage("cur")
  78. self.createMessage("new")
  79. self.mbox = mailbox.Maildir(test_support.TESTFN)
  80. self.assertTrue(len(self.mbox) == 2)
  81. self.assertTrue(self.mbox.next() is not None)
  82. self.assertTrue(self.mbox.next() is not None)
  83. self.assertTrue(self.mbox.next() is None)
  84. self.assertTrue(self.mbox.next() is None)
  85. def test_unix_mbox(self):
  86. ### should be better!
  87. import email.parser
  88. fname = self.createMessage("cur", True)
  89. n = 0
  90. with open(fname) as f:
  91. for msg in mailbox.PortableUnixMailbox(f,
  92. email.parser.Parser().parse):
  93. n += 1
  94. self.assertEqual(msg["subject"], "Simple Test")
  95. self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
  96. self.assertEqual(n, 1)
  97. class MboxTestCase(unittest.TestCase):
  98. def setUp(self):
  99. # create a new maildir mailbox to work with:
  100. self._path = test_support.TESTFN
  101. def tearDown(self):
  102. os.unlink(self._path)
  103. def test_from_regex (self):
  104. # Testing new regex from bug #1633678
  105. with open(self._path, 'w') as f:
  106. f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
  107. Subject: message 1
  108. body1
  109. From fred@example.com Mon May 31 13:24:50 2004 -0200
  110. Subject: message 2
  111. body2
  112. From fred@example.com Mon May 31 13:24:50 2004
  113. Subject: message 3
  114. body3
  115. From fred@example.com Mon May 31 13:24:50 2004
  116. Subject: message 4
  117. body4
  118. """)
  119. with open(self._path, 'r') as f:
  120. box = mailbox.UnixMailbox(f)
  121. self.assertTrue(len(list(iter(box))) == 4)
  122. # XXX We still need more tests!
  123. def test_main():
  124. test_support.run_unittest(MaildirTestCase, MboxTestCase)
  125. if __name__ == "__main__":
  126. test_main()