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 from types
import StringType
18 import DocutilsTestSupport
# must be imported before docutils
19 from docutils
import frontend
, utils
20 from docutils
.writers
import html4css1
, pep_html
21 from docutils
.parsers
import rst
24 warnings
.filterwarnings(action
='ignore',
25 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 'list2': {u
'expose_internals': [u
'a', u
'b', u
'c', u
'd', u
'e', u
'f']},
69 'error': {u
'error_encoding': u
'ascii',
70 u
'error_encoding_error_handler': u
'strict'},
73 compare
= docutils_difflib
.Differ().compare
74 """Comparison method shared by all tests."""
77 self
.option_parser
= frontend
.OptionParser(
78 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
80 def files_settings(self
, *names
):
81 settings
= frontend
.Values()
83 settings
.update(self
.option_parser
.get_config_file_settings(
84 self
.config_files
[name
]), self
.option_parser
)
85 return settings
.__dict
__
87 def expected_settings(self
, *names
):
90 expected
.update(self
.settings
[name
])
93 def compare_output(self
, result
, expected
):
94 """`result` and `expected` should both be dicts."""
95 self
.assert_(result
.has_key('record_dependencies'))
96 if not expected
.has_key('record_dependencies'):
97 # Delete it if we don't want to test it.
98 del result
['record_dependencies']
99 result
= pprint
.pformat(result
) + '\n'
100 expected
= pprint
.pformat(expected
) + '\n'
102 self
.assertEquals(result
, expected
)
103 except AssertionError:
104 print >>sys
.stderr
, '\n%s\n' % (self
,)
105 print >>sys
.stderr
, '-: expected\n+: result'
106 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
107 result
.splitlines(1)))
110 def test_nofiles(self
):
111 self
.compare_output(self
.files_settings(),
112 self
.expected_settings())
115 self
.compare_output(self
.files_settings('old'),
116 self
.expected_settings('old'))
119 self
.compare_output(self
.files_settings('one'),
120 self
.expected_settings('one'))
122 def test_multiple(self
):
123 self
.compare_output(self
.files_settings('one', 'two'),
124 self
.expected_settings('one', 'two'))
126 def test_old_and_new(self
):
127 self
.compare_output(self
.files_settings('old', 'two'),
128 self
.expected_settings('old', 'two'))
131 self
.compare_output(self
.files_settings('list'),
132 self
.expected_settings('list'))
134 def test_list2(self
):
135 self
.compare_output(self
.files_settings('list', 'list2'),
136 self
.expected_settings('list2'))
138 def test_error_handler(self
):
139 self
.compare_output(self
.files_settings('error'),
140 self
.expected_settings('error'))
143 class ConfigEnvVarFileTests(ConfigFileTests
):
146 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
147 environment variable and the standard Docutils config file mechanism.
151 ConfigFileTests
.setUp(self
)
152 self
.orig_environ
= os
.environ
153 os
.environ
= os
.environ
.copy()
155 def files_settings(self
, *names
):
156 files
= [self
.config_files
[name
] for name
in names
]
157 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
158 settings
= self
.option_parser
.get_standard_config_settings()
159 return settings
.__dict
__
162 os
.environ
= self
.orig_environ
165 if __name__
== '__main__':