diff options
Diffstat (limited to 'src/mailman/testing/helpers.py')
| -rw-r--r-- | src/mailman/testing/helpers.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/mailman/testing/helpers.py b/src/mailman/testing/helpers.py index 032b028a9..ca0b14b18 100644 --- a/src/mailman/testing/helpers.py +++ b/src/mailman/testing/helpers.py @@ -25,6 +25,7 @@ __all__ = [ 'TestableMaster', 'body_line_iterator', 'call_api', + 'configuration', 'digest_mbox', 'event_subscribers', 'get_lmtp_client', @@ -40,6 +41,7 @@ __all__ = [ import os import json import time +import uuid import errno import signal import socket @@ -68,6 +70,9 @@ from mailman.interfaces.usermanager import IUserManager from mailman.utilities.mailbox import Mailbox +NL = '\n' + + def make_testable_runner(runner_class, name=None, predicate=None): """Create a runner that runs until its queue is empty. @@ -331,6 +336,42 @@ def event_subscribers(*subscribers): +class configuration: + """A decorator/context manager for temporarily setting configurations.""" + + def __init__(self, section, **kws): + self._section = section + self._values = kws.copy() + self._uuid = uuid.uuid4().hex + + def _apply(self): + lines = ['[{0}]'.format(self._section)] + for key, value in self._values.items(): + lines.append('{0}: {1}'.format(key, value)) + config.push(self._uuid, NL.join(lines)) + + def _remove(self): + config.pop(self._uuid) + + def __enter__(self): + self._apply() + + def __exit__(self, *exc_info): + self._remove() + # Do not suppress exceptions. + return False + + def __call__(self, func): + def wrapper(*args, **kws): + self._apply() + try: + return func(*args, **kws) + finally: + self._remove() + return wrapper + + + def subscribe(mlist, first_name, role=MemberRole.member): """Helper for subscribing a sample person to a mailing list.""" user_manager = getUtility(IUserManager) |
