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 return os
.path
.abspath(os
.path
.join(*(path
.split('/'))))
30 class ConfigFileTests(unittest
.TestCase
):
32 config_files
= {'old': fixpath('data/config_old.txt'),
33 'one': fixpath('data/config_1.txt'),
34 'two': fixpath('data/config_2.txt'),
35 'list': fixpath('data/config_list.txt'),
36 'list2': fixpath('data/config_list_2.txt'),
37 'error': fixpath('data/config_error_handler.txt')}
40 'old': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
43 u
'python_home': u
'http://www.python.org',
46 u
'stylesheet_path': fixpath(u
'data/stylesheets/pep.css'),
47 'template': fixpath(u
'data/pep-html-template')},
48 'one': {u
'datestamp': u
'%Y-%m-%d %H:%M UTC',
51 u
'python_home': u
'http://www.python.org',
53 'record_dependencies': utils
.DependencyList(),
56 u
'stylesheet_path': fixpath(u
'data/stylesheets/pep.css'),
58 u
'template': fixpath(u
'data/pep-html-template'),
59 u
'trim_footnote_reference_space': 1},
60 'two': {u
'footnote_references': u
'superscript',
62 'record_dependencies': utils
.DependencyList(),
64 u
'stylesheet_path': fixpath(u
'data/test.css'),
65 'trim_footnote_reference_space': None},
66 'list': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e'],
67 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot'],
68 u
'strip_elements_with_classes': [u
'sugar', u
'flour', u
'milk',
70 'list2': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e', u
'f'],
71 u
'strip_classes': [u
'spam', u
'pan', u
'fun', u
'parrot',
73 u
'strip_elements_with_classes': [u
'sugar', u
'flour',
76 'error': {u
'error_encoding': u
'ascii',
77 u
'error_encoding_error_handler': u
'strict'},
80 compare
= difflib
.Differ().compare
81 """Comparison method shared by all tests."""
84 self
.option_parser
= frontend
.OptionParser(
85 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
87 def files_settings(self
, *names
):
88 settings
= frontend
.Values()
90 settings
.update(self
.option_parser
.get_config_file_settings(
91 self
.config_files
[name
]), self
.option_parser
)
92 return settings
.__dict
__
94 def expected_settings(self
, *names
):
97 expected
.update(self
.settings
[name
])
100 def compare_output(self
, result
, expected
):
101 """`result` and `expected` should both be dicts."""
102 self
.assert_('record_dependencies' in result
)
103 if 'record_dependencies' not in expected
:
104 # Delete it if we don't want to test it.
105 del result
['record_dependencies']
106 result
= pprint
.pformat(result
) + '\n'
107 expected
= pprint
.pformat(expected
) + '\n'
109 self
.assertEquals(result
, expected
)
110 except AssertionError:
111 print >>sys
.stderr
, '\n%s\n' % (self
,)
112 print >>sys
.stderr
, '-: expected\n+: result'
113 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
114 result
.splitlines(1)))
117 def test_nofiles(self
):
118 self
.compare_output(self
.files_settings(),
119 self
.expected_settings())
122 self
.compare_output(self
.files_settings('old'),
123 self
.expected_settings('old'))
126 self
.compare_output(self
.files_settings('one'),
127 self
.expected_settings('one'))
129 def test_multiple(self
):
130 self
.compare_output(self
.files_settings('one', 'two'),
131 self
.expected_settings('one', 'two'))
133 def test_old_and_new(self
):
134 self
.compare_output(self
.files_settings('old', 'two'),
135 self
.expected_settings('old', 'two'))
138 self
.compare_output(self
.files_settings('list'),
139 self
.expected_settings('list'))
141 def test_list2(self
):
142 self
.compare_output(self
.files_settings('list', 'list2'),
143 self
.expected_settings('list2'))
145 def test_error_handler(self
):
146 self
.compare_output(self
.files_settings('error'),
147 self
.expected_settings('error'))
150 class ConfigEnvVarFileTests(ConfigFileTests
):
153 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
154 environment variable and the standard Docutils config file mechanism.
158 ConfigFileTests
.setUp(self
)
159 self
.orig_environ
= os
.environ
160 os
.environ
= os
.environ
.copy()
162 def files_settings(self
, *names
):
163 files
= [self
.config_files
[name
] for name
in names
]
164 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
165 settings
= self
.option_parser
.get_standard_config_settings()
166 return settings
.__dict
__
169 os
.environ
= self
.orig_environ
172 if __name__
== '__main__':