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': 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',
52 u
'python_home': u
'http://www.python.org',
54 'record_dependencies': utils
.DependencyList(),
57 u
'stylesheet_path': fixpath(u
'data/stylesheets/pep.css'),
59 u
'template': fixpath(u
'data/pep-html-template'),
60 u
'trim_footnote_reference_space': 1},
61 'two': {u
'footnote_references': u
'superscript',
63 'record_dependencies': utils
.DependencyList(),
65 u
'stylesheet_path': fixpath(u
'data/test.css'),
66 'trim_footnote_reference_space': None},
67 'list': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e'],
68 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot'],
69 u
'strip_elements_with_classes': [u
'sugar', u
'flour', u
'milk',
71 'list2': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e', u
'f'],
72 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot',
74 u
'strip_elements_with_classes': [u
'sugar', u
'flour',
77 'error': {u
'error_encoding': u
'ascii',
78 u
'error_encoding_error_handler': u
'strict'},
81 compare
= difflib
.Differ().compare
82 """Comparison method shared by all tests."""
85 self
.option_parser
= frontend
.OptionParser(
86 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
88 def files_settings(self
, *names
):
89 settings
= frontend
.Values()
91 settings
.update(self
.option_parser
.get_config_file_settings(
92 self
.config_files
[name
]), self
.option_parser
)
93 return settings
.__dict
__
95 def expected_settings(self
, *names
):
98 expected
.update(self
.settings
[name
])
101 def compare_output(self
, result
, expected
):
102 """`result` and `expected` should both be dicts."""
103 self
.assertTrue('record_dependencies' in result
)
104 if 'record_dependencies' not in expected
:
105 # Delete it if we don't want to test it.
106 del result
['record_dependencies']
107 result
= pprint
.pformat(result
) + '\n'
108 expected
= pprint
.pformat(expected
) + '\n'
110 self
.assertEqual(result
, expected
)
111 except AssertionError:
112 print >>sys
.stderr
, '\n%s\n' % (self
,)
113 print >>sys
.stderr
, '-: expected\n+: result'
114 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
115 result
.splitlines(1)))
118 def test_nofiles(self
):
119 self
.compare_output(self
.files_settings(),
120 self
.expected_settings())
123 self
.compare_output(self
.files_settings('old'),
124 self
.expected_settings('old'))
127 self
.compare_output(self
.files_settings('one'),
128 self
.expected_settings('one'))
130 def test_multiple(self
):
131 self
.compare_output(self
.files_settings('one', 'two'),
132 self
.expected_settings('one', 'two'))
134 def test_old_and_new(self
):
135 self
.compare_output(self
.files_settings('old', 'two'),
136 self
.expected_settings('old', 'two'))
139 self
.compare_output(self
.files_settings('list'),
140 self
.expected_settings('list'))
142 def test_list2(self
):
143 self
.compare_output(self
.files_settings('list', 'list2'),
144 self
.expected_settings('list2'))
146 def test_error_handler(self
):
147 self
.compare_output(self
.files_settings('error'),
148 self
.expected_settings('error'))
151 class ConfigEnvVarFileTests(ConfigFileTests
):
154 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
155 environment variable and the standard Docutils config file mechanism.
159 ConfigFileTests
.setUp(self
)
160 self
.orig_environ
= os
.environ
161 os
.environ
= os
.environ
.copy()
163 def files_settings(self
, *names
):
164 files
= [self
.config_files
[name
] for name
in names
]
165 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
166 settings
= self
.option_parser
.get_standard_config_settings()
167 return settings
.__dict
__
170 os
.environ
= self
.orig_environ
173 class HelperFunctionsTests(unittest
.TestCase
):
175 pathdict
= {'foo': 'hallo', 'ham': u
'h\xE4m', 'spam': u
'spam'}
176 keys
= ['foo', 'ham']
178 def test_make_paths_absolute(self
):
179 pathdict
= self
.pathdict
.copy()
180 frontend
.make_paths_absolute(pathdict
, self
.keys
, base_path
='base')
181 self
.assertEqual(pathdict
['foo'], os
.path
.abspath('base/hallo'))
182 self
.assertEqual(pathdict
['ham'], os
.path
.abspath(u
'base/h\xE4m'))
183 # not touched, because key not in keys:
184 self
.assertEqual(pathdict
['spam'], u
'spam')
186 def test_make_paths_absolute_cwd(self
):
187 # With base_path None, the cwd is used as base path.
188 # Settings values may-be `unicode` instances, therefore
189 # os.getcwdu() is used and the converted path is a unicode instance:
190 pathdict
= self
.pathdict
.copy()
191 frontend
.make_paths_absolute(pathdict
, self
.keys
)
192 self
.assertEqual(pathdict
['foo'], os
.path
.abspath(u
'hallo'))
193 self
.assertEqual(pathdict
['ham'], os
.path
.abspath(u
'h\xE4m'))
194 # not touched, because key not in keys:
195 self
.assertEqual(pathdict
['spam'], u
'spam')
197 if __name__
== '__main__':