| 1 |
"""Test all builders.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
from docutils import nodes |
| 9 |
|
| 10 |
from sphinx.errors import SphinxError |
| 11 |
|
| 12 |
from tests.utils import extract_element, extract_node |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from collections.abc import Callable |
| 16 |
from pathlib import Path |
| 17 |
|
| 18 |
from sphinx.testing.util import SphinxTestApp |
| 19 |
|
| 20 |
|
| 21 |
def test_root_doc_not_found( |
| 22 |
tmp_path: Path, make_app: Callable[..., SphinxTestApp] |
| 23 |
) -> None: |
| 24 |
(tmp_path / 'conf.py').touch() |
| 25 |
assert [p.name for p in tmp_path.iterdir()] == ['conf.py'] |
| 26 |
|
| 27 |
app = make_app('dummy', srcdir=tmp_path) |
| 28 |
with pytest.raises(SphinxError): |
| 29 |
app.build(force_all=True) # no index.rst |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.sphinx('text', testroot='circular') |
| 33 |
def test_circular_toctree(app: SphinxTestApp) -> None: |
| 34 |
app.build(force_all=True) |
| 35 |
warnings = app.warning.getvalue() |
| 36 |
assert ( |
| 37 |
'circular toctree references detected, ignoring: sub <- index <- sub' |
| 38 |
) in warnings |
| 39 |
assert ( |
| 40 |
'circular toctree references detected, ignoring: index <- sub <- index' |
| 41 |
) in warnings |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.sphinx('text', testroot='numbered-circular') |
| 45 |
def test_numbered_circular_toctree(app: SphinxTestApp) -> None: |
| 46 |
app.build(force_all=True) |
| 47 |
warnings = app.warning.getvalue() |
| 48 |
assert ( |
| 49 |
'circular toctree references detected, ignoring: sub <- index <- sub' |
| 50 |
) in warnings |
| 51 |
assert ( |
| 52 |
'circular toctree references detected, ignoring: index <- sub <- index' |
| 53 |
) in warnings |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.sphinx('text', testroot='toctree-multiple-parents') |
| 57 |
def test_multiple_parents_toctree(app: SphinxTestApp) -> None: |
| 58 |
app.build(force_all=True) |
| 59 |
assert ( |
| 60 |
"document is referenced in multiple toctrees: ['bravo', 'delta'], selecting: delta <- charlie" |
| 61 |
) in app.status.getvalue() |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.usefixtures('_http_teapot') |
| 65 |
@pytest.mark.sphinx('dummy', testroot='images') |
| 66 |
def test_image_glob(app: SphinxTestApp) -> None: |
| 67 |
app.build(force_all=True) |
| 68 |
|
| 69 |
# index.rst |
| 70 |
doctree = app.env.get_doctree('index') |
| 71 |
assert isinstance(doctree[0], nodes.Element) |
| 72 |
|
| 73 |
assert isinstance(extract_node(doctree, 0, 1), nodes.image) |
| 74 |
assert extract_element(doctree, 0, 1)['candidates'] == {'*': 'rimg.png'} |
| 75 |
assert extract_element(doctree, 0, 1)['uri'] == 'rimg.png' |
| 76 |
|
| 77 |
assert isinstance(extract_node(doctree, 0, 2), nodes.figure) |
| 78 |
assert isinstance(extract_node(doctree, 0, 2, 0), nodes.image) |
| 79 |
assert extract_element(doctree, 0, 2, 0)['candidates'] == {'*': 'rimg.png'} |
| 80 |
assert extract_element(doctree, 0, 2, 0)['uri'] == 'rimg.png' |
| 81 |
|
| 82 |
assert isinstance(extract_node(doctree, 0, 3), nodes.image) |
| 83 |
assert extract_element(doctree, 0, 3)['candidates'] == { |
| 84 |
'application/pdf': 'img.pdf', |
| 85 |
'image/gif': 'img.gif', |
| 86 |
'image/png': 'img.png', |
| 87 |
} |
| 88 |
assert extract_element(doctree, 0, 3)['uri'] == 'img.*' |
| 89 |
|
| 90 |
assert isinstance(extract_node(doctree, 0, 4), nodes.figure) |
| 91 |
assert isinstance(extract_node(doctree, 0, 4, 0), nodes.image) |
| 92 |
assert extract_element(doctree, 0, 4, 0)['candidates'] == { |
| 93 |
'application/pdf': 'img.pdf', |
| 94 |
'image/gif': 'img.gif', |
| 95 |
'image/png': 'img.png', |
| 96 |
} |
| 97 |
assert extract_element(doctree, 0, 4, 0)['uri'] == 'img.*' |
| 98 |
|
| 99 |
# subdir/index.rst |
| 100 |
doctree = app.env.get_doctree('subdir/index') |
| 101 |
assert isinstance(doctree[0], nodes.Element) |
| 102 |
|
| 103 |
assert isinstance(extract_node(doctree, 0, 1), nodes.image) |
| 104 |
assert extract_element(doctree, 0, 1)['candidates'] == {'*': 'subdir/rimg.png'} |
| 105 |
assert extract_element(doctree, 0, 1)['uri'] == 'subdir/rimg.png' |
| 106 |
|
| 107 |
assert isinstance(extract_node(doctree, 0, 2), nodes.image) |
| 108 |
assert extract_element(doctree, 0, 2)['candidates'] == { |
| 109 |
'application/pdf': 'subdir/svgimg.pdf', |
| 110 |
'image/svg+xml': 'subdir/svgimg.svg', |
| 111 |
} |
| 112 |
assert extract_element(doctree, 0, 2)['uri'] == 'subdir/svgimg.*' |
| 113 |
|
| 114 |
assert isinstance(extract_node(doctree, 0, 3), nodes.figure) |
| 115 |
assert isinstance(extract_node(doctree, 0, 3, 0), nodes.image) |
| 116 |
assert extract_element(doctree, 0, 3, 0)['candidates'] == { |
| 117 |
'application/pdf': 'subdir/svgimg.pdf', |
| 118 |
'image/svg+xml': 'subdir/svgimg.svg', |
| 119 |
} |
| 120 |
assert extract_element(doctree, 0, 3, 0)['uri'] == 'subdir/svgimg.*' |