linkcheck: Emit a warning for disallowed redirects

Now linkcheck builder integrates `linkcheck_warn_redirects` into
`linkcheck_allowed_redirects`.  As a result, linkcheck builder will
emit a warning when "disallowed" redirection detected via
`linkcheck_allowed_redirects`.
This commit is contained in:
Takeshi KOMIYA 2021-05-31 21:48:58 +09:00
parent 2887dd07df
commit 988a79de65
4 changed files with 4 additions and 44 deletions

View File

@ -43,8 +43,6 @@ Features added
text
* #9176: i18n: Emit a debug message if message catalog file not found under
:confval:`locale_dirs`
* #6525: linkcheck: Add :confval:`linkcheck_warn_redirects` to emit a warning
when the hyperlink is redirected
* #6525: linkcheck: Add :confval:`linkcheck_allowed_redirects` to mark
hyperlinks that are redirected to expected URLs as "working"
* #1874: py domain: Support union types using ``|`` in info-field-list

View File

@ -2544,8 +2544,9 @@ Options for the linkcheck builder
r'https://sphinx-doc\.org/.*': r'https://sphinx-doc\.org/en/master/.*'
}
It's helpful to enable :confval:`linkcheck_warn_redirects` to warn for URIs
causing unexpected HTTP redirection.
If set, linkcheck builder will emit a warning when disallowed redirection
found. It's useful to detect unexpected redirects under :option:`the
warn-is-error mode <sphinx-build -W>`.
.. versionadded:: 4.1
@ -2669,14 +2670,6 @@ Options for the linkcheck builder
.. versionadded:: 3.4
.. confval:: linkcheck_warn_redirects
If true, emits a warning when the response for a hyperlink is a redirect.
It's useful to detect unexpected redirects under :option:`the warn-is-error
mode <sphinx-build -W>`. Default is ``False``.
.. versionadded:: 4.1
Options for the XML builder
---------------------------

View File

@ -272,7 +272,7 @@ class CheckExternalLinksBuilder(DummyBuilder):
except KeyError:
text, color = ('with unknown code', purple)
linkstat['text'] = text
if self.config.linkcheck_warn_redirects:
if self.config.linkcheck_allowed_redirects:
logger.warning('redirect ' + result.uri + ' - ' + text + ' to ' +
result.message, location=(filename, result.lineno))
else:
@ -685,7 +685,6 @@ def setup(app: Sphinx) -> Dict[str, Any]:
# commonly used for dynamic pages
app.add_config_value('linkcheck_anchors_ignore', ["^!"], None)
app.add_config_value('linkcheck_rate_limit_timeout', 300.0, None)
app.add_config_value('linkcheck_warn_redirects', False, None)
app.connect('config-inited', compile_linkcheck_allowed_redirects, priority=800)

View File

@ -289,18 +289,6 @@ def test_follows_redirects_on_GET(app, capsys, warning):
assert warning.getvalue() == ''
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-warn-redirects',
freshenv=True, confoverrides={'linkcheck_warn_redirects': True})
def test_linkcheck_warn_redirects(app, warning):
with http_server(make_redirect_handler(support_head=False)):
app.build()
assert ("index.rst.rst:1: WARNING: redirect http://localhost:7777/path1 - with Found to "
"http://localhost:7777/?redirected=1\n" in strip_escseq(warning.getvalue()))
assert ("index.rst.rst:1: WARNING: redirect http://localhost:7777/path2 - with Found to "
"http://localhost:7777/?redirected=1\n" in strip_escseq(warning.getvalue()))
assert len(warning.getvalue().splitlines()) == 2
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-warn-redirects',
freshenv=True, confoverrides={
'linkcheck_allowed_redirects': {'http://localhost:7777/.*1': '.*'}
@ -316,25 +304,7 @@ def test_linkcheck_allowed_redirects(app, warning):
result = {r["uri"]: r["status"] for r in records}
assert result["http://localhost:7777/path1"] == "working"
assert result["http://localhost:7777/path2"] == "redirected"
assert warning.getvalue() == ''
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-warn-redirects',
freshenv=True, confoverrides={
'linkcheck_allowed_redirects': {'http://localhost:7777/.*1': '.*'},
'linkcheck_warn_redirects': True,
})
def test_linkcheck_allowed_redirects_and_linkcheck_warn_redirects(app, warning):
with http_server(make_redirect_handler(support_head=False)):
app.build()
with open(app.outdir / 'output.json') as fp:
records = [json.loads(l) for l in fp.readlines()]
assert len(records) == 2
result = {r["uri"]: r["status"] for r in records}
assert result["http://localhost:7777/path1"] == "working"
assert result["http://localhost:7777/path2"] == "redirected"
assert ("index.rst.rst:1: WARNING: redirect http://localhost:7777/path2 - with Found to "
"http://localhost:7777/?redirected=1\n" in strip_escseq(warning.getvalue()))
assert len(warning.getvalue().splitlines()) == 1