tests.test_snippet module

Tests for click_extra.capture.snippet.

A snippet rides the screenshot renderer, so this module tests the step that is new: the colors a Pygments style hands over, and the shape of the text it hands them on. What the renderer then does with that text is covered by test_screenshots.py, and is deliberately not re-tested here.

tests.test_snippet.SAMPLE = 'def ripen(fruit, days=3):\n    """Wait for the mango."""\n    return fruit\n'

A few lines of Python exercising keywords, strings and punctuation.

tests.test_snippet.WINDOW_RE = re.compile('<rect fill="(?P<paint>#[0-9a-f]{6})" stroke=')

The window’s own rectangle, whose fill is the terminal’s background.

tests.test_snippet.window_paint(svg)[source]

The background color a rendered snippet’s window is filled with.

Return type:

str

tests.test_snippet.test_snippet_wears_the_style_background(background)[source]

The window is painted the background the syntax style was designed for.

A style states the surface its colors were chosen against, and drawn on any other one it is a picture of that style in a window it was never meant for.

tests.test_snippet.test_snippet_style_beats_the_preset_chrome()[source]

A preset dresses the window; the style still paints its body.

The two answer different questions, so a snippet wearing a terminal’s decorations keeps the colors its code was highlighted with.

tests.test_snippet.test_every_style_paints_a_readable_window(style)[source]

Every style Pygments ships renders, and states a background and an ink.

A conformance sweep rather than a spot check: the palette is built from two attributes a style is free to leave unset, and the fallback for each has to hold across the whole catalog.

tests.test_snippet.test_style_without_text_color_keeps_the_chrome_ink()[source]

A style naming no color for plain text falls back to the chrome’s own.

Twenty-five of the styles Pygments ships are in this case, all of them light ones whose code is meant to be black.

tests.test_snippet.test_snippet_carries_the_style_colors_into_the_picture()[source]

The colors a style names reach the rendered document.

The whole premise of the module: Pygments writes true-color ANSI, which is the interchange format the renderer already reads.

tests.test_snippet.test_highlight_expands_tabs()[source]

A tab becomes spaces before the snippet is laid out.

It has no width on a character grid, so one reaching the renderer takes the whole line’s measurement down with it.

tests.test_snippet.test_snippet_draws_the_lines_it_was_given()[source]

The code comes back out of the picture, character for character.

tests.test_snippet.test_snippet_numbers_its_lines()[source]

A numbered snippet counts from one, in a gutter of its own.

tests.test_snippet.test_snippet_leaves_room_for_line_numbers_on_the_rule()[source]

A numbered snippet cut short rules the cut inside its text column.

The gutter takes cells in front of every row, the rule’s included, so a rule as wide as columns would fold on its last dots and put them alone on a row of their own.

tests.test_snippet.test_snippet_renders_the_same_bytes_twice()[source]

Nothing in a snippet is timed, sampled or ordered by chance.

Which is what lets a documentation build rewrite a committed asset and leave the working tree clean.

tests.test_snippet.test_resolve_lexer(language, filename, expected)[source]

A stated language wins over a file name, which wins over a guess.

tests.test_snippet.test_unknown_language_is_an_error()[source]

A misspelled language fails loudly rather than drawing plain text.

A typo swallowed here would surface as an uncolored picture nobody re-reads.

tests.test_snippet.test_unknown_style_is_an_error()[source]

A misspelled style names the ones that exist.

tests.test_snippet.test_style_defaults_to_the_chrome(background)[source]

Naming no style takes the one the chrome is drawn for.

tests.test_snippet.test_default_style_sits_close_to_the_capture_chrome(background)[source]

The default style’s background is a near match for a capture’s own.

The reason these two were picked: a snippet and a terminal capture sharing a page must not show a step between their windows.

tests.test_snippet.test_snippet_renders_html()[source]

The HTML format carries the style’s colors too.

tests.test_snippet.shared_option_names()[source]

Every option name capture_options() attaches.

Read off a bare function the decorator is applied to, rather than restated here: a list written out by hand is one more place for the two commands to drift apart, which is the thing this is here to catch.

Return type:

tuple[str, ...]

tests.test_snippet.options_of(cmd)[source]

The command’s options, keyed by the name they bind to.

Return type:

dict[str, Option]

tests.test_snippet.test_capture_commands_share_one_vocabulary()[source]

Both capture commands spell every shared option the same way.

The drift this guards against is silent and lands on the reader rather than the maintainer: an option learned on one command that means something else, or nothing at all, on the other.

--columns is the deliberate exception. Its whole purpose is to differ: a command wraps its own output to it, where a file was never wrapped.

tests.test_snippet.test_columns_defaults_differ_by_command()[source]

A snippet sizes itself to its longest line; a capture pins a width.

tests.test_snippet.test_snippet_command_writes_a_capture(invoke, tmp_path)[source]

The command colors a file and writes the picture where it was told.

tests.test_snippet.test_snippet_command_reads_stdin(invoke, tmp_path)[source]

A dash reads the source from stdin, where no file name identifies it.

tests.test_snippet.test_snippet_command_rejects_a_fragment_image(invoke, tmp_path)[source]

--fragment is an HTML notion: an SVG has no page to be pasted into.

tests.test_snippet.test_snippet_command_reports_an_unknown_language(invoke, tmp_path)[source]

A misspelled language is a clean CLI error, not a traceback.

tests.test_snippet.LATIN1_SOURCE = b"p\xe2te = 'bris\xe9e'\n"

A line of Python saved in Latin-1, which UTF-8 cannot decode.

tests.test_snippet.test_snippet_command_refuses_a_source_that_is_not_utf8(invoke, tmp_path, from_stdin)[source]

A source in another encoding is a usage error on SOURCE, not a traceback.

tests.test_snippet.test_snippet_command_leaves_the_output_alone_on_undecodable_stdin(tmp_path)[source]

Undecodable stdin is refused before the output file is opened.

In UTF-8 mode, sys.stdin reads undecodable bytes as surrogates, and those only fail once the capture is written, over the previous capture. The test runner’s stdin decodes strictly and never takes that path, so the command runs as its own process, its stdin set to surrogateescape on every Python version.

tests.test_snippet.test_snippet_prints_ansi_to_stdout(invoke, tmp_path)[source]

A dash target prints the escape sequences a terminal paints.

The one target that needs no rendering: what a terminal reads is the stream the capture was carried in all along.

tests.test_snippet.test_snippet_stdout_strips_color_when_piped(invoke, tmp_path)[source]

Piped, the escapes go and the code stays.

The reason this routes through echo rather than a bare write: a redirect that captured raw escape sequences would produce a file nobody can read.

tests.test_snippet.test_snippet_stdout_closes_on_one_newline(invoke, tmp_path)[source]

Exactly one, so the next prompt does not land on the last row.

tests.test_snippet.test_snippet_writes_an_ansi_file(invoke, tmp_path)[source]

The .ansi extension names the same format, written out.

tests.test_snippet.EPILOG_EXAMPLES = (['--output', 'basket.svg', 'basket.py'], ['--output', 'basket.html', '--syntax-style', 'dracula', 'basket.py'], ['--output', 'basket.svg', '--line-numbers', '--emphasize-lines', '12', 'basket.py'])

The argument lists the snippet help screen closes on, one per example.

tests.test_snippet.test_snippet_epilog_examples_do_what_they_say(invoke, tmp_path, arguments)[source]

Every example of the help screen runs, and a style it names paints the window.

The --theme of every Click Extra command is the help screen’s own theme, so an example spelling the syntax style that way ran without complaint and drew the default style instead of the one it named.