| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Test the `Publisher` facade and the ``publish_*`` convenience functions. |
| 9 |
""" |
| 10 |
import pickle |
| 11 |
from pathlib import Path |
| 12 |
import sys |
| 13 |
import unittest |
| 14 |
|
| 15 |
if __name__ == '__main__': |
| 16 |
|
| 17 |
|
| 18 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 19 |
|
| 20 |
import docutils |
| 21 |
import docutils.parsers.null |
| 22 |
from docutils import core, nodes, parsers, readers, writers |
| 23 |
from docutils.writers import html4css1, odf_odt, pseudoxml |
| 24 |
|
| 25 |
|
| 26 |
DATA_ROOT = Path(__file__).parent / 'data' |
| 27 |
|
| 28 |
|
| 29 |
test_document = """\ |
| 30 |
Test Document |
| 31 |
============= |
| 32 |
|
| 33 |
This is a test document with a broken reference: nonexistent_ |
| 34 |
""" |
| 35 |
pseudoxml_output = """\ |
| 36 |
<document ids="test-document" names="test\\ document" source="<string>" title="Test Document"> |
| 37 |
<title> |
| 38 |
Test Document |
| 39 |
<paragraph> |
| 40 |
This is a test document with a broken reference: \n\ |
| 41 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 42 |
nonexistent_ |
| 43 |
<section classes="system-messages"> |
| 44 |
<title> |
| 45 |
Docutils System Messages |
| 46 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="4" source="<string>" type="ERROR"> |
| 47 |
<paragraph> |
| 48 |
Unknown target name: "nonexistent". |
| 49 |
""" |
| 50 |
exposed_pseudoxml_output = """\ |
| 51 |
<document ids="test-document" internal:refnames="{'nonexistent': [<reference: <#text: 'nonexistent'>>]}" names="test\\ document" source="<string>" title="Test Document"> |
| 52 |
<title> |
| 53 |
Test Document |
| 54 |
<paragraph> |
| 55 |
This is a test document with a broken reference: \n\ |
| 56 |
<problematic ids="problematic-1" refid="system-message-1"> |
| 57 |
nonexistent_ |
| 58 |
<section classes="system-messages"> |
| 59 |
<title> |
| 60 |
Docutils System Messages |
| 61 |
<system_message backrefs="problematic-1" ids="system-message-1" level="3" line="4" source="<string>" type="ERROR"> |
| 62 |
<paragraph> |
| 63 |
Unknown target name: "nonexistent". |
| 64 |
""" |
| 65 |
|
| 66 |
|
| 67 |
class PublisherTests(unittest.TestCase): |
| 68 |
|
| 69 |
def test__init__(self): |
| 70 |
reader = readers.standalone.Reader() |
| 71 |
parser = parsers.null.Parser() |
| 72 |
writer = writers.null.Writer() |
| 73 |
|
| 74 |
publisher = core.Publisher(reader, parser, writer) |
| 75 |
self.assertEqual(publisher.reader, reader) |
| 76 |
self.assertEqual(publisher.parser, parser) |
| 77 |
self.assertEqual(publisher.writer, writer) |
| 78 |
|
| 79 |
publisher = core.Publisher('standalone', parser, writer) |
| 80 |
self.assertTrue(isinstance(publisher.reader, |
| 81 |
readers.standalone.Reader)) |
| 82 |
self.assertEqual(publisher.parser, parser) |
| 83 |
self.assertEqual(publisher.writer, writer) |
| 84 |
|
| 85 |
publisher = core.Publisher(reader, 'rst', writer) |
| 86 |
self.assertEqual(publisher.reader, reader) |
| 87 |
self.assertTrue(isinstance(publisher.parser, parsers.rst.Parser)) |
| 88 |
self.assertEqual(publisher.writer, writer) |
| 89 |
|
| 90 |
publisher = core.Publisher(reader, parser, 'latex') |
| 91 |
self.assertEqual(publisher.reader, reader) |
| 92 |
self.assertEqual(publisher.parser, parser) |
| 93 |
self.assertTrue(isinstance(publisher.writer, writers.latex2e.Writer)) |
| 94 |
|
| 95 |
def test_set_reader(self): |
| 96 |
publisher = core.Publisher(parser='null') |
| 97 |
parser = parsers.null.Parser() |
| 98 |
|
| 99 |
publisher.set_reader('standalone', parser='rst') |
| 100 |
self.assertTrue(isinstance(publisher.parser, parsers.rst.Parser)) |
| 101 |
|
| 102 |
self.assertEqual(publisher.reader.parser, publisher.parser) |
| 103 |
|
| 104 |
with self.assertWarnsRegex(PendingDeprecationWarning, |
| 105 |
'Argument "parser_name" will be removed'): |
| 106 |
publisher.set_reader('standalone', parser=None, parser_name='rst') |
| 107 |
self.assertTrue(isinstance(publisher.parser, parsers.rst.Parser)) |
| 108 |
self.assertEqual(publisher.reader.parser, publisher.parser) |
| 109 |
|
| 110 |
with self.assertWarns(PendingDeprecationWarning): |
| 111 |
publisher.set_reader('standalone', parser, parser_name='rst') |
| 112 |
self.assertEqual(publisher.parser, parser) |
| 113 |
self.assertEqual(publisher.reader.parser, publisher.parser) |
| 114 |
|
| 115 |
publisher.set_reader('standalone') |
| 116 |
self.assertTrue(isinstance(publisher.parser, parsers.null.Parser)) |
| 117 |
self.assertEqual(publisher.reader.parser, publisher.parser) |
| 118 |
|
| 119 |
def test_set_components(self): |
| 120 |
publisher = core.Publisher() |
| 121 |
reader = readers.standalone.Reader() |
| 122 |
parser = parsers.null.Parser() |
| 123 |
writer = writers.null.Writer() |
| 124 |
|
| 125 |
with self.assertWarnsRegex(PendingDeprecationWarning, |
| 126 |
'set_components.* will be removed'): |
| 127 |
publisher.set_components('pep', 'rst', 'odt') |
| 128 |
self.assertTrue(isinstance(publisher.reader, readers.pep.Reader)) |
| 129 |
self.assertTrue(isinstance(publisher.parser, parsers.rst.Parser)) |
| 130 |
self.assertTrue(isinstance(publisher.writer, writers.odf_odt.Writer)) |
| 131 |
|
| 132 |
publisher = core.Publisher(reader, parser, writer) |
| 133 |
with self.assertWarns(PendingDeprecationWarning): |
| 134 |
publisher.set_components('standalone', 'xml', 'odt') |
| 135 |
self.assertEqual(publisher.reader, reader) |
| 136 |
self.assertEqual(publisher.parser, parser) |
| 137 |
self.assertEqual(publisher.writer, writer) |
| 138 |
|
| 139 |
def test_set_destination(self): |
| 140 |
|
| 141 |
publisher = core.Publisher() |
| 142 |
publisher.get_settings(output_path='out_name', _destination='other') |
| 143 |
with self.assertRaises(SystemExit): |
| 144 |
publisher.set_destination() |
| 145 |
|
| 146 |
publisher.settings._destination = 'out_name' |
| 147 |
publisher.set_destination() |
| 148 |
|
| 149 |
publisher.set_destination(destination_path='winning_dest') |
| 150 |
|
| 151 |
self.assertEqual(publisher.settings.output_path, 'winning_dest') |
| 152 |
self.assertEqual(publisher.settings.output, 'winning_dest') |
| 153 |
self.assertEqual(publisher.settings._destination, 'winning_dest') |
| 154 |
|
| 155 |
|
| 156 |
class ConvenienceFunctionTests(unittest.TestCase): |
| 157 |
maxDiff = None |
| 158 |
|
| 159 |
settings = {'_disable_config': True, |
| 160 |
'datestamp': False} |
| 161 |
|
| 162 |
def test_publish_cmdline(self): |
| 163 |
|
| 164 |
with self.assertWarns(PendingDeprecationWarning): |
| 165 |
core.publish_cmdline(writer_name='null', |
| 166 |
argv=[(DATA_ROOT/'include.rst').as_posix()], |
| 167 |
settings_overrides={'traceback': True}) |
| 168 |
|
| 169 |
def test_input_error_handling(self): |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
with self.assertRaises(IOError): |
| 174 |
core.publish_cmdline(argv=['nonexisting/path'], |
| 175 |
settings_overrides={'traceback': True}) |
| 176 |
|
| 177 |
def test_output_error_handling(self): |
| 178 |
|
| 179 |
with self.assertRaises(docutils.io.OutputError): |
| 180 |
core.publish_cmdline(argv=[(DATA_ROOT/'include.rst').as_posix(), |
| 181 |
'nonexisting/path'], |
| 182 |
settings_overrides={'traceback': True}) |
| 183 |
|
| 184 |
def test_destination_output_conflict(self): |
| 185 |
|
| 186 |
settings = {'output_path': 'out_name'} |
| 187 |
with self.assertRaises(SystemExit): |
| 188 |
core.publish_cmdline(argv=['-', 'dest_name'], |
| 189 |
settings_overrides=settings) |
| 190 |
|
| 191 |
def test_publish_string_input_encoding(self): |
| 192 |
"""Test handling of encoded input.""" |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
settings = self.settings | {'input_encoding': 'utf-16', |
| 197 |
'output_encoding': 'unicode'} |
| 198 |
source = 'test → me' |
| 199 |
expected = ('<document source="<string>">\n' |
| 200 |
' <paragraph>\n' |
| 201 |
' test → me\n') |
| 202 |
output = core.publish_string(source.encode('utf-16'), |
| 203 |
settings_overrides=settings) |
| 204 |
self.assertEqual(expected, output) |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
source = '.. encoding: latin1\n\nGrüße' |
| 209 |
settings['input_encoding'] = None |
| 210 |
with self.assertWarnsRegex(DeprecationWarning, 'auto-detection'): |
| 211 |
output = core.publish_string(source.encode('latin1'), |
| 212 |
settings_overrides=settings) |
| 213 |
self.assertTrue(output.endswith('Grüße\n')) |
| 214 |
|
| 215 |
def test_publish_string_output_encoding(self): |
| 216 |
settings = self.settings | {'output_encoding': 'latin1'} |
| 217 |
settings['output_encoding_error_handler'] = 'replace' |
| 218 |
source = 'Grüß → dich' |
| 219 |
expected = ('<document source="<string>">\n' |
| 220 |
' <paragraph>\n' |
| 221 |
' Grüß → dich\n') |
| 222 |
|
| 223 |
output = bytes(core.publish_string(source, |
| 224 |
settings_overrides=settings)) |
| 225 |
self.assertEqual(expected.encode('latin1', 'replace'), output) |
| 226 |
|
| 227 |
def test_publish_string_output_encoding_odt(self): |
| 228 |
"""The ODT writer generates a zip archive, not a `str`. |
| 229 |
|
| 230 |
TODO: return `str` with document as "flat XML" (.fodt). |
| 231 |
""" |
| 232 |
settings = self.settings | {'output_encoding': 'unicode', |
| 233 |
'warning_stream': ''} |
| 234 |
with self.assertRaisesRegex(docutils.utils.SystemMessage, |
| 235 |
'The ODT writer returns `bytes` '): |
| 236 |
core.publish_string('test', writer=odf_odt.Writer(), |
| 237 |
settings_overrides=settings) |
| 238 |
|
| 239 |
def test_publish_string_deprecation_warning(self): |
| 240 |
"""The "*_name" arguments are deprecated.""" |
| 241 |
source = 'test → me' |
| 242 |
with self.assertWarns(PendingDeprecationWarning): |
| 243 |
output = core.publish_string(source, writer_name='xml') |
| 244 |
|
| 245 |
self.assertTrue(output.decode('utf-8').startswith( |
| 246 |
'<?xml version="1.0" encoding="utf-8"?>')) |
| 247 |
|
| 248 |
|
| 249 |
class PublishDoctreeTestCase(unittest.TestCase, docutils.SettingsSpec): |
| 250 |
|
| 251 |
settings_default_overrides = { |
| 252 |
'_disable_config': True, |
| 253 |
'warning_stream': docutils.io.NullOutput(), |
| 254 |
'output_encoding': 'unicode'} |
| 255 |
|
| 256 |
def test_publish_doctree(self): |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
with self.assertWarns(PendingDeprecationWarning): |
| 261 |
doctree = core.publish_doctree( |
| 262 |
source=test_document, |
| 263 |
parser_name='restructuredtext', settings_spec=self, |
| 264 |
settings_overrides={'expose_internals': |
| 265 |
['refnames', 'do_not_expose'], |
| 266 |
'report_level': 5}) |
| 267 |
self.assertTrue(isinstance(doctree, nodes.document)) |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
self.assertTrue(isinstance(doctree[0], nodes.title)) |
| 272 |
self.assertTrue(isinstance(doctree[1], nodes.paragraph)) |
| 273 |
|
| 274 |
self.assertEqual(2, len(doctree)) |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
doctree.do_not_expose = 'test' |
| 280 |
|
| 281 |
output = core.publish_from_doctree( |
| 282 |
doctree, |
| 283 |
writer=pseudoxml.Writer(), |
| 284 |
settings_spec=self, |
| 285 |
settings_overrides={'expose_internals': |
| 286 |
['refnames', 'do_not_expose'], |
| 287 |
'report_level': 1, |
| 288 |
'output_encoding': 'unicode'}) |
| 289 |
self.assertEqual(exposed_pseudoxml_output, output) |
| 290 |
|
| 291 |
|
| 292 |
parts = core.publish_parts( |
| 293 |
reader='doctree', source_class=docutils.io.DocTreeInput, |
| 294 |
source=doctree, source_path='test', writer=html4css1.Writer(), |
| 295 |
settings_spec=self) |
| 296 |
self.assertTrue(isinstance(parts, dict)) |
| 297 |
|
| 298 |
def test_publish_pickle(self): |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
doctree = core.publish_doctree( |
| 303 |
source=test_document, |
| 304 |
settings_spec=self) |
| 305 |
self.assertTrue(isinstance(doctree, nodes.document)) |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
doctree.reporter = None |
| 317 |
doctree.transformer = None |
| 318 |
|
| 319 |
doctree_pickled = pickle.dumps(doctree) |
| 320 |
self.assertTrue(isinstance(doctree_pickled, bytes)) |
| 321 |
del doctree |
| 322 |
|
| 323 |
|
| 324 |
doctree_zombie = pickle.loads(doctree_pickled) |
| 325 |
self.assertTrue(isinstance(doctree_zombie, nodes.document)) |
| 326 |
|
| 327 |
|
| 328 |
with self.assertWarnsRegex(PendingDeprecationWarning, |
| 329 |
'Argument "writer_name" will be removed '): |
| 330 |
output = core.publish_from_doctree(doctree_zombie, |
| 331 |
writer_name='pseudoxml', |
| 332 |
settings_spec=self) |
| 333 |
self.assertEqual(pseudoxml_output, output) |
| 334 |
|
| 335 |
|
| 336 |
if __name__ == '__main__': |
| 337 |
unittest.main() |