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
)
27 if not hasattr(unittest
.TestCase
, "assertTrue"):
28 # HACK? this changes TestCase, fixes the problem for tests executing afterwards.
29 # this tests break if run alone
30 unittest
.TestCase
.assertTrue
= unittest
.TestCase
.failUnless
33 return os
.path
.abspath(os
.path
.join(*(path
.split('/'))))
36 class ConfigFileTests(unittest
.TestCase
):
38 config_files
= {'old': fixpath('data/config_old.txt'),
39 'one': fixpath('data/config_1.txt'),
40 'two': fixpath('data/config_2.txt'),
41 'list': fixpath('data/config_list.txt'),
42 'list2': fixpath('data/config_list_2.txt'),
43 'error': fixpath('data/config_error_handler.txt')}
46 'old': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
49 u
'python_home': u
'http://www.python.org',
52 u
'stylesheet_path': fixpath(u
'data/stylesheets/pep.css'),
53 'template': fixpath(u
'data/pep-html-template')},
54 'one': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
57 u
'python_home': u
'http://www.python.org',
59 'record_dependencies': utils
.DependencyList(),
62 u
'stylesheet_path': fixpath(u
'data/stylesheets/pep.css'),
64 u
'template': fixpath(u
'data/pep-html-template'),
65 u
'trim_footnote_reference_space': 1},
66 'two': {u
'footnote_references': u
'superscript',
68 'record_dependencies': utils
.DependencyList(),
70 u
'stylesheet_path': fixpath(u
'data/test.css'),
71 'trim_footnote_reference_space': None},
72 'list': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e'],
73 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot'],
74 u
'strip_elements_with_classes': [u
'sugar', u
'flour', u
'milk',
76 'list2': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e', u
'f'],
77 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot',
79 u
'strip_elements_with_classes': [u
'sugar', u
'flour',
82 'error': {u
'error_encoding': u
'ascii',
83 u
'error_encoding_error_handler': u
'strict'},
86 compare
= difflib
.Differ().compare
87 """Comparison method shared by all tests."""
90 self
.option_parser
= frontend
.OptionParser(
91 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
93 def files_settings(self
, *names
):
94 settings
= frontend
.Values()
96 settings
.update(self
.option_parser
.get_config_file_settings(
97 self
.config_files
[name
]), self
.option_parser
)
98 return settings
.__dict
__
100 def expected_settings(self
, *names
):
103 expected
.update(self
.settings
[name
])
106 def compare_output(self
, result
, expected
):
107 """`result` and `expected` should both be dicts."""
108 self
.assertTrue('record_dependencies' in result
)
109 if 'record_dependencies' not in expected
:
110 # Delete it if we don't want to test it.
111 del result
['record_dependencies']
112 result
= pprint
.pformat(result
) + '\n'
113 expected
= pprint
.pformat(expected
) + '\n'
115 self
.assertEqual(result
, expected
)
116 except AssertionError:
117 print >>sys
.stderr
, '\n%s\n' % (self
,)
118 print >>sys
.stderr
, '-: expected\n+: result'
119 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
120 result
.splitlines(1)))
123 def test_nofiles(self
):
124 self
.compare_output(self
.files_settings(),
125 self
.expected_settings())
128 self
.compare_output(self
.files_settings('old'),
129 self
.expected_settings('old'))
132 self
.compare_output(self
.files_settings('one'),
133 self
.expected_settings('one'))
135 def test_multiple(self
):
136 self
.compare_output(self
.files_settings('one', 'two'),
137 self
.expected_settings('one', 'two'))
139 def test_old_and_new(self
):
140 self
.compare_output(self
.files_settings('old', 'two'),
141 self
.expected_settings('old', 'two'))
144 self
.compare_output(self
.files_settings('list'),
145 self
.expected_settings('list'))
147 def test_list2(self
):
148 self
.compare_output(self
.files_settings('list', 'list2'),
149 self
.expected_settings('list2'))
151 def test_error_handler(self
):
152 self
.compare_output(self
.files_settings('error'),
153 self
.expected_settings('error'))
156 class ConfigEnvVarFileTests(ConfigFileTests
):
159 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
160 environment variable and the standard Docutils config file mechanism.
164 ConfigFileTests
.setUp(self
)
165 self
.orig_environ
= os
.environ
166 os
.environ
= os
.environ
.copy()
168 def files_settings(self
, *names
):
169 files
= [self
.config_files
[name
] for name
in names
]
170 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
171 settings
= self
.option_parser
.get_standard_config_settings()
172 return settings
.__dict
__
175 os
.environ
= self
.orig_environ
178 class HelperFunctionsTests(unittest
.TestCase
):
180 pathdict
= {'foo': 'hallo', 'ham': u
'h\xE4m', 'spam': u
'spam'}
181 keys
= ['foo', 'ham']
183 def test_make_paths_absolute(self
):
184 pathdict
= self
.pathdict
.copy()
185 frontend
.make_paths_absolute(pathdict
, self
.keys
, base_path
='base')
186 self
.assertEqual(pathdict
['foo'], os
.path
.abspath('base/hallo'))
187 self
.assertEqual(pathdict
['ham'], os
.path
.abspath(u
'base/h\xE4m'))
188 # not touched, because key not in keys:
189 self
.assertEqual(pathdict
['spam'], u
'spam')
191 def test_make_paths_absolute_cwd(self
):
192 # With base_path None, the cwd is used as base path.
193 # Settings values may-be `unicode` instances, therefore
194 # os.getcwdu() is used and the converted path is a unicode instance:
195 pathdict
= self
.pathdict
.copy()
196 frontend
.make_paths_absolute(pathdict
, self
.keys
)
197 self
.assertEqual(pathdict
['foo'], os
.path
.abspath(u
'hallo'))
198 self
.assertEqual(pathdict
['ham'], os
.path
.abspath(u
'h\xE4m'))
199 # not touched, because key not in keys:
200 self
.assertEqual(pathdict
['spam'], u
'spam')
202 if __name__
== '__main__':