4 # Author: Martin Blais <blais@furius.ca>
5 # Copyright: This module has been placed in the public domain.
8 Test the `Publisher` facade and the ``publish_*`` convenience functions.
12 import DocutilsTestSupport
# must be imported before docutils
14 from docutils
import core
, nodes
, io
15 from docutils
._compat
import u_prefix
22 This is a test document with a broken reference: nonexistent_
24 pseudoxml_output
= b
"""\
25 <document ids="test-document" names="test\ document" source="<string>" title="Test Document">
29 This is a test document with a broken reference: \n\
30 <problematic ids="id2" refid="id1">
32 <section classes="system-messages">
34 Docutils System Messages
35 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
37 Unknown target name: "nonexistent".
39 exposed_pseudoxml_output
= (b
"""\
40 <document ids="test-document" internal:refnames="{"""
42 + b
"""\'nonexistent\': [<reference: <#text: \'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
46 This is a test document with a broken reference: \n\
47 <problematic ids="id2" refid="id1">
49 <section classes="system-messages">
51 Docutils System Messages
52 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
54 Unknown target name: "nonexistent".
55 """) # % u_prefix # %-expansion not supported in bytes in 3.3 and 3.4
58 class PublisherTests(DocutilsTestSupport
.StandardTestCase
):
60 def test_input_error_handling(self
):
61 # core.publish_cmdline(argv=['nonexisting/path'])
62 # exits with a short message, if `traceback` is False,
64 # pass IOErrors to calling application if `traceback` is True
66 core
.publish_cmdline(argv
=['nonexisting/path'],
67 settings_overrides
={'traceback': True})
69 self
.assertTrue(isinstance(e
, io
.InputError
))
72 def test_output_error_handling(self
):
73 # pass IOErrors to calling application if `traceback` is True
75 core
.publish_cmdline(argv
=['data/include.txt', 'nonexisting/path'],
76 settings_overrides
={'traceback': True})
78 self
.assertTrue(isinstance(e
, io
.OutputError
))
81 class PublishDoctreeTestCase(DocutilsTestSupport
.StandardTestCase
, docutils
.SettingsSpec
):
83 settings_default_overrides
= {
84 '_disable_config': True,
85 'warning_stream': io
.NullOutput()}
87 def test_publish_doctree(self
):
88 # Test `publish_doctree` and `publish_from_doctree`.
90 # Produce the document tree.
91 doctree
= core
.publish_doctree(
92 source
=test_document
, reader_name
='standalone',
93 parser_name
='restructuredtext', settings_spec
=self
,
94 settings_overrides
={'expose_internals':
95 ['refnames', 'do_not_expose'],
97 self
.assertTrue(isinstance(doctree
, nodes
.document
))
99 # Confirm that transforms have been applied (in this case, the
100 # DocTitle transform):
101 self
.assertTrue(isinstance(doctree
[0], nodes
.title
))
102 self
.assertTrue(isinstance(doctree
[1], nodes
.paragraph
))
103 # Confirm that the Messages transform has not yet been applied:
104 self
.assertEqual(len(doctree
), 2)
106 # The `do_not_expose` attribute may not show up in the
107 # pseudoxml output because the expose_internals transform may
108 # not be applied twice.
109 doctree
.do_not_expose
= 'test'
110 # Write out the document:
111 output
= core
.publish_from_doctree(
112 doctree
, writer_name
='pseudoxml',
114 settings_overrides
={'expose_internals':
115 ['refnames', 'do_not_expose'],
117 self
.assertEqual(output
, exposed_pseudoxml_output
)
119 # Test publishing parts using document as the source.
120 parts
= core
.publish_parts(
121 reader_name
='doctree', source_class
=io
.DocTreeInput
,
122 source
=doctree
, source_path
='test', writer_name
='html',
124 self
.assertTrue(isinstance(parts
, dict))
126 def test_publish_pickle(self
):
127 # Test publishing a document tree with pickling and unpickling.
129 # Produce the document tree.
130 doctree
= core
.publish_doctree(
131 source
=test_document
,
132 reader_name
='standalone',
133 parser_name
='restructuredtext',
135 self
.assertTrue(isinstance(doctree
, nodes
.document
))
137 # Pickle the document. Note: if this fails, some unpickleable
138 # reference has been added somewhere within the document tree.
139 # If so, you need to fix that.
141 # Note: Please do not remove this test, this is an important
142 # requirement, applications will be built on the assumption
143 # that we can pickle the document.
145 # Remove the reporter and the transformer before pickling.
146 doctree
.reporter
= None
147 doctree
.transformer
= None
149 doctree_pickled
= pickle
.dumps(doctree
)
150 self
.assertTrue(isinstance(doctree_pickled
, bytes
))
153 # Unpickle the document.
154 doctree_zombie
= pickle
.loads(doctree_pickled
)
155 self
.assertTrue(isinstance(doctree_zombie
, nodes
.document
))
157 # Write out the document:
158 output
= core
.publish_from_doctree(
159 doctree_zombie
, writer_name
='pseudoxml',
161 self
.assertEqual(output
, pseudoxml_output
)
164 if __name__
== '__main__':