tests.sphinx.test_sphinx_python module

Tests for the python:* Sphinx directive family.

Covers python:source, python:run, and the three render variants:

  • python:render (host parser)

  • python:render-myst (forced MyST, regardless of host)

  • python:render-rst (forced reST, regardless of host)

tests.sphinx.test_sphinx_python.test_python_run_renders_stdout(sphinx_app_myst)[source]

python:run captures print output and renders it in a code block.

tests.sphinx.test_sphinx_python.test_python_source_seeds_namespace_for_python_run(sphinx_app_myst)[source]

python:source runs silently; a follow-up python:run reuses its imports.

tests.sphinx.test_sphinx_python.test_python_run_language_override(sphinx_app_myst)[source]

:language: overrides the default text lexer for the result block.

tests.sphinx.test_sphinx_python.test_python_run_screenshot_writes_the_asset(sphinx_app_myst)[source]

:screenshot: on python:run writes the capture, as on click:run.

The :mirror: refresher links the asset below whichever directive names it, so the block has to write it or the page links an image that is not there.

tests.sphinx.test_sphinx_python.test_python_run_emphasize_lines_split(sphinx_app_myst)[source]

:emphasize-lines: highlights source only; :emphasize-result-lines: highlights result only: independently, on the same block.

tests.sphinx.test_sphinx_python.test_python_render_passes_block_level_html(sphinx_app_myst)[source]

python:render passes block-level raw HTML through unchanged.

A naked print('<div>...</div>') should reach the rendered page without any {raw} html wrapping. Locks down the natural-form pattern so a future MyST upgrade or extension reordering can’t silently regress it.

tests.sphinx.test_sphinx_python.test_python_render_host_myst_injects_table(sphinx_app_myst)[source]

python:render parses captured stdout with the host (MyST) parser.

tests.sphinx.test_sphinx_python.test_python_render_host_myst_injects_heading(sphinx_app_myst)[source]

A heading printed by python:render becomes a real heading node.

tests.sphinx.test_sphinx_python.test_python_render_host_rst_injects_admonition(sphinx_app_rst)[source]

python:render in an rST host: stdout is parsed as reST.

tests.sphinx.test_sphinx_python.test_python_render_myst_in_rst_host(sphinx_app_rst)[source]

python:render-myst forces MyST parsing inside an rST host document.

This is the headline use case: an rST file embeds Python that prints MyST markup and the directive parses it as MyST regardless of host.

tests.sphinx.test_sphinx_python.test_python_render_rst_in_myst_host(sphinx_app_myst)[source]

python:render-rst forces reST parsing inside a MyST host document.

tests.sphinx.test_sphinx_python.test_python_render_myst_in_myst_host_still_works(sphinx_app_myst)[source]

python:render-myst works in MyST hosts too; it always picks MyST.

tests.sphinx.test_sphinx_python.test_python_render_rst_in_rst_host_still_works(sphinx_app_rst)[source]

python:render-rst works in rST hosts too; it always picks reST.

tests.sphinx.test_sphinx_python.test_exec_directives_disabled_by_default(tmp_path)[source]

Without the opt-in flag, click:* and python:* are not registered.

The Sphinx build still succeeds but neither family’s directive body is ever executed. This is the desired security default: a project that adds click_extra.sphinx to its extensions list does not silently gain build-time arbitrary Python execution.

The exact rendering of an unrecognized directive is parser-dependent (MyST silently swallows it; reST emits a system message), so the assertion focuses on the security-relevant invariant: the directive body’s print output never reaches the rendered HTML.

tests.sphinx.test_sphinx_python.test_exec_directives_enabled_with_opt_in(tmp_path)[source]

Setting click_extra_enable_exec_directives = True activates them.

tests.sphinx.test_sphinx_python.test_python_runner_isolated_from_click_runner(sphinx_app_myst)[source]

The Python and Click runners hold independent namespaces.

tests.sphinx.test_sphinx_python.test_mirror_src_rewrite_inserts_region()[source]

A mirror-src comment with no region yet gets one, generator kept hidden.

tests.sphinx.test_sphinx_python.test_mirror_src_rewrite_is_idempotent()[source]

Re-running over an already-filled mirror-src region is a no-op.

tests.sphinx.test_sphinx_python.test_mirror_src_first_sight_stops_at_the_next_generator()[source]

A new generator above a refreshed one gets its own region.

The terminator below the second generator closes that generator’s region: read as the end of the first one’s, it would replace the prose and the second generator between them.

tests.sphinx.test_sphinx_python.test_mirror_src_via_update_mirror_blocks(tmp_path)[source]

The offline refresher fills a mirror-src region, then round-trips clean.

tests.sphinx.test_sphinx_python.test_mirror_src_rewrite_skips_example_nested_in_code_block()[source]

A mirror-src comment shown inside a longer code-block fence is never run.

tests.sphinx.test_sphinx_python.test_rewrite_mirrors_refreshes_every_mirror_src_spelling(opening)[source]

A comment the region walk accepts is refreshed, whatever space follows <!--.

tests.sphinx.test_sphinx_python.test_mirror_rewrite_inserts_region()[source]

A mirror block with no region yet gets one inserted below the fence.

tests.sphinx.test_sphinx_python.test_mirror_rewrite_is_idempotent()[source]

Re-running over an already-mirrored document is a no-op.

tests.sphinx.test_sphinx_python.test_mirror_rewrite_replaces_stale_region()[source]

A stale region is refreshed from the block’s current output.

tests.sphinx.test_sphinx_python.test_mirror_rewrite_skips_example_nested_in_code_block()[source]

A mirror block shown inside a longer code-block fence is never executed.

tests.sphinx.test_sphinx_python.test_python_render_mirror_renders_fresh_without_touching_source(sphinx_app_myst)[source]

:mirror: renders the table once; the build never writes to the source.

tests.sphinx.test_sphinx_python.test_update_mirror_blocks_populates_and_idempotent(tmp_path)[source]

The offline refresher inserts the region, then round-trips clean.

tests.sphinx.test_sphinx_python.test_update_mirror_blocks_check_mode(tmp_path)[source]

check=True reports the stale file without writing it.

tests.sphinx.test_sphinx_python.test_update_mirror_blocks_imports_sibling_module(tmp_path)[source]

A mirror block imports a helper module living next to the page.

Matches the Sphinx build, where the conf.py directory is importable, so a docs_update.py-style generator module works offline too. The path entry is scoped to the execution and removed afterwards.

tests.sphinx.test_sphinx_python.test_refresh_directives_cli_refreshes_mirror_blocks(tmp_path)[source]

click-extra refresh-directives covers mirror regions too.

tests.sphinx.test_sphinx_python.test_refresh_directives_cli_reads_and_writes_a_page_once(tmp_path, monkeypatch)[source]

Every kind of block of a page is refreshed in one read and one write.

The matrix, mirror and screenshot refreshers share one pass over the page.

tests.sphinx.test_sphinx_python.test_refresh_directives_crashing_mirror_gets_no_bug_report(invoke, tmp_path)[source]

A crash of a mirror block is a bug of its page: Click Extra reports none.

The click-extra CLI has a report_url, so its root would otherwise end the run with a report for the issues of Click Extra. The subcommand has its own --version option, so the flag it sets reaches the root only through the ctx.meta they share.

tests.sphinx.test_sphinx_python.test_crashing_mirror_traceback_names_its_page_line(tmp_path, page)[source]

The traceback of a mirror block that raises names the line of its page.

tests.sphinx.test_sphinx_python.test_python_render_mirror_show_source_still_single_table(sphinx_app_myst)[source]

:mirror: :show-source: shows the Python but still renders one table.