Implement #7944. Use `with` throughout the test suite.

This commit is contained in:
Brian Curtin 2010-10-14 02:06:55 +00:00
parent 6ec1eb8e73
commit 4b09b04b4d
1 changed files with 17 additions and 18 deletions

View File

@ -48,18 +48,16 @@ class MaildirTestCase(unittest.TestCase):
filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
tmpname = os.path.join(self._dir, "tmp", filename)
newname = os.path.join(self._dir, dir, filename)
fp = open(tmpname, "w")
self._msgfiles.append(tmpname)
if mbox:
fp.write(FROM_)
fp.write(DUMMY_MESSAGE)
fp.close()
with open(tmpname, "w") as fp:
self._msgfiles.append(tmpname)
if mbox:
fp.write(FROM_)
fp.write(DUMMY_MESSAGE)
if hasattr(os, "link"):
os.link(tmpname, newname)
else:
fp = open(newname, "w")
fp.write(DUMMY_MESSAGE)
fp.close()
with open(newname, "w") as fp:
fp.write(DUMMY_MESSAGE)
self._msgfiles.append(newname)
return tmpname
@ -102,11 +100,12 @@ class MaildirTestCase(unittest.TestCase):
import email.parser
fname = self.createMessage("cur", True)
n = 0
for msg in mailbox.PortableUnixMailbox(open(fname),
with open(fname) as f:
for msg in mailbox.PortableUnixMailbox(f,
email.parser.Parser().parse):
n += 1
self.assertEqual(msg["subject"], "Simple Test")
self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
n += 1
self.assertEqual(msg["subject"], "Simple Test")
self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
self.assertEqual(n, 1)
class MboxTestCase(unittest.TestCase):
@ -119,8 +118,8 @@ class MboxTestCase(unittest.TestCase):
def test_from_regex (self):
# Testing new regex from bug #1633678
f = open(self._path, 'w')
f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
with open(self._path, 'w') as f:
f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
Subject: message 1
body1
@ -137,9 +136,9 @@ Subject: message 4
body4
""")
f.close()
box = mailbox.UnixMailbox(open(self._path, 'r'))
self.assertTrue(len(list(iter(box))) == 4)
with open(self._path, 'r') as f:
box = mailbox.UnixMailbox(f)
self.assertTrue(len(list(iter(box))) == 4)
# XXX We still need more tests!