tests.test_pygments moduleΒΆ
- tests.test_pygments.lex(text, **options)[source]ΒΆ
Shorthand: lex
textand return(token_type, value)pairs.optionsgo to theAnsiColorLexer, liketrue_color=False.
- tests.test_pygments.collect_classes(klass, prefix='Ansi')[source]ΒΆ
Returns all classes defined in
click_extra.pygmentsthat are a subclass ofklass, and whose name starts with the providedprefix.
- tests.test_pygments.get_pyproject_section(*section_path)[source]ΒΆ
Descends into the TOML tree of
pyproject.tomlto reach the value specified bysection_path.
- tests.test_pygments.test_ansi_lexers_candidates(tmp_path)[source]ΒΆ
Look into Pygments test suite to find all ANSI lexers candidates.
Good candidates for ANSI colorization are lexers that are producing
Generic.Outputtokens, which are often used by REPL-like and scripting terminal to render text in a console.The list is manually maintained in Click Extra code, and this test is here to detect new candidates from new releases of Pygments.
Attention
The Pygments source code is downloaded from GitHub in the form of an archive, and extracted in a temporary folder.
The version of Pygments used for this test is the one installed in the current environment.
Danger
Security check While extracting the archive, we double check we are not fed an archive exploiting relative
..or.path attacks.
- tests.test_pygments.test_session_lexers_cover_every_shell_session_lexer()[source]ΒΆ
Every Pygments lexer built on
ShellSessionBaseLexerhas an ANSI variant.collect_session_lexersnames its lexers by hand rather than walking all of Pygments at import time, so this walk is what notices a new one.
- tests.test_pygments.test_plain_text(text, expected)[source]ΒΆ
Plain text without escape sequences passes through unchanged.
- tests.test_pygments.test_sgr_fg_standard(code, color)[source]ΒΆ
SGR 30-37 set standard foreground colors.
- tests.test_pygments.test_sgr_bg_standard(code, color)[source]ΒΆ
SGR 40-47 set standard background colors.
- tests.test_pygments.test_sgr_fg_bright(code, color)[source]ΒΆ
SGR 90-97 set bright foreground colors.
- tests.test_pygments.test_sgr_bg_bright(code, color)[source]ΒΆ
SGR 100-107 set bright background colors.
- tests.test_pygments.test_sgr_text_attribute(code, attr)[source]ΒΆ
SGR attribute codes set the corresponding text styling.
- tests.test_pygments.test_sgr_attribute_reset(set_code, reset_code, attr)[source]ΒΆ
Each attribute can be individually reset by its specific SGR code.
- tests.test_pygments.test_sgr22_resets_bold_and_faint()[source]ΒΆ
SGR 22 (normal intensity) resets both bold and faint simultaneously.
- tests.test_pygments.test_empty_sgr_is_reset()[source]ΒΆ
An empty SGR sequence (ESC [ m) is equivalent to SGR 0.
- tests.test_pygments.test_sgr39_resets_foreground()[source]ΒΆ
SGR 39 resets foreground color to default.
- tests.test_pygments.test_sgr49_resets_background()[source]ΒΆ
SGR 49 resets background color to default.
- tests.test_pygments.test_sgr39_keeps_other_attributes()[source]ΒΆ
SGR 39 only resets foreground; other attributes persist.
- tests.test_pygments.test_sgr_combined(params, expected_token)[source]ΒΆ
Multiple SGR codes in a single escape sequence are applied together.
- tests.test_pygments.test_256color_fg(index)[source]ΒΆ
SGR 38;5;n sets foreground to 256-color index.
- tests.test_pygments.test_256color_bg(index)[source]ΒΆ
SGR 48;5;n sets background to 256-color index.
- tests.test_pygments.test_24bit_rgb_fg_quantized(r, g, b, expected_idx)[source]ΒΆ
SGR 38;2;r;g;b quantizes to nearest 256-color index when true_color=False.
- tests.test_pygments.test_24bit_rgb_bg_quantized(r, g, b, expected_idx)[source]ΒΆ
SGR 48;2;r;g;b quantizes background to nearest 256-color index when true_color=False.
- tests.test_pygments.test_truecolor_fg_preserves_hex(r, g, b, expected_hex)[source]ΒΆ
With
true_color=True, SGR 38;2;r;g;b emitsToken.Ansi.FG_{hex}.
- tests.test_pygments.test_truecolor_bg_preserves_hex(r, g, b, expected_hex)[source]ΒΆ
With
true_color=True, SGR 48;2;r;g;b emitsToken.Ansi.BG_{hex}.
- tests.test_pygments.test_truecolor_combined_fg_and_bg()[source]ΒΆ
Foreground and background 24-bit RGB combine into a single compound token.
- tests.test_pygments.test_truecolor_with_attribute()[source]ΒΆ
Bold + 24-bit RGB combines into a single compound token.
- tests.test_pygments.test_truecolor_default_enabled()[source]ΒΆ
Default
AnsiColorLexer()preserves 24-bit RGB asFG_{rrggbb}tokens.
- tests.test_pygments.test_truecolor_explicit_disable_quantizes()[source]ΒΆ
AnsiColorLexer(true_color=False)quantizes RGB to the 256-color palette.
- tests.test_pygments.test_truecolor_invalid_range_ignored()[source]ΒΆ
Out-of-range RGB values are skipped in true-color mode too.
- tests.test_pygments.test_truecolor_truncated_params_ignored()[source]ΒΆ
Truncated RGB params set no color in true-color mode either.
The stray
2reads as SGR 2 (faint), as in the quantized mode: seetest_24bit_truncated_params.
- tests.test_pygments.test_truecolor_filter_forwards_flag()[source]ΒΆ
AnsiFilter(true_color=True)forwards the flag to its inner lexer.
- tests.test_pygments.test_truecolor_session_lexer_forwards_flag()[source]ΒΆ
Lexer kwarg
true_color=Trueflows through_AnsiFilterMixin.
- tests.test_pygments.test_true_color_lexer_option_reads_a_string(alias, value, expected)[source]ΒΆ
A lexer reads a
true_colorstring as the boolean it spells.
- tests.test_pygments.test_true_color_filter_option_reads_a_string(value, expected)[source]ΒΆ
The filter reads a
true_colorstring as the boolean it spells.
- tests.test_pygments.test_formatter_renders_truecolor_inline_style()[source]ΒΆ
AnsiHtmlFormatteremits inline style forFG_/BG_tokens.
- tests.test_pygments.test_formatter_renders_truecolor_background()[source]ΒΆ
Background 24-bit RGB renders as
background-colorinline style.
- tests.test_pygments.test_formatter_truecolor_combined_with_class_styling()[source]ΒΆ
Bold + RGB renders both a CSS class for Bold and an inline style for the color.
- tests.test_pygments.test_formatter_truecolor_fg_and_bg_nested_spans()[source]ΒΆ
Both fg and bg RGB on the same token produce two nested inline-style spans.
- tests.test_pygments.test_formatter_quantize_path_no_inline_styles()[source]ΒΆ
When
true_color=Falseis opted into, no inline styles are emitted.
- tests.test_pygments.test_formatter_osc8_with_truecolor_coexist()[source]ΒΆ
OSC 8 hyperlink and 24-bit RGB tokens cooperate in the same span.
Both mechanisms inject Private Use Area markers into the token stream (
_LINK_*for hyperlinks,_RGB_*for RGB colors) and rely on independent post-processing passes informat_unencoded. This test pins the contract that both rewrites happen and donβt interfere.
- tests.test_pygments.test_extra_css_matches_sgr_attributes()[source]ΒΆ
EXTRA_ANSI_CSS keys match the attribute component names.
- tests.test_pygments.test_extra_css_follows_the_style_table()[source]ΒΆ
Every attribute but blink renders with the declaration
Style.to_csswrites.Faintin particular dims to the sameopacityas adimstyle. Blink has no CSS declaration of its own and takes the keyframe animation instead.
- tests.test_pygments.test_non_sgr_csi_stripped()[source]ΒΆ
Non-SGR CSI sequences (cursor movement, etc.) are stripped.
- tests.test_pygments.test_csi_ends_on_its_final_byte(text, expected)[source]ΒΆ
A CSI sequence ends on its ECMA-48 final byte, a letter or not.
A final byte like the
~of the bracketed-paste markers ends the sequence: the text after it is kept whole.
- tests.test_pygments.test_escape_ends_on_its_final_byte(text, expected)[source]ΒΆ
An escape carrying intermediate bytes is stripped up to its final byte.
ESC # 8is the screen alignment test: the#is an intermediate byte and the8the final one. A grammar consuming one byte after the escape left that8in the text, wherestrip_ansi()removed it.
- tests.test_pygments.test_vt100_charset_stripped()[source]ΒΆ
VT100 charset selection escapes are stripped.
- tests.test_pygments.test_vt100_charset_g1_stripped()[source]ΒΆ
ESC ) designator for G1 charset is stripped.
- tests.test_pygments.test_unknown_escape_stripped()[source]ΒΆ
Unknown single-byte escape sequences are stripped.
- tests.test_pygments.test_bare_escape_at_end()[source]ΒΆ
A lone ESC at the end of input is consumed without error.
- tests.test_pygments.test_osc_sequence_stripped()[source]ΒΆ
Non-hyperlink OSC sequences are fully consumed and stripped.
- tests.test_pygments.test_osc_st_terminated_stripped()[source]ΒΆ
OSC sequences terminated by ST (ESC ) are fully stripped.
- tests.test_pygments.test_osc8_hyperlink(text, expected)[source]ΒΆ
OSC 8 hyperlinks emit link start/end tokens around the visible text.
- tests.test_pygments.test_osc8_with_sgr()[source]ΒΆ
OSC 8 hyperlink combined with SGR color preserves both.
- tests.test_pygments.test_osc8_unsafe_scheme_stripped(url)[source]ΒΆ
OSC 8 with unsafe or missing URL scheme is stripped silently.
- tests.test_pygments.test_osc8_implicit_close()[source]ΒΆ
A new OSC 8 link implicitly closes the previous one.
- tests.test_pygments.test_osc8_unclosed()[source]ΒΆ
An unclosed OSC 8 link is automatically closed at end of input.
- tests.test_pygments.test_osc8_close_without_open()[source]ΒΆ
An OSC 8 close without a preceding open is silently ignored.
- tests.test_pygments.test_osc8_survives_a_full_reset()[source]ΒΆ
SGR 0 inside a link resets the styling alone: the link closes at its OSC 8.
- tests.test_pygments.test_color_persists_until_changed()[source]ΒΆ
A foreground color set in one sequence persists until explicitly changed.
- tests.test_pygments.test_multiple_color_changes()[source]ΒΆ
Color can be changed multiple times without resetting.
- tests.test_pygments.test_attribute_stacking()[source]ΒΆ
Attributes accumulate: bold then italic produces bold+italic.
- tests.test_pygments.test_independent_fg_bg()[source]ΒΆ
Foreground and background colors are independent of each other.
- tests.test_pygments.test_sgr_with_trailing_semicolons()[source]ΒΆ
An empty trailing parameter is a
0, which resets the color set before it.
- tests.test_pygments.test_sgr_leading_semicolons()[source]ΒΆ
An empty leading parameter is a
0: a reset, then the color after it.The lexer shares the decoder of
split_ansi, which reads\x1b[;31mas red, so a page renders what a terminal shows.
- tests.test_pygments.test_sgr_double_semicolons()[source]ΒΆ
An empty parameter between two codes is a reset between them.
- tests.test_pygments.test_sgr_unknown_codes_ignored()[source]ΒΆ
Unknown SGR codes are silently ignored; known codes still apply.
- tests.test_pygments.test_256color_truncated_params()[source]ΒΆ
Truncated 256-color sequence (missing color index) leaves leftover codes.
38;5: code 38 triggers extended color handling but needs at least 2 more values (mode + index). Only 1 remains (5), so 38 skips. Then 5 is processed as SGR 5 (blink).
- tests.test_pygments.test_256color_out_of_range()[source]ΒΆ
256-color index outside 0-255 is ignored.
- tests.test_pygments.test_24bit_truncated_params()[source]ΒΆ
A truncated 24-bit RGB sequence sets no color.
38;2;255;128misses the blue channel, so the decoder skips38without consuming anything after it:2is then read as SGR 2 (faint), and the two channel values are unknown codes. The same readingsplit_ansigives the sequence.
- tests.test_pygments.test_24bit_out_of_range()[source]ΒΆ
24-bit RGB values outside 0-255 are ignored.
- tests.test_pygments.test_extended_color_unknown_mode()[source]ΒΆ
Extended color with unknown mode (not 5 or 2) sets no color.
38;3;100: code 38 is skipped on its own, since neither of the two color modes follows it.3is then read as SGR 3 (italic) and100as SGR 100 (bright black background), the readingsplit_ansigives the sequence.
- tests.test_pygments.test_non_numeric_sgr_params()[source]ΒΆ
Non-numeric characters in CSI params cause partial consumption.
ESC[abc;31mis not a valid SGR sequence. The regex consumesESC[aas a CSI withaas the final byte, leavingbc;31mtextas plain text.
- tests.test_pygments.test_empty_text_between_sequences()[source]ΒΆ
Sequences with no text between them produce no empty tokens.
- tests.test_pygments.test_newline_in_colored_text()[source]ΒΆ
Newlines within colored text are preserved in the token value.
- tests.test_pygments.test_interleaved_text_and_escapes()[source]ΒΆ
Complex interleaving of plain text and escape sequences.
- tests.test_pygments.test_lexer_resets_between_calls()[source]ΒΆ
Each call to get_tokens starts from a clean state.
- tests.test_pygments.test_ansi_styles_has_all_named_colors()[source]ΒΆ
Style dict contains entries for all 16 named foreground and background colors.
- tests.test_pygments.test_ansi_styles_has_256_palette()[source]ΒΆ
Style dict contains entries for all 256 foreground and background indices.
- tests.test_pygments.test_ansi_styles_excludes_all_attributes()[source]ΒΆ
All text attribute tokens are absent from the style dict.
Furoβs dark-mode CSS generator adds
color: #D0D0D0to every token in the style dict. For attribute tokens, this overrides actual foreground colors on compound tokens when the attribute rule appears later in the CSS cascade. All attribute styling is handled byEXTRA_ANSI_CSS/custom.cssinstead.
- tests.test_pygments.test_ansi_styles_count()[source]ΒΆ
Style dict has only color entries: 32 named + 512 indexed.
- tests.test_pygments.test_formatter_no_color_on_attribute_css()[source]ΒΆ
CSS rules for attribute-only tokens must not set a color property.
When an attribute token (like -Ansi-Strikethrough) has a
colorproperty in its CSS rule, it can override the foreground color of a sibling color token (like -Ansi-Red) if the attribute rule appears later in the CSS cascade. This regression test catches the issue that Furoβs dark-mode generator exposed.
- tests.test_pygments.test_palette_256_completeness()[source]ΒΆ
256-color palette has exactly 256 entries.
- tests.test_pygments.test_palette_256_hex_format()[source]ΒΆ
All palette values are 7-character hex strings.
- tests.test_pygments.test_lexer_map_completeness()[source]ΒΆ
LEXER_MAP has one entry per session lexer.
- tests.test_pygments.test_ansi_variant_belongs_to_this_module(original, variant)[source]ΒΆ
Each variant lives in
click_extra.pygments, under the name its entry point resolves, and derives from the lexer it varies.
- tests.test_pygments.test_ansi_variant_subclass_keeps_its_names()[source]ΒΆ
A subclass of a variant inherits its names and aliases, prefixed once.
- tests.test_pygments.test_ansi_filter_transforms_output_tokens()[source]ΒΆ
AnsiFilter converts Generic.Output tokens containing ANSI codes.
- tests.test_pygments.test_ansi_filter_passes_through_other_tokens()[source]ΒΆ
AnsiFilter does not modify tokens that are not Generic.Output.
- tests.test_pygments.test_formatter_css_classes_single_color()[source]ΒΆ
Single-color token gets the expected CSS classes.
- tests.test_pygments.test_formatter_css_classes_compound()[source]ΒΆ
Compound token gets decomposed CSS classes.
- tests.test_pygments.test_formatter_css_classes_256color()[source]ΒΆ
256-color tokens get the correct CSS class.
- tests.test_pygments.test_formatter_style_defs_contain_ansi_colors()[source]ΒΆ
get_style_defs() includes CSS rules for ANSI color tokens.
Two formatters on the same base style run on one augmented style class.
- tests.test_pygments.test_formatter_css_classes_cache_keyed_on_prefix()[source]ΒΆ
The shared class cache tells formatters with different prefixes apart.
- tests.test_pygments.test_formatter_osc8_hyperlink()[source]ΒΆ
OSC 8 hyperlink is rendered as an HTML <a> tag.
- tests.test_pygments.test_formatter_osc8_with_color()[source]ΒΆ
OSC 8 hyperlink combined with SGR color renders both link and color.
- tests.test_pygments.test_formatter_osc8_url_escaping()[source]ΒΆ
URLs with special HTML characters are properly escaped in href.
- tests.test_pygments.test_formatter_css_contains_link_rules()[source]ΒΆ
get_style_defs() includes CSS for hyperlink styling.
- tests.test_pygments.test_formatter_osc8_unsafe_scheme_no_link()[source]ΒΆ
Unsafe URL schemes produce no <a> tag in formatted output.
- tests.test_pygments.test_formatter_osc8_quotes_escaped_in_href()[source]ΒΆ
Quotes in a hyperlink URL cannot break out of the href attribute.
Pygments escapes quotes in token text only before its
2.21.0release, so the formatter escapes them itself, with the entities older Pygments writes. Ampersands are left to Pygments, and are escaped once.
- tests.test_pygments.test_real_world_ansi(text, expected_tokens)[source]ΒΆ
Real-world ANSI patterns from terminal tools and documentation references.
- tests.test_pygments.test_ansi_session_decodes_a_styled_input_line()[source]ΒΆ
An input line arriving styled keeps its colors instead of its escape bytes.
A session lexer reads any line opening on a prompt as a typed command, and a
--helpepilog writing$ my-cli pick --ripematches. A shell lexer reading its escapes would tokenize\x1b[36mas an operator between two runs of text, and leave the raw bytes in the page.
- tests.test_pygments.test_ansi_session_leaves_a_plain_input_line_to_the_shell_lexer()[source]ΒΆ
Input carrying no escape still gets its shell highlighting.
- tests.test_pygments.test_ansi_variant_keeps_the_text_of_its_original(original, variant)[source]ΒΆ
A variant renders a session free of escapes to the text of its original.
The blank lines opening and closing an output block included: each lexer reads its own prompts in
_PLAIN_SESSIONS, so it splits the text into input and output blocks at different places.
- tests.test_pygments.test_no_click_extra_help_screen_renders_raw_escapes()[source]ΒΆ
No help screen this package ships leaks an escape byte into its HTML.
The whole CLI tree is rendered the way
click:rundoes it, then lexed and formatted. Whatever the lexer fails to decode surfaces as a literal\x1bin the page, so the assertion reads the formatted output rather than the token stream.