4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
8 Tests of runtime settings.
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
)
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')}
41 'old': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
44 u
'python_home': u
'http://www.python.org',
47 u
'stylesheet_path': [u
'stylesheets/pep.css'],
48 'template': fixpath(u
'data/pep-html-template')},
49 'one': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
52 u
'python_home': u
'http://www.python.org',
53 u
'raw_enabled': False,
54 'record_dependencies': utils
.DependencyList(),
57 u
'stylesheet_path': [u
'stylesheets/pep.css'],
59 u
'template': fixpath(u
'data/pep-html-template'),
60 u
'trim_footnote_reference_space': True,
62 'two': {u
'footnote_references': u
'superscript',
64 'record_dependencies': utils
.DependencyList(),
66 u
'stylesheet_path': [u
'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',
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',
75 u
'strip_elements_with_classes': [u
'sugar', u
'flour',
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."""
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()
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
):
99 expected
.update(self
.settings
[name
])
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'
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)))
119 def test_nofiles(self
):
120 self
.compare_output(self
.files_settings(),
121 self
.expected_settings())
124 self
.compare_output(self
.files_settings('old'),
125 self
.expected_settings('old'))
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'))
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.
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
__
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
):
202 (u
'a:b', ['a', 'b'] ),
203 ('a:b', ['a', 'b'] ),
205 ([u
'a', u
'b:c'], ['a', 'b', 'c'] ),
209 frontend
.validate_colon_separated_string_list(None, t
[0], None),
213 def test_validate_comma_separated_list(self
):
217 (u
'a,b', ['a', 'b'] ),
218 ('a,b', ['a', 'b'] ),
220 ([u
'a', u
'b,c'], ['a', 'b', 'c'] ),
221 (['a', 'b,c'], ['a', 'b', 'c'] ),
225 frontend
.validate_comma_separated_list(None, t
[0], None),
228 if __name__
== '__main__':