Added rst2odt/odtwriter to the main branch.
[docutils.git] / test / test_settings.py
blob9f573ae56e49d04daf4ca52631a81ea549d0ab5b
1 #!/usr/bin/env python
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests of runtime settings.
9 """
11 import sys
12 import os
13 import docutils_difflib
14 import pprint
15 import warnings
16 import unittest
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)
26 def fixpath(path):
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')}
39 settings = {
40 'old': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
41 u'generator': 1,
42 u'no_random': 1,
43 u'python_home': u'http://www.python.org',
44 u'source_link': 1,
45 'stylesheet': None,
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',
49 u'generator': 1,
50 u'no_random': 1,
51 u'python_home': u'http://www.python.org',
52 u'raw_enabled': 0,
53 'record_dependencies': utils.DependencyList(),
54 u'source_link': 1,
55 'stylesheet': None,
56 u'stylesheet_path': fixpath(u'data/stylesheets/pep.css'),
57 u'tab_width': 8,
58 u'template': fixpath(u'data/pep-html-template'),
59 u'trim_footnote_reference_space': 1},
60 'two': {u'footnote_references': u'superscript',
61 u'generator': 0,
62 'record_dependencies': utils.DependencyList(),
63 u'stylesheet': None,
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."""
75 def setUp(self):
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()
81 for name in names:
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):
87 expected = {}
88 for name in names:
89 expected.update(self.settings[name])
90 return expected
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'
100 try:
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)))
107 raise
109 def test_nofiles(self):
110 self.compare_output(self.files_settings(),
111 self.expected_settings())
113 def test_old(self):
114 self.compare_output(self.files_settings('old'),
115 self.expected_settings('old'))
117 def test_one(self):
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'))
129 def test_list(self):
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.
149 def setUp(self):
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__
160 def tearDown(self):
161 os.environ = self.orig_environ
164 if __name__ == '__main__':
165 unittest.main()