3 # Author: David Goodger
4 # Contact: goodger@python.org
7 # Copyright: This module has been placed in the public domain.
10 Tests of runtime settings.
15 import docutils_difflib
19 from types
import StringType
20 import DocutilsTestSupport
# must be imported before docutils
21 from docutils
import frontend
, utils
22 from docutils
.writers
import html4css1
, pep_html
23 from docutils
.parsers
import rst
26 warnings
.filterwarnings(action
='ignore',
27 category
=frontend
.ConfigDeprecationWarning
)
30 return os
.path
.abspath(os
.path
.join(*(path
.split('/'))))
33 class ConfigFileTests(unittest
.TestCase
):
35 config_files
= {'old': fixpath('data/config_old.txt'),
36 'one': fixpath('data/config_1.txt'),
37 'two': fixpath('data/config_2.txt'),
38 'list': fixpath('data/config_list.txt'),
39 'list2': fixpath('data/config_list_2.txt'),
40 'error': fixpath('data/config_error_handler.txt')}
43 'old': {'datestamp': '%Y-%m-%d %H:%M UTC',
46 'python_home': 'http://www.python.org',
49 'stylesheet_path': fixpath('data/stylesheets/pep.css'),
50 'template': fixpath('data/pep-html-template')},
51 'one': {'datestamp': '%Y-%m-%d %H:%M UTC',
54 'python_home': 'http://www.python.org',
55 'record_dependencies': utils
.DependencyList(),
58 'stylesheet_path': fixpath('data/stylesheets/pep.css'),
60 'template': fixpath('data/pep-html-template'),
61 'trim_footnote_reference_space': 1},
62 'two': {'footnote_references': 'superscript',
64 'record_dependencies': utils
.DependencyList(),
66 'stylesheet_path': fixpath('data/test.css'),
67 'trim_footnote_reference_space': None},
68 'list': {'expose_internals': ['a', 'b', 'c', 'd', 'e']},
69 'list2': {'expose_internals': ['a', 'b', 'c', 'd', 'e', 'f']},
70 'error': {'error_encoding': 'ascii',
71 'error_encoding_error_handler': 'strict'},
74 compare
= docutils_difflib
.Differ().compare
75 """Comparison method shared by all tests."""
78 self
.option_parser
= frontend
.OptionParser(
79 components
=(pep_html
.Writer
, rst
.Parser
), read_config_files
=None)
81 def files_settings(self
, *names
):
82 settings
= frontend
.Values()
84 settings
.update(self
.option_parser
.get_config_file_settings(
85 self
.config_files
[name
]), self
.option_parser
)
86 return settings
.__dict
__
88 def expected_settings(self
, *names
):
91 expected
.update(self
.settings
[name
])
94 def compare_output(self
, result
, expected
):
95 """`result` and `expected` should both be dicts."""
96 self
.assert_(result
.has_key('record_dependencies'))
97 if not expected
.has_key('record_dependencies'):
98 # Delete it if we don't want to test it.
99 del result
['record_dependencies']
100 result
= pprint
.pformat(result
) + '\n'
101 expected
= pprint
.pformat(expected
) + '\n'
103 self
.assertEquals(result
, expected
)
104 except AssertionError:
105 print >>sys
.stderr
, '\n%s\n' % (self
,)
106 print >>sys
.stderr
, '-: expected\n+: result'
107 print >>sys
.stderr
, ''.join(self
.compare(expected
.splitlines(1),
108 result
.splitlines(1)))
111 def test_nofiles(self
):
112 self
.compare_output(self
.files_settings(),
113 self
.expected_settings())
116 self
.compare_output(self
.files_settings('old'),
117 self
.expected_settings('old'))
120 self
.compare_output(self
.files_settings('one'),
121 self
.expected_settings('one'))
123 def test_multiple(self
):
124 self
.compare_output(self
.files_settings('one', 'two'),
125 self
.expected_settings('one', 'two'))
127 def test_old_and_new(self
):
128 self
.compare_output(self
.files_settings('old', 'two'),
129 self
.expected_settings('old', 'two'))
132 self
.compare_output(self
.files_settings('list'),
133 self
.expected_settings('list'))
135 def test_list2(self
):
136 self
.compare_output(self
.files_settings('list', 'list2'),
137 self
.expected_settings('list2'))
139 def test_error_handler(self
):
140 self
.compare_output(self
.files_settings('error'),
141 self
.expected_settings('error'))
144 class ConfigEnvVarFileTests(ConfigFileTests
):
147 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
148 environment variable and the standard Docutils config file mechanism.
152 ConfigFileTests
.setUp(self
)
153 self
.orig_environ
= os
.environ
154 os
.environ
= os
.environ
.copy()
156 def files_settings(self
, *names
):
157 files
= [self
.config_files
[name
] for name
in names
]
158 os
.environ
['DOCUTILSCONFIG'] = os
.pathsep
.join(files
)
159 settings
= self
.option_parser
.get_standard_config_settings()
160 return settings
.__dict
__
163 os
.environ
= self
.orig_environ
166 if __name__
== '__main__':