| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
import traceback |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from sphinx._cli.util.errors import strip_escape_sequences |
| 12 |
from sphinx.errors import SphinxError |
| 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 |
ENV_WARNINGS = """\ |
| 21 |
{root}/autodoc_fodder.py:docstring of autodoc_fodder.MarkupError:\\d+: \ |
| 22 |
WARNING: Explicit markup ends without a blank line; unexpected unindent. \\[docutils\\] |
| 23 |
{root}/index.rst:\\d+: WARNING: Encoding 'utf-8-sig' used for reading included \ |
| 24 |
file '{root}/wrongenc.inc' seems to be wrong, try giving an :encoding: option \\[docutils\\] |
| 25 |
{root}/index.rst:\\d+: WARNING: invalid single index entry '' \\[index\\] |
| 26 |
{root}/index.rst:\\d+: WARNING: image file not readable: foo.png \\[image.not_readable\\] |
| 27 |
{root}/index.rst:\\d+: WARNING: download file not readable: {root}/nonexisting.png \\[download.not_readable\\] |
| 28 |
""" |
| 29 |
|
| 30 |
HTML_WARNINGS = ( |
| 31 |
ENV_WARNINGS |
| 32 |
+ """\ |
| 33 |
{root}/index.rst:\\d+: WARNING: unknown option: '&option' \\[ref.option\\] |
| 34 |
{root}/index.rst:\\d+: WARNING: citation not found: missing \\[ref.ref\\] |
| 35 |
{root}/index.rst:\\d+: WARNING: a suitable image for html builder not found: foo.\\* |
| 36 |
{root}/index.rst:\\d+: WARNING: Lexing literal_block ".*" as "c" resulted in an error at token: ".*". Retrying in relaxed mode. \\[misc.highlighting_failure\\] |
| 37 |
""" |
| 38 |
) |
| 39 |
|
| 40 |
LATEX_WARNINGS = ( |
| 41 |
ENV_WARNINGS |
| 42 |
+ """\ |
| 43 |
{root}/index.rst:\\d+: WARNING: unknown option: '&option' \\[ref.option\\] |
| 44 |
{root}/index.rst:\\d+: WARNING: citation not found: missing \\[ref.ref\\] |
| 45 |
{root}/index.rst:\\d+: WARNING: a suitable image for latex builder not found: foo.\\* |
| 46 |
{root}/index.rst:\\d+: WARNING: Lexing literal_block ".*" as "c" resulted in an error at token: ".*". Retrying in relaxed mode. \\[misc.highlighting_failure\\] |
| 47 |
""" |
| 48 |
) |
| 49 |
|
| 50 |
TEXINFO_WARNINGS = ( |
| 51 |
ENV_WARNINGS |
| 52 |
+ """\ |
| 53 |
{root}/index.rst:\\d+: WARNING: unknown option: '&option' \\[ref.option\\] |
| 54 |
{root}/index.rst:\\d+: WARNING: citation not found: missing \\[ref.ref\\] |
| 55 |
{root}/index.rst:\\d+: WARNING: a suitable image for texinfo builder not found: foo.\\* |
| 56 |
{root}/index.rst:\\d+: WARNING: a suitable image for texinfo builder not found: \ |
| 57 |
\\['application/pdf', 'image/svg\\+xml'\\] \\(svgimg.\\*\\) |
| 58 |
""" |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
def _check_warnings(expected_warnings: str, warning: str) -> None: |
| 63 |
warnings = strip_escape_sequences(re.sub(re.escape(os.sep) + '{1,2}', '/', warning)) |
| 64 |
assert re.match(f'{expected_warnings}$', warnings), ( |
| 65 |
"Warnings don't match:\n" |
| 66 |
f'--- Expected (regex):\n{expected_warnings}\n' |
| 67 |
f'--- Got:\n{warnings}' |
| 68 |
) |
| 69 |
sys.modules.pop('autodoc_fodder', None) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.sphinx( |
| 73 |
'html', |
| 74 |
testroot='warnings', |
| 75 |
freshenv=True, |
| 76 |
) |
| 77 |
def test_html_warnings(app: SphinxTestApp) -> None: |
| 78 |
app.build(force_all=True) |
| 79 |
warnings_exp = HTML_WARNINGS.format(root=re.escape(app.srcdir.as_posix())) |
| 80 |
_check_warnings(warnings_exp, app.warning.getvalue()) |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.sphinx( |
| 84 |
'html', |
| 85 |
testroot='warnings', |
| 86 |
freshenv=True, |
| 87 |
exception_on_warning=True, |
| 88 |
) |
| 89 |
def test_html_warnings_exception_on_warning(app: SphinxTestApp) -> None: |
| 90 |
try: |
| 91 |
app.build(force_all=True) |
| 92 |
pytest.fail('Expected an exception to be raised') |
| 93 |
except SphinxError: |
| 94 |
tb = traceback.format_exc() |
| 95 |
assert 'unindent_warning' in tb |
| 96 |
assert 'pending_warnings' not in tb |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.sphinx( |
| 100 |
'latex', |
| 101 |
testroot='warnings', |
| 102 |
freshenv=True, |
| 103 |
) |
| 104 |
def test_latex_warnings(app: SphinxTestApp) -> None: |
| 105 |
app.build(force_all=True) |
| 106 |
warnings_exp = LATEX_WARNINGS.format(root=re.escape(app.srcdir.as_posix())) |
| 107 |
_check_warnings(warnings_exp, app.warning.getvalue()) |
| 108 |
|
| 109 |
|
| 110 |
@pytest.mark.sphinx( |
| 111 |
'texinfo', |
| 112 |
testroot='warnings', |
| 113 |
freshenv=True, |
| 114 |
) |
| 115 |
def test_texinfo_warnings(app: SphinxTestApp) -> None: |
| 116 |
app.build(force_all=True) |
| 117 |
warnings_exp = TEXINFO_WARNINGS.format(root=re.escape(app.srcdir.as_posix())) |
| 118 |
_check_warnings(warnings_exp, app.warning.getvalue()) |
| 119 |
|
| 120 |
|
| 121 |
def test_uncacheable_config_warning( |
| 122 |
make_app: Callable[..., SphinxTestApp], tmp_path: Path |
| 123 |
) -> None: |
| 124 |
"""Test that an unpickleable config value raises a warning.""" |
| 125 |
tmp_path.joinpath('conf.py').write_text( |
| 126 |
"""\ |
| 127 |
my_config = lambda: None |
| 128 |
show_warning_types = True |
| 129 |
def setup(app): |
| 130 |
app.add_config_value('my_config', None, 'env') |
| 131 |
""", |
| 132 |
encoding='utf-8', |
| 133 |
) |
| 134 |
tmp_path.joinpath('index.rst').write_text('Test\n====\n', encoding='utf-8') |
| 135 |
app = make_app(srcdir=tmp_path) |
| 136 |
app.build() |
| 137 |
assert strip_escape_sequences(app.warning.getvalue()).strip() == ( |
| 138 |
"WARNING: cannot cache unpickleable configuration value: 'my_config' " |
| 139 |
'(because it contains a function, class, or module object) [config.cache]' |
| 140 |
) |