13 @pytest.fixture(scope='function')
15 """The tmpdir fixture wrapped in pathlib.Path."""
16 return pathlib.Path(str(tmpdir))
21 """Return a function which runs notmuch commands on our test maildir.
23 This uses the notmuch-config file created by the ``maildir``
27 """Run a notmuch comand.
29 This function runs with a timeout error as many notmuch
30 commands may block if multiple processes are trying to open
31 the database in write-mode. It is all too easy to
32 accidentally do this in the unittests.
34 cfg_fname = maildir.path / 'notmuch-config'
35 cmd = ['notmuch'] + list(args)
36 env = os.environ.copy()
37 env['NOTMUCH_CONFIG'] = str(cfg_fname)
38 proc = subprocess.run(cmd,
41 proc.check_returncode()
47 """A basic test interface to a valid maildir directory.
49 This creates a valid maildir and provides a simple mechanism to
50 deliver test emails to it. It also writes a notmuch-config file
51 in the top of the maildir.
59 cfg_fname = tmppath/'notmuch-config'
60 with cfg_fname.open('w') as fp:
61 fp.write(textwrap.dedent("""\
66 primary_email=dst@example.com
71 exclude_tags=deleted;spam;
73 synchronize_flags=true
76 """.format(tmppath=tmppath)))
77 return MailDir(tmppath)
81 """An interface around a correct maildir."""
83 def __init__(self, path):
84 self._path = pathlib.Path(path)
85 self.mailbox = mailbox.Maildir(str(path))
90 """The pathname of the maildir."""
93 def _next_msgid(self):
94 """Return a new unique message ID."""
95 msgid = '{}@{}'.format(self._idcount, socket.getfqdn())
101 body='This is a test mail',
102 to='dst@example.com',
103 frm='src@example.com',
105 new=False, # Move to new dir or cur dir?
106 keywords=None, # List of keywords or labels
107 seen=False, # Seen flag (cur dir only)
108 replied=False, # Replied flag (cur dir only)
109 flagged=False): # Flagged flag (cur dir only)
110 """Deliver a new mail message in the mbox.
112 This does only adds the message to maildir, does not insert it
113 into the notmuch database.
115 :returns: A tuple of (msgid, pathname).
117 msgid = self._next_msgid()
119 msg = email.message.EmailMessage()
120 msg.add_header('Received', 'by MailDir; {}'.format(time.ctime(when)))
121 msg.add_header('Message-ID', '<{}>'.format(msgid))
122 msg.add_header('Date', time.ctime(when))
123 msg.add_header('From', frm)
124 msg.add_header('To', to)
125 msg.add_header('Subject', subject)
129 msg.set_content(body)
130 mdmsg = mailbox.MaildirMessage(msg)
132 mdmsg.set_subdir('cur')
139 boxid = self.mailbox.add(mdmsg)
142 basename += mailbox.Maildir.colon + mdmsg.get_info()
143 msgpath = self.path / mdmsg.get_subdir() / basename
144 return (msgid, msgpath)