latex2e writer : Move usepackage hyperref after stylesheet inclusion.
[docutils.git] / test / test_settings.py
blob2cf4e080c6f268ba0eb8642579a6d9fb98b1b24e
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 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)
27 def fixpath(path):
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')}
40 settings = {
41 'old': {u'datestamp': u'%Y-%m-%d %H:%M UTC',
42 u'generator': 1,
43 u'no_random': 1,
44 u'python_home': u'http://www.python.org',
45 u'source_link': 1,
46 'stylesheet': None,
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',
50 u'generator': 1,
51 u'no_random': 1,
52 u'python_home': u'http://www.python.org',
53 u'raw_enabled': 0,
54 'record_dependencies': utils.DependencyList(),
55 u'source_link': 1,
56 'stylesheet': None,
57 u'stylesheet_path': fixpath(u'data/stylesheets/pep.css'),
58 u'tab_width': 8,
59 u'template': fixpath(u'data/pep-html-template'),
60 u'trim_footnote_reference_space': 1},
61 'two': {u'footnote_references': u'superscript',
62 u'generator': 0,
63 'record_dependencies': utils.DependencyList(),
64 u'stylesheet': None,
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."""
76 def setUp(self):
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()
82 for name in names:
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):
88 expected = {}
89 for name in names:
90 expected.update(self.settings[name])
91 return expected
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'
101 try:
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)))
108 raise
110 def test_nofiles(self):
111 self.compare_output(self.files_settings(),
112 self.expected_settings())
114 def test_old(self):
115 self.compare_output(self.files_settings('old'),
116 self.expected_settings('old'))
118 def test_one(self):
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'))
130 def test_list(self):
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.
150 def setUp(self):
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__
161 def tearDown(self):
162 os.environ = self.orig_environ
165 if __name__ == '__main__':
166 unittest.main()