4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
8 Tests of runtime settings.
13 import docutils_difflib
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 'list2': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e', u
'f']},
68 'error': {u
'error_encoding': u
'ascii',
69 u
'error_encoding_error_handler': u
'strict'},
72 compare
= docutils_difflib
.Differ().compare
73 """Comparison method shared by all tests."""
76 self
.option_parser
= frontend
.OptionParser(
77 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
79 def files_settings(self
, *names
):
80 settings
= frontend
.Values()
82 settings
.update(self
.option_parser
.get_config_file_settings(
83 self
.config_files
[name
]), self
.option_parser
)
84 return settings
.__dict
__
86 def expected_settings(self
, *names
):
89 expected
.update(self
.settings
[name
])
92 def compare_output(self
, result
, expected
):
93 """`result` and `expected` should both be dicts."""
94 self
.assert_('record_dependencies' in result
)
95 if 'record_dependencies' not in expected
:
96 # Delete it if we don't want to test it.
97 del result
['record_dependencies']
98 result
= pprint
.pformat(result
) + '\n'
99 expected
= pprint
.pformat(expected
) + '\n'
101 self
.assertEquals(result
, expected
)
102 except AssertionError:
103 print >>sys
.stderr
, '\n%s\n' % (self
,)
104 print >>sys
.stderr
, '-: expected\n+: result'
105 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
106 result
.splitlines(1)))
109 def test_nofiles(self
):
110 self
.compare_output(self
.files_settings(),
111 self
.expected_settings())
114 self
.compare_output(self
.files_settings('old'),
115 self
.expected_settings('old'))
118 self
.compare_output(self
.files_settings('one'),
119 self
.expected_settings('one'))
121 def test_multiple(self
):
122 self
.compare_output(self
.files_settings('one', 'two'),
123 self
.expected_settings('one', 'two'))
125 def test_old_and_new(self
):
126 self
.compare_output(self
.files_settings('old', 'two'),
127 self
.expected_settings('old', 'two'))
130 self
.compare_output(self
.files_settings('list'),
131 self
.expected_settings('list'))
133 def test_list2(self
):
134 self
.compare_output(self
.files_settings('list', 'list2'),
135 self
.expected_settings('list2'))
137 def test_error_handler(self
):
138 self
.compare_output(self
.files_settings('error'),
139 self
.expected_settings('error'))
142 class ConfigEnvVarFileTests(ConfigFileTests
):
145 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
146 environment variable and the standard Docutils config file mechanism.
150 ConfigFileTests
.setUp(self
)
151 self
.orig_environ
= os
.environ
152 os
.environ
= os
.environ
.copy()
154 def files_settings(self
, *names
):
155 files
= [self
.config_files
[name
] for name
in names
]
156 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
157 settings
= self
.option_parser
.get_standard_config_settings()
158 return settings
.__dict
__
161 os
.environ
= self
.orig_environ
164 if __name__
== '__main__':