tests.test_sphinx_crossrefs module

Render tests for Sphinx cross-references in the built documentation.

Build the docs once and assert against the real HTML that in-page and cross-page anchors resolve to the sections they name, and that intersphinx references to Click resolve to the upstream site. This catches drift the moment a heading rename shifts a make_id slug, a page link goes stale, or the intersphinx_mapping URL breaks, none of which a mock-based test would notice.

The same build shows what the click_extra.sphinx extension changes in its output: each changelog release anchored on its version, no viewcode page that nothing links to, and no pickled page holding a node of another document.

The docs are built in a subprocess through uv run --group docs, and so are the pickled pages read back, so this module needs no Sphinx dependency of its own and runs in the plain test environment.

tests.test_sphinx_crossrefs.UV_RUN_DOCS = ('uv', 'run', '--group', 'docs')

Prefix of a command run in the docs environment.

tests.test_sphinx_crossrefs.CHANGELOG_RELEASE = re.compile('^## \\[`(?P<version>\\d+\\.\\d+\\.\\d+)(?:\\.\\w+)?` \\(', re.MULTILINE)

Heading of a release in changelog.md, the .devN suffix left out.

tests.test_sphinx_crossrefs.FOREIGN_NODES_SCRIPT = 'import json\nimport pickle\nimport sys\nfrom pathlib import Path\n\ndoctrees = Path(sys.argv[1])\nforeign = {}\nfor path in sorted(doctrees.rglob("*.doctree")):\n    doctree = pickle.loads(path.read_bytes())\n    count = sum(1 for node in doctree.findall() if node.document is not doctree)\n    if count:\n        foreign[path.relative_to(doctrees).as_posix()] = count\nprint(json.dumps(foreign))\n'

Count, for each pickled page, the nodes that belong to another document.

Runs in the docs environment, which holds every class the pages pickle.

tests.test_sphinx_crossrefs.built_docs(tmp_path_factory, captures_before_build)[source]

Build the HTML documentation once and return its output directory.

Builds into a throwaway directory (rather than docs/_build) so the run is hermetic and never clobbers a developer’s local build. Reads in parallel, as docs.yaml does: a serial read hides most of the viewcode pages a parallel one publishes.

Requests captures_before_build() so the snapshot is taken before the build whichever test triggers it: every test of this module shares the one build, and pytest-randomly decides which of them runs first.

Return type:

Path

tests.test_sphinx_crossrefs.captures_before_build()[source]

Snapshot every committed capture before a build gets a chance to rewrite one.

built_docs() requests it, so the bytes are read while the tree is still as committed, whichever test of the module builds first.

Return type:

dict[Path, bytes]

tests.test_sphinx_crossrefs.test_committed_captures_survive_a_build(captures_before_build, built_docs)[source]

A build leaves every committed capture byte-identical.

A click:run block carrying :screenshot: rewrites its image from the CLI’s live output on every build. So an image that no longer matches the code surfaces here, instead of sitting stale in a readme nobody re-checked.

The bytes compare raw: the @generated stamp and the default watermark name no release, so a version bump moves neither. Regenerating is still the fix for a real difference: build and commit it.

tests.test_sphinx_crossrefs.read_html(built_docs, filename)[source]

Read a built HTML page.

Return type:

str

tests.test_sphinx_crossrefs.test_install_page_internal_anchor(built_docs)[source]

The install page’s in-page binaries link resolves to its section.

tests.test_sphinx_crossrefs.test_changelog_releases_are_anchored_on_their_version(built_docs)[source]

Each release of the changelog has an anchor made from its version.

docutils reduces a release heading to nothing and numbers its section from a per-page counter, so #id40 names another release after each new one. click_extra_release_anchors in docs/conf.py gives each section an anchor like #v1-4-2 instead. See click_extra.sphinx.changelog.

Each published module page links back to an object it documents.

sphinx.ext.viewcode keeps a module it read even when no documented object links to it, and a parallel read publishes a page for each such module, reachable from the _modules index alone. See click_extra.sphinx.viewcode.

tests.test_sphinx_crossrefs.test_pickled_pages_hold_no_node_of_another_document(built_docs)[source]

Every node of each pickled page belongs to that page.

A node that keeps a throwaway document pickles that document’s settings with the page, build environment included. Counting such nodes catches a leak of any size, where a ceiling on file sizes lets through a leak that only some of the pages carry. See click_extra.sphinx.doctrees.

tests.test_sphinx_crossrefs.test_cli_reference_anchors(built_docs, anchor)[source]

The CLI page’s tree and config summary tables deep-link their sections.

One anchor per directive: click-extra-wrap comes from {click:tree}, test-suite-file from {click:config}. Their presence is also the canary for the directives themselves staying on the page.

Cross-page links point at anchors that exist on the target page.

Every case names a page, so splitting one moves the anchor and leaves the case behind. Repoint them in the same commit that splits the page: this module is skipped off Linux, so the break surfaces only in CI, and a local pytest -m once reports a clean pass while skipping all of it.

tests.test_sphinx_crossrefs.test_intersphinx_click_resolves(built_docs)[source]

Click cross-references resolve to the upstream documentation site.