support for new python.org and pep2pyramid.py
[docutils.git] / test / test_settings.py
blobc18d613dee843903d52c944bbc906566423adfb4
1 #!/usr/bin/env python
3 # Author: David Goodger
4 # Contact: goodger@python.org
5 # Revision: $Revision$
6 # Date: $Date$
7 # Copyright: This module has been placed in the public domain.
9 """
10 Tests of runtime settings.
11 """
13 import sys
14 import os
15 import docutils_difflib
16 import pprint
17 import warnings
18 import unittest
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)
29 def fixpath(path):
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')}
42 settings = {
43 'old': {'datestamp': '%Y-%m-%d %H:%M UTC',
44 'generator': 1,
45 'no_random': 1,
46 'python_home': 'http://www.python.org',
47 'source_link': 1,
48 'stylesheet': None,
49 'stylesheet_path': fixpath('data/stylesheets/pep.css'),
50 'template': fixpath('data/pep-html-template')},
51 'one': {'datestamp': '%Y-%m-%d %H:%M UTC',
52 'generator': 1,
53 'no_random': 1,
54 'python_home': 'http://www.python.org',
55 'record_dependencies': utils.DependencyList(),
56 'source_link': 1,
57 'stylesheet': None,
58 'stylesheet_path': fixpath('data/stylesheets/pep.css'),
59 'tab_width': 8,
60 'template': fixpath('data/pep-html-template'),
61 'trim_footnote_reference_space': 1},
62 'two': {'footnote_references': 'superscript',
63 'generator': 0,
64 'record_dependencies': utils.DependencyList(),
65 'stylesheet': None,
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."""
77 def setUp(self):
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()
83 for name in names:
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):
89 expected = {}
90 for name in names:
91 expected.update(self.settings[name])
92 return expected
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'
102 try:
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)))
109 raise
111 def test_nofiles(self):
112 self.compare_output(self.files_settings(),
113 self.expected_settings())
115 def test_old(self):
116 self.compare_output(self.files_settings('old'),
117 self.expected_settings('old'))
119 def test_one(self):
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'))
131 def test_list(self):
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.
151 def setUp(self):
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__
162 def tearDown(self):
163 os.environ = self.orig_environ
166 if __name__ == '__main__':
167 unittest.main()