Fix [3541369] Relative __import__ also with Python 3.3.
[docutils.git] / test / test_settings.py
blobe5f0a2b22411a24fa7d4bbe51cbfca2169c8f957
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 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)
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 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot'],
69 u'strip_elements_with_classes': [u'sugar', u'flour', u'milk',
70 u'safran']},
71 'list2': {u'expose_internals': [u'a', u'b', u'c', u'd', u'e', u'f'],
72 u'strip_classes': [u'spam', u'pan', u'fun', u'parrot',
73 u'ham', u'eggs'],
74 u'strip_elements_with_classes': [u'sugar', u'flour',
75 u'milk', u'safran',
76 u'eggs', u'salt']},
77 'error': {u'error_encoding': u'ascii',
78 u'error_encoding_error_handler': u'strict'},
81 compare = difflib.Differ().compare
82 """Comparison method shared by all tests."""
84 def setUp(self):
85 self.option_parser = frontend.OptionParser(
86 components=(pep_html.Writer, rst.Parser), read_config_files=None)
88 def files_settings(self, *names):
89 settings = frontend.Values()
90 for name in names:
91 settings.update(self.option_parser.get_config_file_settings(
92 self.config_files[name]), self.option_parser)
93 return settings.__dict__
95 def expected_settings(self, *names):
96 expected = {}
97 for name in names:
98 expected.update(self.settings[name])
99 return expected
101 def compare_output(self, result, expected):
102 """`result` and `expected` should both be dicts."""
103 self.assertTrue('record_dependencies' in result)
104 if 'record_dependencies' not in expected:
105 # Delete it if we don't want to test it.
106 del result['record_dependencies']
107 result = pprint.pformat(result) + '\n'
108 expected = pprint.pformat(expected) + '\n'
109 try:
110 self.assertEqual(result, expected)
111 except AssertionError:
112 print >>sys.stderr, '\n%s\n' % (self,)
113 print >>sys.stderr, '-: expected\n+: result'
114 print >>sys.stderr, ''.join(self.compare(expected.splitlines(1),
115 result.splitlines(1)))
116 raise
118 def test_nofiles(self):
119 self.compare_output(self.files_settings(),
120 self.expected_settings())
122 def test_old(self):
123 self.compare_output(self.files_settings('old'),
124 self.expected_settings('old'))
126 def test_one(self):
127 self.compare_output(self.files_settings('one'),
128 self.expected_settings('one'))
130 def test_multiple(self):
131 self.compare_output(self.files_settings('one', 'two'),
132 self.expected_settings('one', 'two'))
134 def test_old_and_new(self):
135 self.compare_output(self.files_settings('old', 'two'),
136 self.expected_settings('old', 'two'))
138 def test_list(self):
139 self.compare_output(self.files_settings('list'),
140 self.expected_settings('list'))
142 def test_list2(self):
143 self.compare_output(self.files_settings('list', 'list2'),
144 self.expected_settings('list2'))
146 def test_error_handler(self):
147 self.compare_output(self.files_settings('error'),
148 self.expected_settings('error'))
151 class ConfigEnvVarFileTests(ConfigFileTests):
154 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
155 environment variable and the standard Docutils config file mechanism.
158 def setUp(self):
159 ConfigFileTests.setUp(self)
160 self.orig_environ = os.environ
161 os.environ = os.environ.copy()
163 def files_settings(self, *names):
164 files = [self.config_files[name] for name in names]
165 os.environ['DOCUTILSCONFIG'] = os.pathsep.join(files)
166 settings = self.option_parser.get_standard_config_settings()
167 return settings.__dict__
169 def tearDown(self):
170 os.environ = self.orig_environ
173 class HelperFunctionsTests(unittest.TestCase):
175 pathdict = {'foo': 'hallo', 'ham': u'h\xE4m', 'spam': u'spam'}
176 keys = ['foo', 'ham']
178 def test_make_paths_absolute(self):
179 pathdict = self.pathdict.copy()
180 frontend.make_paths_absolute(pathdict, self.keys, base_path='base')
181 self.assertEqual(pathdict['foo'], os.path.abspath('base/hallo'))
182 self.assertEqual(pathdict['ham'], os.path.abspath(u'base/h\xE4m'))
183 # not touched, because key not in keys:
184 self.assertEqual(pathdict['spam'], u'spam')
186 def test_make_paths_absolute_cwd(self):
187 # With base_path None, the cwd is used as base path.
188 # Settings values may-be `unicode` instances, therefore
189 # os.getcwdu() is used and the converted path is a unicode instance:
190 pathdict = self.pathdict.copy()
191 frontend.make_paths_absolute(pathdict, self.keys)
192 self.assertEqual(pathdict['foo'], os.path.abspath(u'hallo'))
193 self.assertEqual(pathdict['ham'], os.path.abspath(u'h\xE4m'))
194 # not touched, because key not in keys:
195 self.assertEqual(pathdict['spam'], u'spam')
197 if __name__ == '__main__':
198 unittest.main()