Add tests for frontend.validate_comma_separated_list and frontend.validate_colon_sepa...
[docutils.git] / test / test_settings.py
blobfa81f430730d06ab485a11ecb9d5ad1c6617c7bb
1 #!/usr/bin/env python
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests of runtime settings.
9 """
11 import sys
12 import os
13 import difflib
14 import pprint
15 import warnings
16 import unittest
17 import DocutilsTestSupport # must be imported before docutils
18 from docutils import frontend, utils
19 from docutils.writers import html4css1, pep_html
20 from docutils.parsers import rst
23 warnings.filterwarnings(action='ignore',
24 category=frontend.ConfigDeprecationWarning)
27 def fixpath(path):
28 return os.path.abspath(os.path.join(*(path.split('/'))))
31 class ConfigFileTests(unittest.TestCase):
33 config_files = {'old': fixpath('data/config_old.txt'),
34 'one': fixpath('data/config_1.txt'),
35 'two': fixpath('data/config_2.txt'),
36 'list': fixpath('data/config_list.txt'),
37 'list2': fixpath('data/config_list_2.txt'),
38 'error': fixpath('data/config_error_handler.txt')}
40 settings = {
41 'old': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
42 u'generator': True,
43 u'no_random': True,
44 u'python_home': u'http://www.python.org',
45 u'source_link': True,
46 'stylesheet': None,
47 u'stylesheet_path': [fixpath(u'data/stylesheets/pep.css')],
48 'template': fixpath(u'data/pep-html-template')},
49 'one': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
50 u'generator': True,
51 u'no_random': True,
52 u'python_home': u'http://www.python.org',
53 u'raw_enabled': False,
54 'record_dependencies': utils.DependencyList(),
55 u'source_link': True,
56 'stylesheet': None,
57 u'stylesheet_path': [fixpath(u'data/stylesheets/pep.css')],
58 u'tab_width': 8,
59 u'template': fixpath(u'data/pep-html-template'),
60 u'trim_footnote_reference_space': True,
62 'two': {u'footnote_references': u'superscript',
63 u'generator': False,
64 'record_dependencies': utils.DependencyList(),
65 u'stylesheet': None,
66 u'stylesheet_path': [fixpath(u'data/test.css')],
67 'trim_footnote_reference_space': None},
68 'list': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e'],
69 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot'],
70 u'strip_elements_with_classes': [u'sugar', u'flour', u'milk',
71 u'safran']},
72 'list2': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e', u'f'],
73 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot',
74 u'ham', u'eggs'],
75 u'strip_elements_with_classes': [u'sugar', u'flour',
76 u'milk', u'safran',
77 u'eggs', u'salt']},
78 'error': {u'error_encoding': u'ascii',
79 u'error_encoding_error_handler': u'strict'},
82 compare = difflib.Differ().compare
83 """Comparison method shared by all tests."""
85 def setUp(self):
86 self.option_parser = frontend.OptionParser(
87 components=(pep_html.Writer, rst.Parser), read_config_files=None)
89 def files_settings(self, *names):
90 settings = frontend.Values()
91 for name in names:
92 settings.update(self.option_parser.get_config_file_settings(
93 self.config_files[name]), self.option_parser)
94 return settings.__dict__
96 def expected_settings(self, *names):
97 expected = {}
98 for name in names:
99 expected.update(self.settings[name])
100 return expected
102 def compare_output(self, result, expected):
103 """`result` and `expected` should both be dicts."""
104 self.assertTrue('record_dependencies' in result)
105 if 'record_dependencies' not in expected:
106 # Delete it if we don't want to test it.
107 del result['record_dependencies']
108 result = pprint.pformat(result) + '\n'
109 expected = pprint.pformat(expected) + '\n'
110 try:
111 self.assertEqual(result, expected)
112 except AssertionError:
113 print >>sys.stderr, '\n%s\n' % (self,)
114 print >>sys.stderr, '-: expected\n+: result'
115 print >>sys.stderr, ''.join(self.compare(expected.splitlines(1),
116 result.splitlines(1)))
117 raise
119 def test_nofiles(self):
120 self.compare_output(self.files_settings(),
121 self.expected_settings())
123 def test_old(self):
124 self.compare_output(self.files_settings('old'),
125 self.expected_settings('old'))
127 def test_one(self):
128 self.compare_output(self.files_settings('one'),
129 self.expected_settings('one'))
131 def test_multiple(self):
132 self.compare_output(self.files_settings('one', 'two'),
133 self.expected_settings('one', 'two'))
135 def test_old_and_new(self):
136 self.compare_output(self.files_settings('old', 'two'),
137 self.expected_settings('old', 'two'))
139 def test_list(self):
140 self.compare_output(self.files_settings('list'),
141 self.expected_settings('list'))
143 def test_list2(self):
144 self.compare_output(self.files_settings('list', 'list2'),
145 self.expected_settings('list2'))
147 def test_error_handler(self):
148 self.compare_output(self.files_settings('error'),
149 self.expected_settings('error'))
152 class ConfigEnvVarFileTests(ConfigFileTests):
155 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
156 environment variable and the standard Docutils config file mechanism.
159 def setUp(self):
160 ConfigFileTests.setUp(self)
161 self.orig_environ = os.environ
162 os.environ = os.environ.copy()
164 def files_settings(self, *names):
165 files = [self.config_files[name] for name in names]
166 os.environ['DOCUTILSCONFIG'] = os.pathsep.join(files)
167 settings = self.option_parser.get_standard_config_settings()
168 return settings.__dict__
170 def tearDown(self):
171 os.environ = self.orig_environ
174 class HelperFunctionsTests(unittest.TestCase):
176 pathdict = {'foo': 'hallo', 'ham': u'h\xE4m', 'spam': u'spam'}
177 keys = ['foo', 'ham']
179 def test_make_paths_absolute(self):
180 pathdict = self.pathdict.copy()
181 frontend.make_paths_absolute(pathdict, self.keys, base_path='base')
182 self.assertEqual(pathdict['foo'], os.path.abspath('base/hallo'))
183 self.assertEqual(pathdict['ham'], os.path.abspath(u'base/h\xE4m'))
184 # not touched, because key not in keys:
185 self.assertEqual(pathdict['spam'], u'spam')
187 def test_make_paths_absolute_cwd(self):
188 # With base_path None, the cwd is used as base path.
189 # Settings values may-be `unicode` instances, therefore
190 # os.getcwdu() is used and the converted path is a unicode instance:
191 pathdict = self.pathdict.copy()
192 frontend.make_paths_absolute(pathdict, self.keys)
193 self.assertEqual(pathdict['foo'], os.path.abspath(u'hallo'))
194 self.assertEqual(pathdict['ham'], os.path.abspath(u'h\xE4m'))
195 # not touched, because key not in keys:
196 self.assertEqual(pathdict['spam'], u'spam')
198 def test_validate_colon_separated_string_list(self):
199 tests = (
200 (u'a', ['a',] ),
201 (u'a:12', ['a', '12'] ),
202 ([u'a',], ['a',] ),
203 # TODO ("u'a',", ['a',] ), AttributeError: 'str' object has no attribute 'pop'
205 for t in tests:
206 self.assertEqual(
207 frontend.validate_colon_separated_string_list(None, t[0], None),
208 t[1])
211 def test_validate_comma_separated_list(self):
212 tests = (
213 (u'a', ['a',] ),
214 (u'a,12', ['a', '12'] ),
215 ([u'a',], ['a',] ),
216 # TODO ("u'a',", ['a',] ), AttributeError: 'str' object has no attribute 'pop'
218 for t in tests:
219 self.assertEqual(
220 frontend.validate_comma_separated_list(None, t[0], None),
221 t[1])
223 if __name__ == '__main__':
224 unittest.main()