tests.sphinx.test_sphinx_click module

Tests for Sphinx directives click:source and click:run in rST and MyST formats.

tests.sphinx.test_sphinx_click.test_directive_functionality(sphinx_app_for_format, test_case)[source]

Test standard directive functionalities in each format a case targets.

tests.sphinx.test_sphinx_click.test_directive_option_format(sphinx_app_rst)[source]

rST will fail to render if an :option: is not followed by an empty line.

tests.sphinx.test_sphinx_click.test_directive_option_language_override(sphinx_app)[source]

Test that language override works for click:run directive.

tests.sphinx.test_sphinx_click.test_sphinx_directive_state_persistence(sphinx_app)[source]

Test that state persists between declare and run directives in real Sphinx.

tests.sphinx.test_sphinx_click.test_directive_variable_conflict(var_name, sphinx_app_for_format, content, directive_lineno, error_lineno)[source]

Test that variable conflicts are properly detected in real Sphinx environment.

tests.sphinx.test_sphinx_click.GENERATED_BLOCK_ANCHOR = 'from click_extra import command, echo'

First body line of the GENERATED_BLOCK_LINE_CASES documents below.

The line a generated block is expected to be attributed to, located in the document itself so editing a case moves the expectation with it.

tests.sphinx.test_sphinx_click.test_generated_block_is_attributed_to_the_directive_body(sphinx_app_for_format, test_case)[source]

A block a directive generates is attributed to its first body line.

The generated lines exist nowhere in the document, so the parser has to be told which document line to hang a diagnostic raised inside them on. Both formats must answer the same way, and neither is free to fall back on its own default: docutils numbers an unlabelled block from the top of the file, and myst-parser measures its offsets from the directive rather than the document.

The last two cases carry both an option block and a body ending in blank lines, whose content_offset myst-parser 5.1.0 and older over-counted by one.

tests.sphinx.test_sphinx_click.test_exit_exception_percolate(sphinx_app)[source]

Test directives that handle command errors and exit codes.

tests.sphinx.test_sphinx_click.test_clickrunner_forces_color(monkeypatch)[source]

ClickRunner forces FORCE_COLOR so Rich-based CLIs colorize under NO_COLOR.

The runner already passes color=True (Click’s color system). But rich-click renders help through Rich’s Console, gated on FORCE_COLOR, which color=True never reaches. The runner therefore also forces FORCE_COLOR (clearing the disabling vars) around the executed command, then restores the environment.

tests.sphinx.test_sphinx_click.test_clickrunner_sets_the_handlers_of_the_build_aside()[source]

ClickRunner takes the handlers it found on the root logger off it while a documented command runs, then puts them back.

A build shares its process with Sphinx, its extensions and its theme. A handler one of them gave the root logger would print each record of the command a second time, which the shell of a reader never does.

tests.sphinx.test_sphinx_click.test_clickrunner_sets_the_level_of_the_build_aside(document_level, expected)[source]

ClickRunner gives the root logger the default level while a documented command runs, where the build left another one, then gives that one back.

A command leaves the root logger at the level it found. The level of the build would so let through what a command logs before its verbosity options apply, which the shell of a reader never prints.

tests.sphinx.test_sphinx_click.test_clickrunner_capture_mode_controls_fileno(capture, renders)[source]

ClickRunner(capture=...) decides whether a CLI calling fileno() renders.

Click’s "sys" mode backs the captured stream with an in-memory buffer whose fileno() raises io.UnsupportedOperation, so a documented command that re-opens its descriptor (a common UTF-8-on-Windows guard) aborts. "fd" (the default on Unix, also exposed as the click_extra_run_capture conf.py value) returns a copy of the original stdout descriptor, so the command runs and its click.echo output renders. What it writes through that copy does not, see test_clickrunner_captures_prep_path_stdout. On Windows, where fd-backed streams are not supported, the default falls back to "sys".

tests.sphinx.test_sphinx_click.test_clickrunner_captures_prep_path_stdout(capture)[source]

A command writing to - through prep_path renders in every capture mode.

Under "fd", sys.stdout.fileno() returns a copy of the original stdout descriptor, so a stream opened on it writes past the capture.

tests.sphinx.test_sphinx_click.test_program_from_command_line(command_line, expected)[source]

Only an interpreter prefix is dropped from a displayed command line.

tests.sphinx.test_sphinx_click.test_clickrunner_keeps_a_multi_word_prog_name(monkeypatch)[source]

A subcommand-shaped program name reaches the command it runs, whole.

tests.sphinx.test_sphinx_click.test_clickrunner_prompt_inlines_environment_assignments(monkeypatch)[source]

Variables are set on the invocation, the way the runner applies them.

CliRunner scopes env to the one call, so an export line would advertise a persistence the next block does not inherit.

tests.sphinx.test_sphinx_click.test_clickrunner_prompt_quotes_a_spaced_argument(monkeypatch)[source]

An argument holding spaces stays the single token a reader must type.

tests.sphinx.test_sphinx_click.test_clickrunner_hide_prompt_drops_the_invocation(monkeypatch)[source]

_show_prompt=False leaves the output alone and drops the line above it.

tests.sphinx.test_sphinx_click.test_clickrunner_prompt_past_the_input_aborts(monkeypatch, terminate_input)[source]

A prompt reading past the end of the input aborts, as it does under Click.

The read past the end returns no byte at all, which the echoing stream has to pass through rather than look for a ^D in.

tests.sphinx.test_sphinx_click.test_click_run_prompt_options(sphinx_app_myst, options, prompted)[source]

:show-prompt: / :hide-prompt: gate the invocation line.

tests.sphinx.test_sphinx_click.test_click_run_output_holding_a_fence_stays_in_its_block(sphinx_app_myst)[source]

Output carrying a code fence of its own renders whole in the result block.

A fixed three-backtick fence closes on the output’s own bare fence line, and MyST then parses the rest of the output as part of the page.

tests.sphinx.test_sphinx_click.test_code_fence_outgrows_the_fences_it_wraps(lines, fence)[source]

The fence is one backtick longer than any run opening a line, at least three.

tests.sphinx.test_sphinx_click.test_click_run_hide_prompt_reaches_the_screenshot(sphinx_app_myst)[source]

A capture is drawn from the same lines, so it loses the prompt too.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_writes_the_asset(sphinx_app_myst)[source]

:screenshot: writes the capture beside the documentation.

The image is a side effect: the page keeps its results code block, which inside Sphinx beats an image by staying selectable and searchable.

tests.sphinx.test_sphinx_click.ANIMATED_SOURCE = '\n    ```{click:source}\n    :hide-source:\n    from click_extra import SPINNERS, Spinner, Style, command, echo\n\n    @command\n    def greet():\n        echo("Hello, papaya!")\n\n    steeping = Spinner("Steeping", spinner=SPINNERS["moon"], style=Style(fg="green"))\n    ```\n'

A spinner and a CLI, seeded for the animated-capture blocks below.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_stacks_a_spinner(sphinx_app_myst)[source]

:screenshot-animate: draws every frame of the spinner it names.

The frames and the interval are taken off the spinner itself, so the picture and the animation cannot disagree about either.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_defines_every_class_it_uses(sphinx_app_myst)[source]

No frame names a class the animated capture leaves undefined.

A frame whose rules are missing does not vanish: it falls back to the presentation attributes and draws in the wrong face and the wrong color, which reads as the animation resetting its styling once a cycle.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_accepts_bare_frames(sphinx_app_myst)[source]

A sequence of texts animates too, timed by :screenshot-interval:.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_line_numbers_give_every_frame_one_gutter(sphinx_app_myst)[source]

A frame reaching ten rows does not push its text a cell to the right.

Each frame counts its own rows, from one. Numbered on its own, the nine-row frame aligns its numbers on one digit and the ten-row frame on two, so every row moves between the two frames.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_bare_frames_need_an_interval(sphinx_app_myst)[source]

Bare frames carry no timing of their own, so one has to be stated.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_rejects_a_foreign_subject(sphinx_app_myst)[source]

Something that is neither a spinner nor frames fails the build.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_is_deterministic(sphinx_app_myst)[source]

A declared subject composes the same lines on every build.

This is what lets an animated capture be committed at all: a recording would time its frames a little differently on every run and dirty the tree.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_rewrites_a_declared_asset(sphinx_app_myst)[source]

A declared animation is regenerated on every build, like any capture.

It composes the same lines every time, so the file only changes with the code, and regenerating it is what keeps the asset from drifting away from that code. Gating the write on what the animation is would freeze out every change to how it is drawn.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_animate_carries_a_presentation_change(sphinx_app_myst)[source]

Restating how an animation is drawn reaches the committed asset.

The frames are untouched by a margin, so nothing about what the animation is moves. The picture still has to change.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_leaves_an_unchanged_asset_alone(sphinx_app_myst, name, options)[source]

A rebuild drawing the same capture does not write the file again.

Writing the same bytes would still move the modification time of the file, which a tool watching the documentation tree reads as a change. A capture that differs is still written.

tests.sphinx.test_sphinx_click.RECORDED_SOURCE = '\n    ```{click:source}\n    :hide-source:\n    from click_extra.capture.recording import Frame\n\n    kettle = [Frame("filling", 0.2), Frame("boiled", 0.2)]\n    ```\n'

A stand-in recording: frames carrying their own durations, as one does.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_record_writes_once(sphinx_app_myst)[source]

A recorded animation is written the first time and then left alone.

Which spinner glyph pairs with which screen is settled by the scheduler, so a recording cannot be reproduced and rewriting it would dirty the working tree for nothing anyone did.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_record_holds_its_last_frame(sphinx_app_myst)[source]

A recording pauses on its final screen before starting over.

An animation that ends somewhere is worth reading, and a loop restarting the instant it arrives never lets anyone.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_hold_overrides_the_pause(sphinx_app_myst)[source]

:screenshot-hold: states the pause a page would rather have.

tests.sphinx.test_sphinx_click.STEEPING_SOURCE = '\n    ```{click:source}\n    :hide-source:\n    from click_extra.capture.recording import Frame\n\n    steeping = [Frame("filling", 0.2), Frame("heating", 0.2), Frame("boiled", 0.2)]\n    ```\n'

A stand-in recording of three screens, for a block to start into.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_keeps_a_slice_of_a_recording(sphinx_app_myst, options, kept, dropped, period)[source]

:screenshot-start: and :screenshot-end: keep the screens between them.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_start_refuses_what_it_cannot_cut(sphinx_app_myst, options, message)[source]

A start past the recording, or into frames nobody recorded, fails the build.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_start_takes_zero_or_more(argument, expected)[source]

A start time is zero or more seconds, and anything else fails the build.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_blank_closes_the_cycle(sphinx_app_myst)[source]

A recording ends on an empty beat, so the loop’s turnover is visible.

Without it, a loop jumping from its last frame back to its first reads as one long animation doing something odd rather than as a repetition.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_speed_scales_the_recorded_frames(sphinx_app_myst)[source]

:screenshot-speed: replays faster, leaving the pauses as stated.

The pauses are how long a reader is given, not part of what is replayed, so they are stated in real seconds and speed does not touch them.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_emphasize_lines_bands_a_still(sphinx_app_myst)[source]

:screenshot-emphasize-lines: draws a band behind the lines it names.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_emphasize_lines_bands_an_animation(sphinx_app_myst)[source]

A band marks a row of the screen, so an animation keeps it throughout.

Drawn once behind every frame rather than per frame: the emphasis is on the row, not on whatever a given frame happened to put there.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_emphasize_lines_rejects_a_line_that_is_not_there(sphinx_app_myst)[source]

Naming a line the capture never drew fails the build rather than passing.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_background(sphinx_app_myst)[source]

:screenshot-background: draws the capture on the chrome it names.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_columns_auto(sphinx_app_myst)[source]

:screenshot-columns: auto widens the image to what the block printed.

Click wraps a CLI’s own text at its fixed width whatever the option says. What it decides is the picture, and a line the CLI never wrapped (the prompt of a long invocation) is the one that needs the room.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_columns_rejects_an_unusable_width(value)[source]

A width narrower than the floor, or no width at all, is a build error.

The option reads through the parser the capture commands share, so the two reject the same widths with the same words.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_frame_options(sphinx_app_myst)[source]

The :screenshot-*: options restate the window, the title included.

The hex color is quoted because a directive’s options are read as YAML, where an unquoted # opens a comment and leaves the option empty.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_preset_swaps_the_prompt(sphinx_app_myst)[source]

A capture drawn as another terminal prompts the way that one does.

A block runs under a documentation build, so its own prompt is this platform’s $. The picture is of a Windows terminal, whose shell prompts with something else entirely.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_preset_defaults_to_the_project_wide_one(sphinx_app_myst)[source]

A project drawing every capture as the same terminal states it once.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_carries_no_watermark_by_default(sphinx_app_myst)[source]

A capture a click:run block writes carries no credit line by default.

The screenshot command credits click-extra on every image it writes. A block’s image sits on a page that already says what drew it.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_watermark(sphinx_app_myst)[source]

A project wanting its captures credited states the line once.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_opacity(sphinx_app_myst)[source]

A see-through window lets the page it is laid on show through its body.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_opacity_rejects_what_is_not_one(argument)[source]

Anything outside the zero-to-one range fails the build instead of clamping.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_timing_options_take_a_positive_number(option, argument, expected)[source]

A duration, a pace or a speed of zero or less fails the build.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_background_rejects_an_unknown_chrome()[source]

A typo names the chromes it could have been, instead of drawing a default.

tests.sphinx.test_sphinx_click.HARVEST_SOURCE = '\n```{click:source}\n:hide-source:\nfrom click_extra import command, echo\n\ncrates = ("apples", "pears")\n\n@command\ndef harvest():\n    echo("apples")\n```\n\n'

A page defining a CLI, for a click:run block below it to picture.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_takes_a_palette_and_a_font(sphinx_app_myst, animation)[source]

:screenshot-palette: and :screenshot-font: reach the picture.

The scheme is read relative to the page and noted as one of its dependencies, so editing the scheme rebuilds the page.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_palette_reports_a_missing_scheme(sphinx_app_myst)[source]

A scheme file that is not there fails the build, naming the option.

tests.sphinx.test_sphinx_click.test_click_source_screenshot_refuses_a_palette(sphinx_app_myst)[source]

A capture of code takes its colors from its syntax style, as snippet does.

tests.sphinx.test_sphinx_click.test_click_run_screenshot_font_reads_as_the_commands_do()[source]

:screenshot-font: shares the parser of --font, refusals included.

tests.sphinx.test_sphinx_click.test_click_run_mirror_region_round_trips()[source]

:mirror: inserts an image link below the fence, then leaves it alone.

tests.sphinx.test_sphinx_click.test_click_run_mirror_needs_both_options()[source]

A block missing either option gets no region.

:screenshot: alone maintains an asset some other surface embeds, without putting it on this page; :mirror: alone has no capture to point at.

tests.sphinx.test_sphinx_click.test_click_run_mirror_skips_a_nested_example()[source]

A click:run shown inside a longer fence is documentation, not a block.

tests.sphinx.test_sphinx_click.test_update_screenshot_blocks_writes_and_checks(tmp_path)[source]

The file-level refresher reports a stale page, then writes its region.

check leaves the page alone, a write links the capture under the given directory, and a page already current is not reported again.

tests.sphinx.test_sphinx_click.test_click_source_screenshot_pictures_its_own_code(sphinx_app_myst)[source]

A source block’s :screenshot: draws the code it declares.

A directive that runs something has output worth committing; one that only declares code has none, so its subject is the code itself.

tests.sphinx.test_sphinx_click.test_click_source_screenshot_takes_a_syntax_style(sphinx_app_myst)[source]

:screenshot-syntax-style: repaints the window along with the code.

tests.sphinx.test_sphinx_click.test_click_source_screenshot_bands_its_emphasized_lines(sphinx_app_myst)[source]

:emphasize-lines: marks the same lines on the page and in the image.

A source block has one content, so saying it twice would be the surprise.

tests.sphinx.test_sphinx_click.test_click_source_screenshot_is_deterministic(sphinx_app_myst)[source]

Two builds of one source block write the same bytes.

Nothing here runs a command or reads a clock, so a committed asset stays put and leaves the working tree clean.

tests.sphinx.test_sphinx_click.test_click_source_mirror_shows_the_snippet()[source]

A source block mirrors its capture the way a run block does.

Same marker pair, same derivation from the :screenshot: name: what changed is only which directives are allowed to ask.

tests.sphinx.test_sphinx_click.test_python_render_mirror_is_left_to_its_own_refresher()[source]

python:render keeps the one :mirror: that means something else.

It mirrors the markup the block generated, and a second refresher writing that region would undo the first on every alternate run.