Skip to content

Commit

Permalink
linkcheck: Emit a warning for disallowed redirects
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
tk0miya committed May 31, 2021
1 parent 2887dd0 commit 988a79d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 44 deletions.
2 changes: 0 additions & 2 deletions CHANGES
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions doc/usage/configuration.rst
Expand Up @@ -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

Expand Down Expand Up @@ -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
---------------------------
Expand Down
3 changes: 1 addition & 2 deletions sphinx/builders/linkcheck.py
Expand Up @@ -272,7 +272,7 @@ def process_result(self, result: CheckResult) -> None:
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:
Expand Down Expand Up @@ -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)

Expand Down
30 changes: 0 additions & 30 deletions tests/test_build_linkcheck.py
Expand Up @@ -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': '.*'}
Expand All @@ -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
Expand Down

0 comments on commit 988a79d

Please sign in to comment.