| 1 |
"""Test the other directives.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
from docutils import nodes |
| 10 |
|
| 11 |
from sphinx import addnodes |
| 12 |
from sphinx.testing import restructuredtext |
| 13 |
from sphinx.testing.util import assert_node |
| 14 |
|
| 15 |
from tests.utils import extract_node |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from sphinx.testing.util import SphinxTestApp |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 22 |
def test_toctree(app): |
| 23 |
text = '.. toctree::\n\n foo\n bar/index\n baz\n' |
| 24 |
|
| 25 |
app.env.find_files(app.config, app.builder) |
| 26 |
doctree = restructuredtext.parse(app, text, 'index') |
| 27 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 28 |
assert_node( |
| 29 |
extract_node(doctree, 0, 0), |
| 30 |
entries=[(None, 'foo'), (None, 'bar/index'), (None, 'baz')], |
| 31 |
includefiles=['foo', 'bar/index', 'baz'], |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 36 |
def test_relative_toctree(app): |
| 37 |
text = '.. toctree::\n\n bar_1\n bar_2\n bar_3\n ../quux\n' |
| 38 |
|
| 39 |
app.env.find_files(app.config, app.builder) |
| 40 |
doctree = restructuredtext.parse(app, text, 'bar/index') |
| 41 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 42 |
assert_node( |
| 43 |
extract_node(doctree, 0, 0), |
| 44 |
entries=[ |
| 45 |
(None, 'bar/bar_1'), |
| 46 |
(None, 'bar/bar_2'), |
| 47 |
(None, 'bar/bar_3'), |
| 48 |
(None, 'quux'), |
| 49 |
], |
| 50 |
includefiles=['bar/bar_1', 'bar/bar_2', 'bar/bar_3', 'quux'], |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 55 |
def test_toctree_urls_and_titles(app): |
| 56 |
text = ( |
| 57 |
'.. toctree::\n' |
| 58 |
'\n' |
| 59 |
' Sphinx <https://www.sphinx-doc.org/>\n' |
| 60 |
' https://readthedocs.org/\n' |
| 61 |
' The BAR <bar/index>\n' |
| 62 |
) |
| 63 |
|
| 64 |
app.env.find_files(app.config, app.builder) |
| 65 |
doctree = restructuredtext.parse(app, text, 'index') |
| 66 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 67 |
assert_node( |
| 68 |
extract_node(doctree, 0, 0), |
| 69 |
entries=[ |
| 70 |
('Sphinx', 'https://www.sphinx-doc.org/'), |
| 71 |
(None, 'https://readthedocs.org/'), |
| 72 |
('The BAR', 'bar/index'), |
| 73 |
], |
| 74 |
includefiles=['bar/index'], |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 79 |
def test_toctree_glob(app): |
| 80 |
text = '.. toctree::\n :glob:\n\n *\n' |
| 81 |
|
| 82 |
app.env.find_files(app.config, app.builder) |
| 83 |
doctree = restructuredtext.parse(app, text, 'index') |
| 84 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 85 |
assert_node( |
| 86 |
extract_node(doctree, 0, 0), |
| 87 |
entries=[(None, 'baz'), (None, 'foo'), (None, 'quux')], |
| 88 |
includefiles=['baz', 'foo', 'quux'], |
| 89 |
) |
| 90 |
|
| 91 |
# give both docname and glob (case1) |
| 92 |
text = '.. toctree::\n :glob:\n\n foo\n *\n' |
| 93 |
|
| 94 |
app.env.find_files(app.config, app.builder) |
| 95 |
doctree = restructuredtext.parse(app, text, 'index') |
| 96 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 97 |
assert_node( |
| 98 |
extract_node(doctree, 0, 0), |
| 99 |
entries=[(None, 'foo'), (None, 'baz'), (None, 'quux')], |
| 100 |
includefiles=['foo', 'baz', 'quux'], |
| 101 |
) |
| 102 |
|
| 103 |
# give both docname and glob (case2) |
| 104 |
text = '.. toctree::\n :glob:\n\n *\n foo\n' |
| 105 |
|
| 106 |
app.env.find_files(app.config, app.builder) |
| 107 |
doctree = restructuredtext.parse(app, text, 'index') |
| 108 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 109 |
assert_node( |
| 110 |
extract_node(doctree, 0, 0), |
| 111 |
entries=[(None, 'baz'), (None, 'foo'), (None, 'quux'), (None, 'foo')], |
| 112 |
includefiles=['baz', 'foo', 'quux', 'foo'], |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 117 |
def test_toctree_glob_and_url(app): |
| 118 |
text = '.. toctree::\n :glob:\n\n https://example.com/?q=sphinx\n' |
| 119 |
|
| 120 |
app.env.find_files(app.config, app.builder) |
| 121 |
doctree = restructuredtext.parse(app, text, 'index') |
| 122 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 123 |
assert_node( |
| 124 |
extract_node(doctree, 0, 0), |
| 125 |
entries=[(None, 'https://example.com/?q=sphinx')], |
| 126 |
includefiles=[], |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 131 |
def test_reversed_toctree(app): |
| 132 |
text = '.. toctree::\n :reversed:\n\n foo\n bar/index\n baz\n' |
| 133 |
|
| 134 |
app.env.find_files(app.config, app.builder) |
| 135 |
doctree = restructuredtext.parse(app, text, 'index') |
| 136 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 137 |
assert_node( |
| 138 |
extract_node(doctree, 0, 0), |
| 139 |
entries=[(None, 'baz'), (None, 'bar/index'), (None, 'foo')], |
| 140 |
includefiles=['baz', 'bar/index', 'foo'], |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 145 |
def test_toctree_class(app): |
| 146 |
text = '.. toctree::\n :class: custom-toc\n\n foo\n' |
| 147 |
app.env.find_files(app.config, app.builder) |
| 148 |
doctree = restructuredtext.parse(app, text, 'index') |
| 149 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 150 |
assert isinstance(doctree[0], nodes.compound) |
| 151 |
assert doctree[0].attributes['classes'] == ['toctree-wrapper', 'custom-toc'] |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.sphinx('html', testroot='toctree-glob') |
| 155 |
def test_toctree_twice(app): |
| 156 |
text = '.. toctree::\n\n foo\n foo\n' |
| 157 |
|
| 158 |
app.env.find_files(app.config, app.builder) |
| 159 |
doctree = restructuredtext.parse(app, text, 'index') |
| 160 |
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree]) |
| 161 |
assert_node( |
| 162 |
extract_node(doctree, 0, 0), |
| 163 |
entries=[(None, 'foo'), (None, 'foo')], |
| 164 |
includefiles=['foo', 'foo'], |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.sphinx('html', testroot='directive-include') |
| 169 |
def test_include_include_read_event(app: SphinxTestApp) -> None: |
| 170 |
sources_reported = [] |
| 171 |
|
| 172 |
def source_read_handler(_app, relative_path, parent_docname, source): |
| 173 |
sources_reported.append((relative_path, parent_docname, source[0])) |
| 174 |
|
| 175 |
app.connect('include-read', source_read_handler) |
| 176 |
text = """\ |
| 177 |
.. include:: baz/baz.rst |
| 178 |
:start-line: 4 |
| 179 |
.. include:: text.txt |
| 180 |
:literal: |
| 181 |
.. include:: bar.txt |
| 182 |
""" |
| 183 |
app.env.find_files(app.config, app.builder) |
| 184 |
restructuredtext.parse(app, text, 'index') |
| 185 |
|
| 186 |
included_files = {filename.as_posix() for filename, p, s in sources_reported} |
| 187 |
# sources don't emit 'include-read' |
| 188 |
assert 'index.rst' not in included_files |
| 189 |
|
| 190 |
assert 'baz/baz.rst' in included_files |
| 191 |
|
| 192 |
# text was included as literal, no rst parsing |
| 193 |
assert 'text.txt' not in included_files |
| 194 |
|
| 195 |
# suffix not in source-suffixes |
| 196 |
assert 'bar.txt' in included_files |
| 197 |
|
| 198 |
assert (Path('baz/baz.rst'), 'index', '\nBaz was here.') in sources_reported |
| 199 |
|
| 200 |
|
| 201 |
@pytest.mark.sphinx('html', testroot='directive-include') |
| 202 |
def test_include_include_read_event_nested_includes(app): |
| 203 |
def source_read_handler(_app, _relative_path, _parent_docname, source): |
| 204 |
text = source[0].replace('#magical', 'amazing') |
| 205 |
source[0] = text |
| 206 |
|
| 207 |
app.connect('include-read', source_read_handler) |
| 208 |
text = '.. include:: baz/baz.rst\n' |
| 209 |
app.env.find_files(app.config, app.builder) |
| 210 |
doctree = restructuredtext.parse(app, text, 'index') |
| 211 |
assert_node(doctree, addnodes.document) |
| 212 |
assert len(doctree.children) == 3 |
| 213 |
assert isinstance(doctree.children[1], nodes.paragraph) |
| 214 |
assert doctree.children[1].rawsource == 'The amazing foo.' |