18
18
"""Test the system-wide global configuration."""
20
from __future__ import absolute_import, print_function, unicode_literals
24
21
'TestConfiguration',
25
22
'TestConfigurationErrors',
32
from contextlib import ExitStack
35
33
from mailman.config.config import (
36
34
Configuration, external_configuration, load_external)
37
35
from mailman.interfaces.configuration import (
65
63
class TestExternal(unittest.TestCase):
66
64
"""Test external configuration file loading APIs."""
68
def test_load_external_by_filename_as_bytes(self):
66
def test_load_external_by_filename(self):
69
67
filename = resource_filename('mailman.config', 'postfix.cfg')
70
68
contents = load_external(filename)
71
self.assertIsInstance(contents, bytes)
72
self.assertEqual(contents[:9], b'[postfix]')
69
self.assertEqual(contents[:9], '[postfix]')
74
def test_load_external_by_path_as_bytes(self):
71
def test_load_external_by_path(self):
75
72
contents = load_external('python:mailman.config.postfix')
76
self.assertIsInstance(contents, bytes)
77
self.assertEqual(contents[:9], b'[postfix]')
79
def test_load_external_by_filename_as_string(self):
80
filename = resource_filename('mailman.config', 'postfix.cfg')
81
contents = load_external(filename, encoding='utf-8')
82
self.assertIsInstance(contents, unicode)
83
self.assertEqual(contents[:9], '[postfix]')
85
def test_load_external_by_path_as_string(self):
86
contents = load_external('python:mailman.config.postfix', 'utf-8')
87
self.assertIsInstance(contents, unicode)
88
73
self.assertEqual(contents[:9], '[postfix]')
90
75
def test_external_configuration_by_filename(self):
121
106
# Use a fake sys.exit() function that records that it was called, and
122
107
# that prevents further processing.
123
108
config = Configuration()
124
# Suppress warning messages in the test output.
125
with self.assertRaises(SystemExit) as cm, mock.patch('sys.stderr'):
109
# Suppress warning messages in the test output. Also, make sure that
110
# the config.load() call doesn't break global state.
111
with ExitStack() as resources:
112
resources.enter_context(mock.patch('sys.stderr'))
113
resources.enter_context(mock.patch.object(config, '_clear'))
114
cm = resources.enter_context(self.assertRaises(SystemExit))
126
115
config.load(filename)
127
116
self.assertEqual(cm.exception.args, (1,))
129
118
def test_path_expansion_infloop(self):
130
# A path expansion never completes because it references a
131
# non-existent substitution variable.
119
# A path expansion never completes because it references a non-existent
120
# substitution variable.
132
121
fd, filename = tempfile.mkstemp()
133
122
self.addCleanup(os.remove, filename)
135
124
with open(filename, 'w') as fp:
138
127
log_dir: $nopath/log_dir
140
129
config = Configuration()
141
# Suppress warning messages in the test output.
142
with self.assertRaises(SystemExit) as cm, mock.patch('sys.stderr'):
130
# Suppress warning messages in the test output. Also, make sure that
131
# the config.load() call doesn't break global state.
132
with ExitStack() as resources:
133
resources.enter_context(mock.patch('sys.stderr'))
134
resources.enter_context(mock.patch.object(config, '_clear'))
135
cm = resources.enter_context(self.assertRaises(SystemExit))
143
136
config.load(filename)
144
137
self.assertEqual(cm.exception.args, (1,))