2 # -*- coding: utf-8 -*-
5 # Author: David Goodger <goodger@python.org>
6 # Copyright: This module has been placed in the public domain.
9 Tests of runtime settings.
18 import DocutilsTestSupport
# must be imported before docutils
19 from docutils
import frontend
, utils
20 from docutils
.writers
import html4css1
, pep_html
21 from docutils
.parsers
import rst
24 warnings
.filterwarnings(action
='ignore',
25 category
=frontend
.ConfigDeprecationWarning
)
29 return os
.path
.abspath(os
.path
.join(*(path
.split('/'))))
32 class ConfigFileTests(unittest
.TestCase
):
34 config_files
= {'old': fixpath('data/config_old.txt'),
35 'one': fixpath('data/config_1.txt'),
36 'two': fixpath('data/config_2.txt'),
37 'list': fixpath('data/config_list.txt'),
38 'list2': fixpath('data/config_list_2.txt'),
39 'error': fixpath('data/config_error_handler.txt')}
42 'old': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
45 u
'python_home': u
'http://www.python.org',
48 u
'stylesheet_path': [u
'stylesheets/pep.css'],
49 'template': fixpath(u
'data/pep-html-template')},
50 'one': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
53 u
'python_home': u
'http://www.python.org',
54 u
'raw_enabled': False,
55 'record_dependencies': utils
.DependencyList(),
58 u
'stylesheet_path': [u
'stylesheets/pep.css'],
60 u
'template': fixpath(u
'data/pep-html-template'),
61 u
'trim_footnote_reference_space': True,
63 'two': {u
'footnote_references': u
'superscript',
65 'record_dependencies': utils
.DependencyList(),
67 u
'stylesheet_path': [u
'test.css'],
68 'trim_footnote_reference_space': None},
69 'list': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e'],
70 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot'],
71 u
'strip_elements_with_classes': [u
'sugar', u
'flour', u
'milk',
73 'list2': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e', u
'f'],
74 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot',
76 u
'strip_elements_with_classes': [u
'sugar', u
'flour',
79 'error': {u
'error_encoding': u
'ascii',
80 u
'error_encoding_error_handler': u
'strict'},
83 compare
= difflib
.Differ().compare
84 """Comparison method shared by all tests."""
87 self
.option_parser
= frontend
.OptionParser(
88 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
90 def files_settings(self
, *names
):
91 settings
= frontend
.Values()
93 settings
.update(self
.option_parser
.get_config_file_settings(
94 self
.config_files
[name
]), self
.option_parser
)
95 return settings
.__dict
__
97 def expected_settings(self
, *names
):
100 expected
.update(self
.settings
[name
])
103 def compare_output(self
, result
, expected
):
104 """`result` and `expected` should both be dicts."""
105 self
.assertTrue('record_dependencies' in result
)
106 if 'record_dependencies' not in expected
:
107 # Delete it if we don't want to test it.
108 del result
['record_dependencies']
109 result
= pprint
.pformat(result
) + '\n'
110 expected
= pprint
.pformat(expected
) + '\n'
112 self
.assertEqual(result
, expected
)
113 except AssertionError:
114 print >>sys
.stderr
, '\n%s\n' % (self
,)
115 print >>sys
.stderr
, '-: expected\n+: result'
116 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
117 result
.splitlines(1)))
120 def test_nofiles(self
):
121 self
.compare_output(self
.files_settings(),
122 self
.expected_settings())
125 self
.compare_output(self
.files_settings('old'),
126 self
.expected_settings('old'))
129 self
.compare_output(self
.files_settings('one'),
130 self
.expected_settings('one'))
132 def test_multiple(self
):
133 self
.compare_output(self
.files_settings('one', 'two'),
134 self
.expected_settings('one', 'two'))
136 def test_old_and_new(self
):
137 self
.compare_output(self
.files_settings('old', 'two'),
138 self
.expected_settings('old', 'two'))
141 self
.compare_output(self
.files_settings('list'),
142 self
.expected_settings('list'))
144 def test_list2(self
):
145 self
.compare_output(self
.files_settings('list', 'list2'),
146 self
.expected_settings('list2'))
148 def test_error_handler(self
):
149 self
.compare_output(self
.files_settings('error'),
150 self
.expected_settings('error'))
153 class ConfigEnvVarFileTests(ConfigFileTests
):
156 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
157 environment variable and the standard Docutils config file mechanism.
161 ConfigFileTests
.setUp(self
)
162 self
.orig_environ
= os
.environ
163 os
.environ
= os
.environ
.copy()
165 def files_settings(self
, *names
):
166 files
= [self
.config_files
[name
] for name
in names
]
167 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
168 settings
= self
.option_parser
.get_standard_config_settings()
169 return settings
.__dict
__
172 os
.environ
= self
.orig_environ
175 class HelperFunctionsTests(unittest
.TestCase
):
177 pathdict
= {'foo': 'hallo', 'ham': u
'h\xE4m', 'spam': u
'spam'}
178 keys
= ['foo', 'ham']
181 self
.option_parser
= frontend
.OptionParser(
182 components
=(rst
.Parser
,), read_config_files
=None)
184 def test_make_paths_absolute(self
):
185 pathdict
= self
.pathdict
.copy()
186 frontend
.make_paths_absolute(pathdict
, self
.keys
, base_path
='base')
187 self
.assertEqual(pathdict
['foo'], os
.path
.abspath('base/hallo'))
188 self
.assertEqual(pathdict
['ham'], os
.path
.abspath(u
'base/h\xE4m'))
189 # not touched, because key not in keys:
190 self
.assertEqual(pathdict
['spam'], u
'spam')
192 def test_make_paths_absolute_cwd(self
):
193 # With base_path None, the cwd is used as base path.
194 # Settings values may-be `unicode` instances, therefore
195 # os.getcwdu() is used and the converted path is a unicode instance:
196 pathdict
= self
.pathdict
.copy()
197 frontend
.make_paths_absolute(pathdict
, self
.keys
)
198 self
.assertEqual(pathdict
['foo'], os
.path
.abspath(u
'hallo'))
199 self
.assertEqual(pathdict
['ham'], os
.path
.abspath(u
'h\xE4m'))
200 # not touched, because key not in keys:
201 self
.assertEqual(pathdict
['spam'], u
'spam')
214 def test_validate_boolean(self
):
215 for t
in self
.boolean_settings
:
217 frontend
.validate_boolean(None, t
[0], self
.option_parser
),
220 def test_validate_ternary(self
):
223 (u
'parrot', u
'parrot'),
225 for t
in self
.boolean_settings
+ tests
:
227 frontend
.validate_ternary(None, t
[0], self
.option_parser
),
230 def test_validate_colon_separated_string_list(self
):
234 (u
'a:b', ['a', 'b'] ),
235 ('a:b', ['a', 'b'] ),
237 ([u
'a', u
'b:c'], ['a', 'b', 'c'] ),
241 frontend
.validate_colon_separated_string_list(None, t
[0], None),
244 def test_validate_comma_separated_list(self
):
248 (u
'a,b', ['a', 'b'] ),
249 ('a,b', ['a', 'b'] ),
251 ([u
'a', u
'b,c'], ['a', 'b', 'c'] ),
252 (['a', 'b,c'], ['a', 'b', 'c'] ),
256 frontend
.validate_comma_separated_list(None, t
[0], None),
259 def test_validate_url_trailing_slash(self
):
263 (u
'http://example.org', u
'http://example.org/' ),
264 ('http://example.org/', 'http://example.org/' ),
268 frontend
.validate_url_trailing_slash(None, t
[0], None),
271 def test_validate_smartquotes_locales(self
):
273 ('en:ssvv', [('en', 'ssvv')]),
274 (u
'sd:«»°°', [(u
'sd', u
'«»°°')]),
275 ([('sd', u
'«»°°'), u
'ds:°°«»'], [('sd', u
'«»°°'),
277 (u
'frs:« : »:((:))', [(u
'frs', [u
'« ', u
' »',
282 frontend
.validate_smartquotes_locales(None, t
[0], None),
287 if __name__
== '__main__':