| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
if TYPE_CHECKING: |
| 8 |
from sphinx.testing.util import SphinxTestApp |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.sphinx( |
| 12 |
'html', |
| 13 |
testroot='ext-extlinks-hardcoded-urls', |
| 14 |
confoverrides={'extlinks_detect_hardcoded_links': False}, |
| 15 |
) |
| 16 |
def test_extlinks_detect_candidates(app: SphinxTestApp) -> None: |
| 17 |
app.build() |
| 18 |
assert app.warning.getvalue() == '' |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.sphinx('html', testroot='ext-extlinks-hardcoded-urls') |
| 22 |
def test_replaceable_uris_emit_extlinks_warnings(app: SphinxTestApp) -> None: |
| 23 |
app.build() |
| 24 |
warning_output = app.warning.getvalue() |
| 25 |
|
| 26 |
# there should be exactly three warnings for replaceable URLs |
| 27 |
message = ( |
| 28 |
"index.rst:%d: WARNING: hardcoded link 'https://github.com/sphinx-doc/sphinx/issues/1' " |
| 29 |
"could be replaced by an extlink (try using '%s' instead)" |
| 30 |
) |
| 31 |
assert message % (11, ':issue:`1`') in warning_output |
| 32 |
assert message % (13, ':issue:`inline replaceable link <1>`') in warning_output |
| 33 |
assert message % (15, ':issue:`replaceable link <1>`') in warning_output |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.sphinx( |
| 37 |
'html', |
| 38 |
testroot='ext-extlinks-hardcoded-urls-multiple-replacements', |
| 39 |
) |
| 40 |
def test_all_replacements_suggested_if_multiple_replacements_possible( |
| 41 |
app: SphinxTestApp, |
| 42 |
) -> None: |
| 43 |
app.build() |
| 44 |
warning_output = app.warning.getvalue() |
| 45 |
# there should be six warnings for replaceable URLs, three pairs per link |
| 46 |
assert warning_output.count('WARNING: hardcoded link') == 6 |
| 47 |
message = ( |
| 48 |
"index.rst:%d: WARNING: hardcoded link 'https://github.com/octocat' " |
| 49 |
"could be replaced by an extlink (try using '%s' instead)" |
| 50 |
) |
| 51 |
assert message % (14, ':user:`octocat`') in warning_output |
| 52 |
assert message % (16, ':user:`inline replaceable link <octocat>`') in warning_output |
| 53 |
assert message % (18, ':user:`replaceable link <octocat>`') in warning_output |
| 54 |
message = ( |
| 55 |
"index.rst:%d: WARNING: hardcoded link 'https://github.com/octocat' " |
| 56 |
"could be replaced by an extlink (try using '%s' instead)" |
| 57 |
) |
| 58 |
assert message % (14, ':repo:`octocat`') in warning_output |
| 59 |
assert message % (16, ':repo:`inline replaceable link <octocat>`') in warning_output |
| 60 |
assert message % (18, ':repo:`replaceable link <octocat>`') in warning_output |