From 664d7d73e13b649f003f51a727bf39fdfdc2ab65 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Sun, 1 Apr 2012 10:55:52 -0600 Subject: - More tests of mailman.runners.nntp.prepare_message(), this time of the header removal and de-duplication algorithms. - Improve the robustness of the de-duplication code. - Add a very nice helper called `configuration` which can be used either as a decorator or context manager to temporarily set values in a configuration section. No more need to manage pushes/pops. More code needs to be converted to use this style. --- src/mailman/testing/helpers.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/mailman/testing/helpers.py') 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. @@ -330,6 +335,42 @@ def event_subscribers(*subscribers): event.subscribers[:] = old_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.""" -- cgit v1.2.3-70-g09d2