| 1 |
"""Test the build process with Texinfo builder with the test root.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
import subprocess |
| 7 |
from pathlib import Path |
| 8 |
from subprocess import CalledProcessError |
| 9 |
from typing import TYPE_CHECKING |
| 10 |
from unittest.mock import Mock |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from sphinx.builders.texinfo import default_texinfo_documents |
| 15 |
from sphinx.config import Config |
| 16 |
from sphinx.util.docutils import new_document |
| 17 |
from sphinx.writers.texinfo import TexinfoTranslator |
| 18 |
|
| 19 |
if TYPE_CHECKING: |
| 20 |
from sphinx.testing.util import SphinxTestApp |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.sphinx('texinfo', testroot='root') |
| 24 |
def test_texinfo(app: SphinxTestApp) -> None: |
| 25 |
TexinfoTranslator.ignore_missing_images = True |
| 26 |
app.build(force_all=True) |
| 27 |
result = (app.outdir / 'sphinxtests.texi').read_text(encoding='utf8') |
| 28 |
assert ( |
| 29 |
'@anchor{markup doc}@anchor{11}' |
| 30 |
'@anchor{markup id1}@anchor{12}' |
| 31 |
'@anchor{markup testing-various-markup}@anchor{13}' |
| 32 |
) in result |
| 33 |
assert 'Footnotes' not in result |
| 34 |
# now, try to run makeinfo over it |
| 35 |
try: |
| 36 |
args = ['makeinfo', '--no-split', 'sphinxtests.texi'] |
| 37 |
subprocess.run(args, capture_output=True, cwd=app.outdir, check=True) |
| 38 |
except OSError as exc: |
| 39 |
raise pytest.skip.Exception from exc # most likely makeinfo was not found |
| 40 |
except CalledProcessError as exc: |
| 41 |
print(exc.stdout) |
| 42 |
print(exc.stderr) |
| 43 |
msg = f'makeinfo exited with return code {exc.returncode}' |
| 44 |
raise AssertionError(msg) from exc |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.sphinx('texinfo', testroot='markup-rubric') |
| 48 |
def test_texinfo_rubric(app: SphinxTestApp) -> None: |
| 49 |
app.build() |
| 50 |
|
| 51 |
output = (app.outdir / 'projectnamenotset.texi').read_text(encoding='utf8') |
| 52 |
assert '@heading This is a rubric' in output |
| 53 |
assert '@heading This is a multiline rubric' in output |
| 54 |
assert '@heading A rubric with a heading level' in output |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.sphinx('texinfo', testroot='markup-citation') |
| 58 |
def test_texinfo_citation(app: SphinxTestApp) -> None: |
| 59 |
app.build(force_all=True) |
| 60 |
|
| 61 |
output = (app.outdir / 'projectnamenotset.texi').read_text(encoding='utf8') |
| 62 |
assert 'This is a citation ref; @ref{1,,[CITE1]} and @ref{2,,[CITE2]}.' in output |
| 63 |
assert ( |
| 64 |
'@anchor{index cite1}@anchor{1}@w{(CITE1)} \nThis is a citation\n' |
| 65 |
) in output |
| 66 |
assert ( |
| 67 |
'@anchor{index cite2}@anchor{2}@w{(CITE2)} \nThis is a multiline citation\n' |
| 68 |
) in output |
| 69 |
|
| 70 |
|
| 71 |
def test_default_texinfo_documents() -> None: |
| 72 |
config = Config({ |
| 73 |
'project': 'STASI™ Documentation', |
| 74 |
'author': "Wolfgang Schäuble & G'Beckstein", |
| 75 |
}) |
| 76 |
expected = [ |
| 77 |
( |
| 78 |
'index', |
| 79 |
'stasi', |
| 80 |
'STASI™ Documentation', |
| 81 |
"Wolfgang Schäuble & G'Beckstein", |
| 82 |
'stasi', |
| 83 |
'One line description of project', |
| 84 |
'Miscellaneous', |
| 85 |
) |
| 86 |
] |
| 87 |
assert default_texinfo_documents(config) == expected |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.sphinx('texinfo', testroot='root') |
| 91 |
def test_texinfo_escape_id(app: SphinxTestApp) -> None: |
| 92 |
settings = Mock(title='', texinfo_dir_entry='', texinfo_elements={}) |
| 93 |
document = new_document('', settings) |
| 94 |
translator = app.builder.create_translator(document, app.builder) |
| 95 |
assert isinstance(translator, TexinfoTranslator) # type-checking |
| 96 |
|
| 97 |
assert translator.escape_id('Hello world') == 'Hello world' |
| 98 |
assert translator.escape_id('Hello world') == 'Hello world' |
| 99 |
assert translator.escape_id('Hello Sphinx world') == 'Hello Sphinx world' |
| 100 |
assert translator.escape_id('Hello:world') == 'Hello world' |
| 101 |
assert translator.escape_id('Hello(world)') == 'Hello world' |
| 102 |
assert translator.escape_id('Hello world.') == 'Hello world' |
| 103 |
assert translator.escape_id('.') == '.' |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.sphinx('texinfo', testroot='footnotes') |
| 107 |
def test_texinfo_footnote(app: SphinxTestApp) -> None: |
| 108 |
app.build(force_all=True) |
| 109 |
|
| 110 |
output = (app.outdir / 'projectnamenotset.texi').read_text(encoding='utf8') |
| 111 |
assert 'First footnote: @footnote{\nFirst\n}' in output |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.sphinx('texinfo', testroot='root') |
| 115 |
def test_texinfo_xrefs(app: SphinxTestApp) -> None: |
| 116 |
app.build(force_all=True) |
| 117 |
output = (app.outdir / 'sphinxtests.texi').read_text(encoding='utf8') |
| 118 |
assert re.search(r'@ref{\w+,,--plugin\.option}', output) |
| 119 |
|
| 120 |
# Now rebuild it without xrefs |
| 121 |
app.config.texinfo_cross_references = False |
| 122 |
app.build(force_all=True) |
| 123 |
output = (app.outdir / 'sphinxtests.texi').read_text(encoding='utf8') |
| 124 |
assert not re.search(r'@ref{\w+,,--plugin\.option}', output) |
| 125 |
assert ( |
| 126 |
'Link to perl +p, --ObjC++, --plugin.option, create-auth-token, arg and -j' |
| 127 |
) in output |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.sphinx('texinfo', testroot='root') |
| 131 |
def test_texinfo_samp_with_variable(app: SphinxTestApp) -> None: |
| 132 |
app.build() |
| 133 |
|
| 134 |
output = (app.outdir / 'sphinxtests.texi').read_text(encoding='utf8') |
| 135 |
|
| 136 |
assert '@code{@var{variable_only}}' in output |
| 137 |
assert '@code{@var{variable} and text}' in output |
| 138 |
assert '@code{Show @var{variable} in the middle}' in output |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.usefixtures('_http_teapot') |
| 142 |
@pytest.mark.sphinx('texinfo', testroot='images') |
| 143 |
def test_copy_images(app: SphinxTestApp) -> None: |
| 144 |
app.build() |
| 145 |
|
| 146 |
images_dir = Path(app.outdir) / 'projectnamenotset-figures' |
| 147 |
images = {image.name for image in images_dir.rglob('*')} |
| 148 |
images.discard('python-logo.png') |
| 149 |
assert images == { |
| 150 |
'ba30773957c3fe046897111afd65a80b81cad089.png', # texinfo: image from data:image/png URI in source |
| 151 |
'img.png', |
| 152 |
'rimg.png', |
| 153 |
'testimäge.png', |
| 154 |
} # fmt: skip |