Skip to content

Commit

Permalink
100% coverage of maildir.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Aug 28, 2017
1 parent 1afd2f4 commit 92e0d37
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/zope/sendmail/maildir.py
Expand Up @@ -68,8 +68,8 @@ def __iter__(self):
if not x.startswith('.')]
# Sort by modification time so earlier messages are sent before
# later messages during queue processing.
msgs_sorted = [(m, os.path.getmtime(m)) for m
in new_messages + cur_messages]
msgs_sorted = [(m, os.path.getmtime(m)) for m
in new_messages + cur_messages]
msgs_sorted.sort(key=lambda x: x[1])
return iter([m[0] for m in msgs_sorted])

Expand Down Expand Up @@ -142,7 +142,7 @@ def commit(self):
raise RuntimeError('Cannot commit, message already aborted')
elif not self._finished:
self._finished = True
self._fd.close()
self.close()
os.rename(self._filename, self._new_filename)
# NOTE: the same maildir.html says it should be a link, followed by
# unlink. But Win32 does not necessarily have hardlinks!
Expand All @@ -154,8 +154,7 @@ def abort(self):
if not self._finished:
self._finished = True
self._aborted = True
self._fd.close()
self.close()
os.unlink(self._filename)

# XXX: should there be a __del__ that calls abort()?

24 changes: 17 additions & 7 deletions src/zope/sendmail/tests/test_maildir.py
Expand Up @@ -22,6 +22,8 @@

from zope.interface.verify import verifyObject

from zope.sendmail.maildir import Maildir
from zope.sendmail.interfaces import IMaildirMessageWriter

class FakeSocketModule(object):

Expand Down Expand Up @@ -185,7 +187,6 @@ def tearDown(self):

def test_factory(self):
from zope.sendmail.interfaces import IMaildirFactory, IMaildir
from zope.sendmail.maildir import Maildir
verifyObject(IMaildirFactory, Maildir)

# Case 1: normal maildir
Expand Down Expand Up @@ -213,7 +214,6 @@ def test_factory(self):
self.assertRaises(ValueError, Maildir, '/path/to/emptydirectory', True)

def test_iteration(self):
from zope.sendmail.maildir import Maildir
m = Maildir('/path/to/maildir')
messages = sorted(m)
self.assertEqual(messages, ['/path/to/maildir/cur/1',
Expand All @@ -222,16 +222,24 @@ def test_iteration(self):
'/path/to/maildir/new/2'])

def test_newMessage(self):
from zope.sendmail.maildir import Maildir
from zope.sendmail.interfaces import IMaildirMessageWriter
m = Maildir('/path/to/maildir')
fd = m.newMessage()
verifyObject(IMaildirMessageWriter, fd)
self.assertTrue(fd._filename.startswith(
'/path/to/maildir/tmp/1234500002.4242.myhostname.'))

def test_newMessage_error(self):
m = Maildir('/path/to/maildir')
def open(*args):
raise OSError(errno.EADDRINUSE, "")
self.fake_os_module.open = open

with self.assertRaises(OSError) as exc:
m.newMessage()

self.assertEqual(exc.exception.errno, errno.EADDRINUSE)

def test_newMessage_never_loops(self):
from zope.sendmail.maildir import Maildir
self.fake_os_module._all_files_exist = True
m = Maildir('/path/to/maildir')
self.assertRaises(RuntimeError, m.newMessage)
Expand All @@ -250,7 +258,7 @@ def test_message_writer_and_abort(self):
self.assertEqual(writer._fd._written, b'fee fie foe foo')

writer.abort()
self.assertEqual(writer._fd._closed, True)
self.assertTrue(writer._fd._closed)
self.assertTrue(filename1 in self.fake_os_module._removed_files)
# Once aborted, abort does nothing
self.fake_os_module._removed_files = ()
Expand All @@ -267,7 +275,7 @@ def test_message_writer_commit(self):
fd = FakeFile(filename1, 'w')
writer = MaildirMessageWriter(fd, filename1, filename2)
writer.commit()
self.assertEqual(writer._fd._closed, True)
self.assertTrue(writer._fd._closed)
self.assertIn((filename1, filename2),
self.fake_os_module._renamed_files)
# Once commited, commit does nothing
Expand All @@ -293,6 +301,8 @@ def test_message_writer_unicode(self):
writer.writelines([u' fo\xe8', u' fo\xf2'])
self.assertEqual(writer._fd._written,
b'fe\xc3\xa8 fi\xc3\xa8 fo\xc3\xa8 fo\xc3\xb2')
writer.close()
self.assertTrue(writer._fd._closed)


def test_suite():
Expand Down

0 comments on commit 92e0d37

Please sign in to comment.