File: test_ext_extlinks.py

package info (click to toggle)
sphinx 8.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 26,712 kB
  • sloc: python: 105,846; javascript: 6,474; perl: 451; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (60 lines) | stat: -rw-r--r-- 2,310 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp


@pytest.mark.sphinx(
    'html',
    testroot='ext-extlinks-hardcoded-urls',
    confoverrides={'extlinks_detect_hardcoded_links': False},
)
def test_extlinks_detect_candidates(app: SphinxTestApp) -> None:
    app.build()
    assert app.warning.getvalue() == ''


@pytest.mark.sphinx('html', testroot='ext-extlinks-hardcoded-urls')
def test_replaceable_uris_emit_extlinks_warnings(app: SphinxTestApp) -> None:
    app.build()
    warning_output = app.warning.getvalue()

    # there should be exactly three warnings for replaceable URLs
    message = (
        "index.rst:%d: WARNING: hardcoded link 'https://github.com/sphinx-doc/sphinx/issues/1' "
        "could be replaced by an extlink (try using '%s' instead)"
    )
    assert message % (11, ':issue:`1`') in warning_output
    assert message % (13, ':issue:`inline replaceable link <1>`') in warning_output
    assert message % (15, ':issue:`replaceable link <1>`') in warning_output


@pytest.mark.sphinx(
    'html',
    testroot='ext-extlinks-hardcoded-urls-multiple-replacements',
)
def test_all_replacements_suggested_if_multiple_replacements_possible(
    app: SphinxTestApp,
) -> None:
    app.build()
    warning_output = app.warning.getvalue()
    # there should be six warnings for replaceable URLs, three pairs per link
    assert warning_output.count('WARNING: hardcoded link') == 6
    message = (
        "index.rst:%d: WARNING: hardcoded link 'https://github.com/octocat' "
        "could be replaced by an extlink (try using '%s' instead)"
    )
    assert message % (14, ':user:`octocat`') in warning_output
    assert message % (16, ':user:`inline replaceable link <octocat>`') in warning_output
    assert message % (18, ':user:`replaceable link <octocat>`') in warning_output
    message = (
        "index.rst:%d: WARNING: hardcoded link 'https://github.com/octocat' "
        "could be replaced by an extlink (try using '%s' instead)"
    )
    assert message % (14, ':repo:`octocat`') in warning_output
    assert message % (16, ':repo:`inline replaceable link <octocat>`') in warning_output
    assert message % (18, ':repo:`replaceable link <octocat>`') in warning_output