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 b
, bytes
, 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="{%s\'nonexistent\': [<reference: <#text: \'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
44 This is a test document with a broken reference: \n\
45 <problematic ids="id2" refid="id1">
47 <section classes="system-messages">
49 Docutils System Messages
50 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
52 Unknown target name: "nonexistent".
56 class PublisherTests(DocutilsTestSupport
.StandardTestCase
):
58 def test_input_error_handling(self
):
59 # core.publish_cmdline(argv=['nonexisting/path'])
60 # exits with a short message, if `traceback` is False,
62 # pass IOErrors to calling application if `traceback` is True
64 core
.publish_cmdline(argv
=['nonexisting/path'],
65 settings_overrides
={'traceback': True})
67 self
.assertTrue(isinstance(e
, io
.InputError
))
70 def test_output_error_handling(self
):
71 # pass IOErrors to calling application if `traceback` is True
73 core
.publish_cmdline(argv
=['data/include.txt', 'nonexisting/path'],
74 settings_overrides
={'traceback': True})
76 self
.assertTrue(isinstance(e
, io
.OutputError
))
79 class PublishDoctreeTestCase(DocutilsTestSupport
.StandardTestCase
, docutils
.SettingsSpec
):
81 settings_default_overrides
= {
82 '_disable_config': True,
83 'warning_stream': io
.NullOutput()}
85 def test_publish_doctree(self
):
86 # Test `publish_doctree` and `publish_from_doctree`.
88 # Produce the document tree.
89 doctree
= core
.publish_doctree(
90 source
=test_document
, reader_name
='standalone',
91 parser_name
='restructuredtext', settings_spec
=self
,
92 settings_overrides
={'expose_internals':
93 ['refnames', 'do_not_expose'],
95 self
.assertTrue(isinstance(doctree
, nodes
.document
))
97 # Confirm that transforms have been applied (in this case, the
98 # DocTitle transform):
99 self
.assertTrue(isinstance(doctree
[0], nodes
.title
))
100 self
.assertTrue(isinstance(doctree
[1], nodes
.paragraph
))
101 # Confirm that the Messages transform has not yet been applied:
102 self
.assertEqual(len(doctree
), 2)
104 # The `do_not_expose` attribute may not show up in the
105 # pseudoxml output because the expose_internals transform may
106 # not be applied twice.
107 doctree
.do_not_expose
= 'test'
108 # Write out the document:
109 output
= core
.publish_from_doctree(
110 doctree
, writer_name
='pseudoxml',
112 settings_overrides
={'expose_internals':
113 ['refnames', 'do_not_expose'],
115 self
.assertEqual(output
, exposed_pseudoxml_output
)
117 # Test publishing parts using document as the source.
118 parts
= core
.publish_parts(
119 reader_name
='doctree', source_class
=io
.DocTreeInput
,
120 source
=doctree
, source_path
='test', writer_name
='html',
122 self
.assertTrue(isinstance(parts
, dict))
124 def test_publish_pickle(self
):
125 # Test publishing a document tree with pickling and unpickling.
127 # Produce the document tree.
128 doctree
= core
.publish_doctree(
129 source
=test_document
,
130 reader_name
='standalone',
131 parser_name
='restructuredtext',
133 self
.assertTrue(isinstance(doctree
, nodes
.document
))
135 # Pickle the document. Note: if this fails, some unpickleable
136 # reference has been added somewhere within the document tree.
137 # If so, you need to fix that.
139 # Note: Please do not remove this test, this is an important
140 # requirement, applications will be built on the assumption
141 # that we can pickle the document.
143 # Remove the reporter and the transformer before pickling.
144 doctree
.reporter
= None
145 doctree
.transformer
= None
147 doctree_pickled
= pickle
.dumps(doctree
)
148 self
.assertTrue(isinstance(doctree_pickled
, bytes
))
151 # Unpickle the document.
152 doctree_zombie
= pickle
.loads(doctree_pickled
)
153 self
.assertTrue(isinstance(doctree_zombie
, nodes
.document
))
155 # Write out the document:
156 output
= core
.publish_from_doctree(
157 doctree_zombie
, writer_name
='pseudoxml',
159 self
.assertEqual(output
, pseudoxml_output
)
162 if __name__
== '__main__':