Skip to content

Commit 06c1339

Browse files
Ensure that linkcheck_anchors is properly respected (#11544)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 89808c6 commit 06c1339

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGES

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Features added
1616
Bugs fixed
1717
----------
1818

19+
* #11542: linkcheck: Properly respect :confval:`linkcheck_anchors`
20+
and do not spuriously report failures to validate anchors.
21+
Patch by James Addison.
22+
1923
Testing
2024
-------
2125

sphinx/builders/linkcheck.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ def _check_uri(self, uri: str, hyperlink: Hyperlink) -> tuple[str, str, int]:
406406
_user_agent=self.user_agent,
407407
_tls_info=(self.tls_verify, self.tls_cacerts),
408408
) as response:
409-
if response.ok and anchor and not contains_anchor(response, anchor):
409+
if (self.check_anchors and response.ok and anchor
410+
and not contains_anchor(response, anchor)):
410411
raise Exception(__(f'Anchor {anchor!r} not found'))
411412

412413
# Copy data we need from the (closed) response

tests/test_build_linkcheck.py

+17
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def test_defaults(app):
152152
}
153153
# looking for '#top' and '#does-not-exist' not found should fail
154154
assert rowsby["http://localhost:7777/#top"]["info"] == "Anchor 'top' not found"
155+
assert rowsby["http://localhost:7777/#top"]["status"] == "broken"
155156
assert rowsby["http://localhost:7777#does-not-exist"]["info"] == "Anchor 'does-not-exist' not found"
156157
# images should fail
157158
assert "Not Found for url: http://localhost:7777/image.png" in rowsby["http://localhost:7777/image.png"]["info"]
@@ -166,6 +167,22 @@ def test_defaults(app):
166167
}
167168

168169

170+
@pytest.mark.sphinx(
171+
'linkcheck', testroot='linkcheck', freshenv=True,
172+
confoverrides={'linkcheck_anchors': False})
173+
def test_check_link_response_only(app):
174+
with http_server(DefaultsHandler):
175+
app.build()
176+
177+
# JSON output
178+
assert (app.outdir / 'output.json').exists()
179+
content = (app.outdir / 'output.json').read_text(encoding='utf8')
180+
181+
rows = [json.loads(x) for x in content.splitlines()]
182+
rowsby = {row["uri"]: row for row in rows}
183+
assert rowsby["http://localhost:7777/#top"]["status"] == "working"
184+
185+
169186
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-too-many-retries', freshenv=True)
170187
def test_too_many_retries(app):
171188
with http_server(DefaultsHandler):

0 commit comments

Comments
 (0)