| 1 |
"""Test sphinx.ext.todo extension.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from sphinx.application import Sphinx |
| 12 |
from sphinx.ext.todo import todo_node |
| 13 |
from sphinx.testing.util import SphinxTestApp |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.sphinx( |
| 17 |
'html', |
| 18 |
testroot='ext-todo', |
| 19 |
freshenv=True, |
| 20 |
confoverrides={'todo_include_todos': True, 'todo_emit_warnings': True}, |
| 21 |
) |
| 22 |
def test_todo(app: SphinxTestApp) -> None: |
| 23 |
todos = [] |
| 24 |
|
| 25 |
def on_todo_defined(app: Sphinx, node: todo_node) -> None: |
| 26 |
todos.append(node) |
| 27 |
|
| 28 |
app.connect('todo-defined', on_todo_defined) |
| 29 |
app.build(force_all=True) |
| 30 |
|
| 31 |
# check todolist |
| 32 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 33 |
assert '<p class="admonition-title">Todo</p>\n<p>todo in foo</p>' in content |
| 34 |
|
| 35 |
assert '<p class="admonition-title">Todo</p>\n<p>todo in bar</p>' in content |
| 36 |
|
| 37 |
# check todo |
| 38 |
content = (app.outdir / 'foo.html').read_text(encoding='utf8') |
| 39 |
assert '<p class="admonition-title">Todo</p>\n<p>todo in foo</p>' in content |
| 40 |
|
| 41 |
assert ( |
| 42 |
'<p class="admonition-title">Todo</p>\n<p>todo in param field</p>' |
| 43 |
) in content |
| 44 |
|
| 45 |
# check emitted warnings |
| 46 |
assert 'WARNING: TODO entry found: todo in foo' in app.warning.getvalue() |
| 47 |
assert 'WARNING: TODO entry found: todo in bar' in app.warning.getvalue() |
| 48 |
|
| 49 |
# check handled event |
| 50 |
assert len(todos) == 3 |
| 51 |
assert {todo[1].astext() for todo in todos} == { |
| 52 |
'todo in foo', |
| 53 |
'todo in bar', |
| 54 |
'todo in param field', |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.sphinx( |
| 59 |
'html', |
| 60 |
testroot='ext-todo', |
| 61 |
freshenv=True, |
| 62 |
confoverrides={'todo_include_todos': False, 'todo_emit_warnings': True}, |
| 63 |
) |
| 64 |
def test_todo_not_included(app: SphinxTestApp) -> None: |
| 65 |
todos = [] |
| 66 |
|
| 67 |
def on_todo_defined(app: Sphinx, node: todo_node) -> None: |
| 68 |
todos.append(node) |
| 69 |
|
| 70 |
app.connect('todo-defined', on_todo_defined) |
| 71 |
app.build(force_all=True) |
| 72 |
|
| 73 |
# check todolist |
| 74 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 75 |
assert '<p class="admonition-title">Todo</p>\n<p>todo in foo</p>' not in content |
| 76 |
|
| 77 |
assert '<p class="admonition-title">Todo</p>\n<p>todo in bar</p>' not in content |
| 78 |
|
| 79 |
# check todo |
| 80 |
content = (app.outdir / 'foo.html').read_text(encoding='utf8') |
| 81 |
assert '<p class="admonition-title">Todo</p>\n<p>todo in foo</p>' not in content |
| 82 |
|
| 83 |
# check emitted warnings |
| 84 |
assert 'WARNING: TODO entry found: todo in foo' in app.warning.getvalue() |
| 85 |
assert 'WARNING: TODO entry found: todo in bar' in app.warning.getvalue() |
| 86 |
|
| 87 |
# check handled event |
| 88 |
assert len(todos) == 3 |
| 89 |
assert {todo[1].astext() for todo in todos} == { |
| 90 |
'todo in foo', |
| 91 |
'todo in bar', |
| 92 |
'todo in param field', |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.sphinx( |
| 97 |
'latex', |
| 98 |
testroot='ext-todo', |
| 99 |
freshenv=True, |
| 100 |
confoverrides={'todo_include_todos': True}, |
| 101 |
) |
| 102 |
def test_todo_valid_link(app: SphinxTestApp) -> None: |
| 103 |
"""Test that the inserted "original entry" links for todo items have a target |
| 104 |
that exists in the LaTeX output. The target was previously incorrectly omitted. |
| 105 |
https://github.com/sphinx-doc/sphinx/issues/1020 |
| 106 |
""" |
| 107 |
# Ensure the LaTeX output is built. |
| 108 |
app.build(force_all=True) |
| 109 |
|
| 110 |
content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 111 |
|
| 112 |
# Look for the link to foo. Note that there are two of them because the |
| 113 |
# source document uses todolist twice. We could equally well look for links |
| 114 |
# to bar. |
| 115 |
link = ( |
| 116 |
r'{\\hyperref\[\\detokenize{(.*?foo.*?)}]{\\sphinxcrossref{' |
| 117 |
r'\\sphinxstyleemphasis{original entry}}}}' |
| 118 |
) |
| 119 |
m = re.findall(link, content) |
| 120 |
assert len(m) == 4 |
| 121 |
target = m[0] |
| 122 |
|
| 123 |
# Look for the targets of this link. |
| 124 |
labels = re.findall(r'\\label{\\detokenize{([^}]*)}}', content) |
| 125 |
matched = [l for l in labels if l == target] |
| 126 |
|
| 127 |
# If everything is correct we should have exactly one target. |
| 128 |
assert len(matched) == 1 |