Fixes in linepsacing doc.
[docutils.git] / test / test_settings.py
blob9b889378e54296cec0722eaaea13c646092c3da6
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 difflib
16 import pprint
17 import warnings
18 import unittest
19 from docutils import frontend
20 from docutils.writers import html4css1
21 from docutils.writers import pep_html
24 warnings.filterwarnings(action='ignore',
25 category=frontend.ConfigDeprecationWarning)
27 def fixpath(path):
28 return os.path.abspath(os.path.join(mydir, path))
30 mydir = os.path.dirname(fixpath.func_code.co_filename)
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_path': fixpath('data/stylesheets/pep.css'),
49 'template': fixpath('data/pep-html-template')},
50 'one': {'datestamp': '%Y-%m-%d %H:%M UTC',
51 'generator': 1,
52 'no_random': 1,
53 'python_home': 'http://www.python.org',
54 'source_link': 1,
55 'stylesheet_path': fixpath('data/stylesheets/pep.css'),
56 'template': fixpath('data/pep-html-template')},
57 'two': {'generator': 0,
58 'stylesheet_path': fixpath('data/test.css')},
59 'list': {'expose_internals': ['a', 'b', 'c', 'd', 'e']},
60 'list2': {'expose_internals': ['a', 'b', 'c', 'd', 'e', 'f']},
61 'error': {'error_encoding': 'ascii',
62 'error_encoding_error_handler': 'strict'},
65 compare = difflib.Differ().compare
66 """Comparison method shared by all tests."""
68 def setUp(self):
69 self.option_parser = frontend.OptionParser(
70 components=(pep_html.Writer,), read_config_files=None)
72 def files_settings(self, *names):
73 settings = frontend.Values()
74 for name in names:
75 settings.update(self.option_parser.get_config_file_settings(
76 self.config_files[name]), self.option_parser)
77 return settings.__dict__
79 def expected_settings(self, *names):
80 expected = {}
81 for name in names:
82 expected.update(self.settings[name])
83 return expected
85 def compare_output(self, result, expected):
86 """`result` and `expected` should both be dicts."""
87 result = pprint.pformat(result) + '\n'
88 expected = pprint.pformat(expected) + '\n'
89 try:
90 self.assertEquals(result, expected)
91 except AssertionError:
92 print >>sys.stderr, '\n%s\n' % (self,)
93 print >>sys.stderr, '-: expected\n+: result'
94 print >>sys.stderr, ''.join(self.compare(expected.splitlines(1),
95 result.splitlines(1)))
96 raise
98 def test_nofiles(self):
99 self.compare_output(self.files_settings(),
100 self.expected_settings())
102 def test_old(self):
103 self.compare_output(self.files_settings('old'),
104 self.expected_settings('old'))
106 def test_one(self):
107 self.compare_output(self.files_settings('one'),
108 self.expected_settings('one'))
110 def test_multiple(self):
111 self.compare_output(self.files_settings('one', 'two'),
112 self.expected_settings('one', 'two'))
114 def test_old_and_new(self):
115 self.compare_output(self.files_settings('old', 'two'),
116 self.expected_settings('old', 'two'))
118 def test_list(self):
119 self.compare_output(self.files_settings('list'),
120 self.expected_settings('list'))
122 def test_list2(self):
123 self.compare_output(self.files_settings('list', 'list2'),
124 self.expected_settings('list2'))
126 def test_error_handler(self):
127 self.compare_output(self.files_settings('error'),
128 self.expected_settings('error'))
131 class ConfigEnvVarFileTests(ConfigFileTests):
134 Repeats the tests of `ConfigFileTests` using the ``DOCUTILSCONFIG``
135 environment variable and the standard Docutils config file mechanism.
138 def setUp(self):
139 ConfigFileTests.setUp(self)
140 self.orig_environ = os.environ
141 os.environ = os.environ.copy()
143 def files_settings(self, *names):
144 files = [self.config_files[name] for name in names]
145 os.environ['DOCUTILSCONFIG'] = os.pathsep.join(files)
146 settings = self.option_parser.get_standard_config_settings()
147 return settings.__dict__
149 def tearDown(self):
150 os.environ = self.orig_environ
153 if __name__ == '__main__':
154 unittest.main()