1"""
2    test_build_texinfo
3    ~~~~~~~~~~~~~~~~~~
4
5    Test the build process with Texinfo builder with the test root.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10
11import os
12import re
13import subprocess
14from subprocess import PIPE, CalledProcessError
15from unittest.mock import Mock
16
17import pytest
18
19from sphinx.builders.texinfo import default_texinfo_documents
20from sphinx.config import Config
21from sphinx.testing.util import strip_escseq
22from sphinx.util.docutils import new_document
23from sphinx.writers.texinfo import TexinfoTranslator
24
25from .test_build_html import ENV_WARNINGS
26
27TEXINFO_WARNINGS = ENV_WARNINGS + """\
28%(root)s/index.rst:\\d+: WARNING: unknown option: &option
29%(root)s/index.rst:\\d+: WARNING: citation not found: missing
30%(root)s/index.rst:\\d+: WARNING: a suitable image for texinfo builder not found: foo.\\*
31%(root)s/index.rst:\\d+: WARNING: a suitable image for texinfo builder not found: \
32\\['application/pdf', 'image/svg\\+xml'\\] \\(svgimg.\\*\\)
33"""
34
35
36@pytest.mark.sphinx('texinfo', testroot='warnings', freshenv=True)
37def test_texinfo_warnings(app, status, warning):
38    app.builder.build_all()
39    warnings = strip_escseq(re.sub(re.escape(os.sep) + '{1,2}', '/', warning.getvalue()))
40    warnings_exp = TEXINFO_WARNINGS % {
41        'root': re.escape(app.srcdir.replace(os.sep, '/'))}
42    assert re.match(warnings_exp + '$', warnings), \
43        'Warnings don\'t match:\n' + \
44        '--- Expected (regex):\n' + warnings_exp + \
45        '--- Got:\n' + warnings
46
47
48@pytest.mark.sphinx('texinfo')
49def test_texinfo(app, status, warning):
50    TexinfoTranslator.ignore_missing_images = True
51    app.builder.build_all()
52    result = (app.outdir / 'sphinxtests.texi').read_text()
53    assert ('@anchor{markup doc}@anchor{11}'
54            '@anchor{markup id1}@anchor{12}'
55            '@anchor{markup testing-various-markup}@anchor{13}' in result)
56    assert 'Footnotes' not in result
57    # now, try to run makeinfo over it
58    try:
59        args = ['makeinfo', '--no-split', 'sphinxtests.texi']
60        subprocess.run(args, stdout=PIPE, stderr=PIPE, cwd=app.outdir, check=True)
61    except OSError as exc:
62        raise pytest.skip.Exception from exc  # most likely makeinfo was not found
63    except CalledProcessError as exc:
64        print(exc.stdout)
65        print(exc.stderr)
66        assert False, 'makeinfo exited with return code %s' % exc.retcode
67
68
69@pytest.mark.sphinx('texinfo', testroot='markup-rubric')
70def test_texinfo_rubric(app, status, warning):
71    app.build()
72
73    output = (app.outdir / 'python.texi').read_text()
74    assert '@heading This is a rubric' in output
75    assert '@heading This is a multiline rubric' in output
76
77
78@pytest.mark.sphinx('texinfo', testroot='markup-citation')
79def test_texinfo_citation(app, status, warning):
80    app.builder.build_all()
81
82    output = (app.outdir / 'python.texi').read_text()
83    assert 'This is a citation ref; @ref{1,,[CITE1]} and @ref{2,,[CITE2]}.' in output
84    assert ('@anchor{index cite1}@anchor{1}@w{(CITE1)} \n'
85            'This is a citation\n') in output
86    assert ('@anchor{index cite2}@anchor{2}@w{(CITE2)} \n'
87            'This is a multiline citation\n') in output
88
89
90def test_default_texinfo_documents():
91    config = Config({'project': 'STASI™ Documentation',
92                     'author': "Wolfgang Schäuble & G'Beckstein"})
93    config.init_values()
94    expected = [('index', 'stasi', 'STASI™ Documentation',
95                 "Wolfgang Schäuble & G'Beckstein", 'stasi',
96                 'One line description of project', 'Miscellaneous')]
97    assert default_texinfo_documents(config) == expected
98
99
100@pytest.mark.sphinx('texinfo')
101def test_texinfo_escape_id(app, status, warning):
102    settings = Mock(title='',
103                    texinfo_dir_entry='',
104                    texinfo_elements={})
105    document = new_document('', settings)
106    translator = app.builder.create_translator(document, app.builder)
107
108    assert translator.escape_id('Hello world') == 'Hello world'
109    assert translator.escape_id('Hello    world') == 'Hello world'
110    assert translator.escape_id('Hello   Sphinx   world') == 'Hello Sphinx world'
111    assert translator.escape_id('Hello:world') == 'Hello world'
112    assert translator.escape_id('Hello(world)') == 'Hello world'
113    assert translator.escape_id('Hello world.') == 'Hello world'
114    assert translator.escape_id('.') == '.'
115