PageRenderTime 116ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/test/test_old_mailbox.py

https://github.com/rpattabi/ironruby
Python | 160 lines | 154 code | 3 blank | 3 comment | 2 complexity | ee6f741102534f72e09ccd7eb5afc3ec 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. test_support.force_gc_collect("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793")
  32. map(os.unlink, self._msgfiles)
  33. os.rmdir(os.path.join(self._dir, "cur"))
  34. os.rmdir(os.path.join(self._dir, "tmp"))
  35. os.rmdir(os.path.join(self._dir, "new"))
  36. os.rmdir(self._dir)
  37. def createMessage(self, dir, mbox=False):
  38. t = int(time.time() % 1000000)
  39. pid = self._counter
  40. self._counter += 1
  41. filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
  42. tmpname = os.path.join(self._dir, "tmp", filename)
  43. newname = os.path.join(self._dir, dir, filename)
  44. fp = open(tmpname, "w")
  45. self._msgfiles.append(tmpname)
  46. if mbox:
  47. fp.write(FROM_)
  48. fp.write(DUMMY_MESSAGE)
  49. fp.close()
  50. if hasattr(os, "link"):
  51. os.link(tmpname, newname)
  52. else:
  53. fp = open(newname, "w")
  54. fp.write(DUMMY_MESSAGE)
  55. fp.close()
  56. self._msgfiles.append(newname)
  57. return tmpname
  58. def test_empty_maildir(self):
  59. """Test an empty maildir mailbox"""
  60. # Test for regression on bug #117490:
  61. self.mbox = mailbox.Maildir(test_support.TESTFN)
  62. self.assertTrue(len(self.mbox) == 0)
  63. self.assertTrue(self.mbox.next() is None)
  64. self.assertTrue(self.mbox.next() is None)
  65. def test_nonempty_maildir_cur(self):
  66. self.createMessage("cur")
  67. self.mbox = mailbox.Maildir(test_support.TESTFN)
  68. self.assertTrue(len(self.mbox) == 1)
  69. self.assertTrue(self.mbox.next() is not None)
  70. self.assertTrue(self.mbox.next() is None)
  71. self.assertTrue(self.mbox.next() is None)
  72. def test_nonempty_maildir_new(self):
  73. self.createMessage("new")
  74. self.mbox = mailbox.Maildir(test_support.TESTFN)
  75. self.assertTrue(len(self.mbox) == 1)
  76. self.assertTrue(self.mbox.next() is not None)
  77. self.assertTrue(self.mbox.next() is None)
  78. self.assertTrue(self.mbox.next() is None)
  79. def test_nonempty_maildir_both(self):
  80. self.createMessage("cur")
  81. self.createMessage("new")
  82. self.mbox = mailbox.Maildir(test_support.TESTFN)
  83. self.assertTrue(len(self.mbox) == 2)
  84. self.assertTrue(self.mbox.next() is not None)
  85. self.assertTrue(self.mbox.next() is not None)
  86. self.assertTrue(self.mbox.next() is None)
  87. self.assertTrue(self.mbox.next() is None)
  88. def test_unix_mbox(self):
  89. ### should be better!
  90. import email.parser
  91. fname = self.createMessage("cur", True)
  92. n = 0
  93. for msg in mailbox.PortableUnixMailbox(open(fname),
  94. email.parser.Parser().parse):
  95. n += 1
  96. self.assertEqual(msg["subject"], "Simple Test")
  97. self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
  98. self.assertEqual(n, 1)
  99. class MboxTestCase(unittest.TestCase):
  100. def setUp(self):
  101. # create a new maildir mailbox to work with:
  102. self._path = test_support.TESTFN
  103. def tearDown(self):
  104. os.unlink(self._path)
  105. def test_from_regex (self):
  106. # Testing new regex from bug #1633678
  107. f = open(self._path, 'w')
  108. f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
  109. Subject: message 1
  110. body1
  111. From fred@example.com Mon May 31 13:24:50 2004 -0200
  112. Subject: message 2
  113. body2
  114. From fred@example.com Mon May 31 13:24:50 2004
  115. Subject: message 3
  116. body3
  117. From fred@example.com Mon May 31 13:24:50 2004
  118. Subject: message 4
  119. body4
  120. """)
  121. f.close()
  122. if test_support.due_to_ironpython_incompatibility("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793"):
  123. with open(self._path, 'r') as f:
  124. box = mailbox.UnixMailbox(f)
  125. self.assert_(len(list(iter(box))) == 4)
  126. else:
  127. box = mailbox.UnixMailbox(open(self._path, 'r'))
  128. self.assertTrue(len(list(iter(box))) == 4)
  129. # XXX We still need more tests!
  130. def test_main():
  131. test_support.run_unittest(MaildirTestCase, MboxTestCase)
  132. if __name__ == "__main__":
  133. test_main()