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