diff options
Diffstat (limited to 'src/mailman/config/tests/test_configuration.py')
| -rw-r--r-- | src/mailman/config/tests/test_configuration.py | 60 |
1 files changed, 22 insertions, 38 deletions
diff --git a/src/mailman/config/tests/test_configuration.py b/src/mailman/config/tests/test_configuration.py index abbfce2c2..a969ce586 100644 --- a/src/mailman/config/tests/test_configuration.py +++ b/src/mailman/config/tests/test_configuration.py @@ -17,15 +17,7 @@ """Test the system-wide global configuration.""" -__all__ = [ - 'TestConfiguration', - 'TestConfigurationErrors', - 'TestExternal', - ] - - import os -import tempfile import unittest from contextlib import ExitStack @@ -36,10 +28,10 @@ from mailman.interfaces.configuration import ( from mailman.testing.helpers import configuration, event_subscribers from mailman.testing.layers import ConfigLayer from pkg_resources import resource_filename +from tempfile import NamedTemporaryFile from unittest import mock - class TestConfiguration(unittest.TestCase): layer = ConfigLayer @@ -47,7 +39,7 @@ class TestConfiguration(unittest.TestCase): # Pushing a new configuration onto the stack triggers a # post-processing event. events = [] - def on_event(event): + def on_event(event): # flake8: noqa if isinstance(event, ConfigurationUpdatedEvent): # Record both the event and the top overlay. events.append(event.config.overlays[0].name) @@ -59,22 +51,19 @@ class TestConfiguration(unittest.TestCase): self.assertEqual(events, ['first', 'second', 'first']) def test_config_template_dir_is_source(self): - fd, filename = tempfile.mkstemp() - self.addCleanup(os.remove, filename) - os.close(fd) - with open(filename, 'w') as fp: + config = Configuration() + with NamedTemporaryFile('w', encoding='utf-8') as fp: print("""\ [paths.here] template_dir: :source: """, file=fp) - config = Configuration() - config.load(filename) + fp.flush() + config.load(fp.name) import mailman.templates self.assertEqual(config.TEMPLATE_DIR, os.path.dirname(mailman.templates.__file__)) - class TestExternal(unittest.TestCase): """Test external configuration file loading APIs.""" @@ -104,49 +93,44 @@ class TestExternal(unittest.TestCase): self.assertEqual(cm.exception.path, 'path:mailman.config.missing') - class TestConfigurationErrors(unittest.TestCase): layer = ConfigLayer def test_bad_path_layout_specifier(self): # Using a [mailman]layout name that doesn't exist is a fatal error. - fd, filename = tempfile.mkstemp() - self.addCleanup(os.remove, filename) - os.close(fd) - with open(filename, 'w') as fp: + config = Configuration() + with ExitStack() as resources: + fp = resources.enter_context( + NamedTemporaryFile('w', encoding='utf-8')) print("""\ [mailman] layout: nonesuch """, file=fp) - # Use a fake sys.exit() function that records that it was called, and - # that prevents further processing. - config = Configuration() - # Suppress warning messages in the test output. Also, make sure that - # the config.load() call doesn't break global state. - with ExitStack() as resources: + fp.flush() + # Suppress warning messages in the test output. Also, make sure + # that the config.load() call doesn't break global state. resources.enter_context(mock.patch('sys.stderr')) resources.enter_context(mock.patch.object(config, '_clear')) cm = resources.enter_context(self.assertRaises(SystemExit)) - config.load(filename) + config.load(fp.name) self.assertEqual(cm.exception.args, (1,)) def test_path_expansion_infloop(self): # A path expansion never completes because it references a non-existent # substitution variable. - fd, filename = tempfile.mkstemp() - self.addCleanup(os.remove, filename) - os.close(fd) - with open(filename, 'w') as fp: + config = Configuration() + with ExitStack() as resources: + fp = resources.enter_context( + NamedTemporaryFile('w', encoding='utf-8')) print("""\ [paths.here] log_dir: $nopath/log_dir """, file=fp) - config = Configuration() - # Suppress warning messages in the test output. Also, make sure that - # the config.load() call doesn't break global state. - with ExitStack() as resources: + fp.flush() + # Suppress warning messages in the test output. Also, make sure + # that the config.load() call doesn't break global state. resources.enter_context(mock.patch('sys.stderr')) resources.enter_context(mock.patch.object(config, '_clear')) cm = resources.enter_context(self.assertRaises(SystemExit)) - config.load(filename) + config.load(fp.name) self.assertEqual(cm.exception.args, (1,)) |
