3 # Author: David Goodger
4 # Contact: goodger@python.org
7 # Copyright: This module has been placed in the public domain.
10 Tests of runtime settings.
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
)
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')}
43 'old': {'datestamp': '%Y-%m-%d %H:%M UTC',
46 'python_home': 'http://www.python.org',
48 'stylesheet_path': fixpath('data/stylesheets/pep.css'),
49 'template': fixpath('data/pep-html-template')},
50 'one': {'datestamp': '%Y-%m-%d %H:%M UTC',
53 'python_home': 'http://www.python.org',
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."""
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()
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
):
82 expected
.update(self
.settings
[name
])
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'
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)))
98 def test_nofiles(self
):
99 self
.compare_output(self
.files_settings(),
100 self
.expected_settings())
103 self
.compare_output(self
.files_settings('old'),
104 self
.expected_settings('old'))
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'))
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.
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
__
150 os
.environ
= self
.orig_environ
153 if __name__
== '__main__':