From 988a79de65737e403cd27721ce091760cf62b97f Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 31 May 2021 21:48:58 +0900 Subject: [PATCH] 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`. --- CHANGES | 2 -- doc/usage/configuration.rst | 13 +++---------- sphinx/builders/linkcheck.py | 3 +-- tests/test_build_linkcheck.py | 30 ------------------------------ 4 files changed, 4 insertions(+), 44 deletions(-) diff --git a/CHANGES b/CHANGES index 0a82da183..074eb3cea 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index 9494bd5bd..73ca1b600 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -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 `. .. 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 `. Default is ``False``. - - .. versionadded:: 4.1 - Options for the XML builder --------------------------- diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py index 2da438cbf..ca7bd5617 100644 --- a/sphinx/builders/linkcheck.py +++ b/sphinx/builders/linkcheck.py @@ -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) diff --git a/tests/test_build_linkcheck.py b/tests/test_build_linkcheck.py index 461305ad7..bc85e402f 100644 --- a/tests/test_build_linkcheck.py +++ b/tests/test_build_linkcheck.py @@ -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