~barry/mailman/lp1423756

« back to all changes in this revision

Viewing changes to src/mailman/config/tests/test_configuration.py

  • Committer: Barry Warsaw
  • Date: 2015-01-05 01:20:33 UTC
  • mfrom: (7264.4.66 py3)
  • Revision ID: barry@list.org-20150105012033-zdrw9c2odhpf22fz
Merge the Python 3 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Test the system-wide global configuration."""
19
19
 
20
 
from __future__ import absolute_import, print_function, unicode_literals
21
 
 
22
 
__metaclass__ = type
23
20
__all__ = [
24
21
    'TestConfiguration',
25
22
    'TestConfigurationErrors',
32
29
import tempfile
33
30
import unittest
34
31
 
 
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."""
67
65
 
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]')
73
70
 
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]')
78
 
 
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]')
84
 
 
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]')
89
74
 
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,))
128
117
 
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)
134
123
        os.close(fd)
135
124
        with open(filename, 'w') as fp:
136
125
            print("""\
137
 
[paths.dev]
 
126
[paths.here]
138
127
log_dir: $nopath/log_dir
139
128
""", file=fp)
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,))