Skip to content

Commit

Permalink
Ensure that linkcheck_anchors is properly respected (#11544)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
  • Loading branch information
jayaddison and AA-Turner authored Aug 2, 2023
1 parent 89808c6 commit 06c1339
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 06c1339

Please sign in to comment.