Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression fixup: respect anchor-check configuration setting #11544

Merged
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,10 @@ Features added
Bugs fixed
----------

* #11542: linkcheck: Properly respect :confval:`linkcheck_anchors`
and do not spuriously report failures to validate anchors.
Patch by James Addison.

Testing
-------

Expand Down
3 changes: 2 additions & 1 deletion sphinx/builders/linkcheck.py
Expand Up @@ -406,7 +406,8 @@ def _check_uri(self, uri: str, hyperlink: Hyperlink) -> tuple[str, str, int]:
_user_agent=self.user_agent,
_tls_info=(self.tls_verify, self.tls_cacerts),
) as response:
if response.ok and anchor and not contains_anchor(response, anchor):
if (self.check_anchors and response.ok and anchor
and not contains_anchor(response, anchor)):
raise Exception(__(f'Anchor {anchor!r} not found'))

# Copy data we need from the (closed) response
Expand Down
17 changes: 17 additions & 0 deletions tests/test_build_linkcheck.py
Expand Up @@ -152,6 +152,7 @@ def test_defaults(app):
}
# looking for '#top' and '#does-not-exist' not found should fail
assert rowsby["http://localhost:7777/#top"]["info"] == "Anchor 'top' not found"
assert rowsby["http://localhost:7777/#top"]["status"] == "broken"
assert rowsby["http://localhost:7777#does-not-exist"]["info"] == "Anchor 'does-not-exist' not found"
# images should fail
assert "Not Found for url: http://localhost:7777/image.png" in rowsby["http://localhost:7777/image.png"]["info"]
Expand All @@ -166,6 +167,22 @@ def test_defaults(app):
}


@pytest.mark.sphinx(
'linkcheck', testroot='linkcheck', freshenv=True,
confoverrides={'linkcheck_anchors': False})
def test_check_link_response_only(app):
with http_server(DefaultsHandler):
app.build()

# JSON output
assert (app.outdir / 'output.json').exists()
content = (app.outdir / 'output.json').read_text(encoding='utf8')

rows = [json.loads(x) for x in content.splitlines()]
rowsby = {row["uri"]: row for row in rows}
assert rowsby["http://localhost:7777/#top"]["status"] == "working"


@pytest.mark.sphinx('linkcheck', testroot='linkcheck-too-many-retries', freshenv=True)
def test_too_many_retries(app):
with http_server(DefaultsHandler):
Expand Down