tests.test_theme module

Tests for click_extra.theme: in-process isolation, built-in themes and [tool.<cli>.themes.<name>] config integration.

tests.test_theme.test_theme_does_not_leak_across_invocations()[source]

A --theme light invocation must not bleed into a later --help render.

Two back-to-back invocations of the same CLI in the same process:

  1. --theme light --help – selects the light palette for this call only.

  2. --help – no --theme argument, must fall back to the dark default.

The dark theme renders headings with \x1b[94m (bright blue); the light theme uses \x1b[35m (magenta, chosen to stay distinct from its blue options). If the second invocation picks up the first’s choice via process-wide state, it leaks the light palette and the assertion below fails.

tests.test_theme.test_theme_default_unchanged_after_invocation()[source]

An invocation with --theme light must not mutate the process-wide default.

tests.test_theme.test_demo_themes_renders_each_builtin_palette()[source]

click-extra themes renders the sample help once per built-in theme, each in its own palette.

Guards against the whole gallery collapsing onto the default palette: the themes command drives get_current_theme through the context THEME meta, so a regression there would render all seven screens identically.

tests.test_theme.test_demo_themes_renders_only_the_named_palettes()[source]

Named theme IDs narrow the gallery down, and keep the order they were given.

tests.test_theme.test_demo_themes_resolves_auto_to_a_palette_name()[source]

auto is a directive, so the gallery labels the palette it lands on.

Which palette that is depends on the background detected from the environment, so the expectation is read back from the same resolver rather than pinned to dark.

tests.test_theme.test_demo_themes_rejects_an_unknown_palette()[source]

An unknown ID fails, and the error lists every name that would have worked.

tests.test_theme.test_theme_meta_key_matches_registry()[source]

get_current_theme() reads from the same key ThemeOption writes.

tests.test_theme.test_font_role_slots_are_known_and_disjoint()[source]

LITERAL_STYLES / REPLACEABLE_STYLES must classify real, distinct slots.

The two frozensets encode the man-pages(7) bold/italic font roles by slot name and are maintained by hand, so guard against drift: every name must be a real HelpTheme field, the two roles must not overlap, and the representative slots must keep their expected role.

tests.test_theme.test_builtin_themes_alphabetical()[source]

Built-in themes are declared alphabetically (matters for --theme choices).

tests.test_theme.test_builtin_themes_build_on_first_lookup()[source]

Importing the package builds no theme, and a --help builds dark only.

Reading the theme names, which --theme does to list its choices, builds nothing either. Checked in a pristine interpreter, since the other tests have built every theme already.

tests.test_theme.test_builtin_themes_resolve_to_one_instance()[source]

A built-in theme is the same instance in every mapping that holds it.

tests.test_theme.test_theme_registry_takes_dict_mutations(monkeypatch)[source]

Registering, replacing and removing entries work on built-in names too.

tests.test_theme.test_builtin_themes_are_helptheme_instances()[source]

Every BUILTIN_THEMES entry is a HelpTheme instance.

tests.test_theme.test_cloup_constructors_keep_the_subclass(palette)[source]

dark() and light() return a click-extra theme carrying cloup’s palette.

Cloup builds both palettes from cloup.Style, which to_dict and cascade reject, so each style is widened to click-extra’s.

tests.test_theme.test_with_rejects_unknown_slots()[source]

Every argument naming no slot is listed, in a stable order.

tests.test_theme.test_with_takes_none_as_a_value()[source]

None resets a slot, where cloup’s version reads it as no change.

tests.test_theme.test_builtin_themes_follow_manpage_font_convention(theme_name)[source]

Every built-in theme bolds literal slots and italicizes replaceable ones.

Encodes the man-pages(7) typographic convention (LITERAL_STYLES bold, REPLACEABLE_STYLES italic) as an invariant across all palettes, so adding a theme or tweaking a slot can’t silently drop the literal/replaceable distinction. The manpage theme renders it with no color at all.

tests.test_theme.test_theme_round_trips_through_dict(theme_name)[source]

HelpTheme.to_dict/from_dict round-trips every built-in theme.

tests.test_theme.test_builtin_theme_definition_matches_to_dict(theme_name)[source]

Each built-in definition equals what to_dict emits for its theme.

So a definition sets no slot to its default value, which to_dict leaves out.

tests.test_theme.test_to_dict_omits_identity_slots()[source]

Slots left at the identity default do not appear in to_dict output.

tests.test_theme.test_to_dict_emits_cross_ref_highlight_only_when_overridden()[source]

cross_ref_highlight is emitted only when it differs from the default.

tests.test_theme.test_from_dict_rejects_unknown_keys()[source]

Typos like optoin raise TypeError instead of being silently dropped.

tests.test_theme.test_cascade_overrides_only_set_slots()[source]

cascade keeps base’s slots wherever the overlay leaves them at default.

tests.test_theme.test_cascade_returns_new_instance_when_overlay_changes_anything()[source]

Even a single-slot overlay produces a distinct theme instance.

tests.test_theme.test_cascade_round_trips_through_dict()[source]

self.to_dict() wins over base.to_dict() slot-by-slot.

tests.test_theme.test_cascade_rejects_non_theme_base()[source]

cascade rejects anything that is not a HelpTheme.

tests.test_theme.test_themechoice_choices_track_global_registry()[source]

Outside any context, choices reflects the module-level registry.

Plus the reserved auto directive, which every CLI takes and no registry holds.

tests.test_theme.test_themechoice_choices_pick_up_context_overrides()[source]

A theme stashed under THEME_OVERRIDES shows up in choices.

tests.test_theme.test_branded_themes_meet_wcag_aa_large(theme_name, slot)[source]

Branded themes’ readable-text slots clear WCAG AA Large (3.0+).

Branded palettes are deliberate 24-bit RGB choices, so we can hold them to a real WCAG threshold. AA Large is the realistic floor: full AA (4.5+) is unattainable for some published themes (like Solarized’s accent blue on its base03 background sits at ~4.08).

A regression that drops one of these slots below 3.0 means the theme is less readable than what currently ships and warrants a deliberate palette tweak rather than a silent slip.

tests.test_theme.test_themes_meet_legibility_floor(theme_name)[source]

Every styled slot in every theme stays above the legibility floor.

Subdued slots (debug, bracket, default, …) are allowed to fall below WCAG AA Large by design, but no slot should be effectively invisible against the theme’s assumed background. Catches accidental palette tweaks like setting an attribute to nearly the background color.

tests.test_theme.test_themechoice_inert_when_registry_empty(monkeypatch)[source]

With no themes available, ThemeChoice ignores any value instead of failing.

A program can empty the registry: then even the built-in dark default cannot resolve, so convert returns None rather than aborting the invocation.

tests.test_theme.test_cli_runs_with_empty_theme_registry(invoke, monkeypatch)[source]

A CLI still runs when no themes are available.

The built-in –theme option defaults to dark; with an empty registry that default must stay inert instead of crashing the whole command.

tests.test_theme.test_themes_from_config_overrides_existing_theme()[source]

Known theme names cascade on top of the matching built-in palette.

tests.test_theme.test_themes_from_config_creates_standalone_theme()[source]

Unknown theme names build a stand-alone theme with unset slots at default.

tests.test_theme.test_themes_from_config_does_not_mutate_global_registry()[source]

themes_from_config is pure: the module-level registry is untouched.

tests.test_theme.test_get_theme_registry_falls_back_to_global_without_ctx()[source]

get_theme_registry(None) returns a copy of the module-level registry.

tests.test_theme.test_config_loads_new_theme(invoke, create_config)[source]

A [tool.<cli>.themes.<name>] table registers a new theme for the invocation.

tests.test_theme.test_config_overrides_existing_theme_palette(invoke, create_config)[source]

A [tool.<cli>.themes.dark] table cascades onto the built-in dark palette.

tests.test_theme.test_config_theme_appears_in_help_metavar(invoke, create_config)[source]

--help lists config-defined themes alongside the built-ins.

tests.test_theme.test_config_theme_validation_error(invoke, create_config)[source]

A malformed [tool.<cli>.themes.<name>] table is rejected with a rooted path.

tests.test_theme.test_validate_themes_config_codes(themes, code)[source]

A malformed themes table is reported under a snake-case code, like every other configuration error.

tests.test_theme.test_command_validators_keep_the_theme_validator(invoke, create_config)[source]

Validators a command declares join the built-in themes validator.

@command(config_validators=...) forwards them to the default --config option, which keeps the built-in validator beside them: a malformed [tool.<cli>.themes.<name>] table is reported as a configuration validation error, not a TypeError.

tests.test_theme.test_config_theme_does_not_leak_across_invocations(invoke, create_config)[source]

Themes defined in invocation N are not visible in invocation N+1.

tests.test_theme.test_validate_config_catches_bad_theme(invoke, create_config)[source]

--validate-config surfaces the same ValidationError as the runtime path.

tests.test_theme.test_theme_auto_resolves_to_detected_palette(invoke, monkeypatch, env, expected_name)[source]

--theme=auto picks the built-in palette implied by the terminal background.

tests.test_theme.test_theme_auto_from_config(invoke, create_config, monkeypatch)[source]

theme = "auto" in the config file makes detection the effective default.

tests.test_theme.test_theme_auto_advertised_in_help_metavar(invoke)[source]

The reserved ‘auto’ directive is listed among the values –theme takes.

It resolves to a palette rather than being one, but it works on every Click Extra CLI, and a value named nowhere is a value nobody finds.

tests.test_theme.test_themechoice_accepts_auto_directive()[source]

‘auto’ converts to itself though it is not a registered palette.

tests.test_theme.test_theme_option_query_background_opt_in()[source]

The live OSC 11 query is off unless a CLI explicitly opts in.

tests.test_theme.test_resolve_auto_theme_forwards_query_flag(monkeypatch)[source]

query_background gates the live query that env-var detection ignores.

tests.test_theme.test_theme_envvar_picks_palette(invoke, monkeypatch, env_value, expected_name)[source]

CLICK_EXTRA_THEME themes a CLI invoked without --theme.

tests.test_theme.test_theme_envvar_accepts_auto_directive(invoke, monkeypatch)[source]

The reserved ‘auto’ directive resolves from the variable too.

tests.test_theme.test_theme_flag_outranks_envvar(invoke, monkeypatch)[source]

An explicit --theme beats the machine-wide variable.

tests.test_theme.test_cli_envvar_outranks_theme_envvar(invoke, monkeypatch)[source]

The <CLI>_THEME variable is more specific than the machine-wide one.

tests.test_theme.test_config_outranks_theme_envvar(invoke, create_config, monkeypatch)[source]

A palette pinned in the CLI’s own config file is more specific.

tests.test_theme.test_unknown_theme_envvar_warns_and_keeps_default(invoke, monkeypatch)[source]

A typo in a shell profile must not break every Click Extra CLI at once.

tests.test_theme.test_theme_from_env_reaches_the_help_screen(invoke, monkeypatch, envvar)[source]

A palette named by the environment paints the screen it exists to paint.

Click processes a typed --help before an untyped --theme, so the palette only reaches the help screen through the pre-pass in Command._resolve_presentation_eagerly.

tests.test_theme.test_theme_flag_after_help_still_paints_it(invoke)[source]

--help --theme nord renders under nord, like the reverse order.

Both are typed, so Click sorts --help first and would render before the palette settles: the same pre-pass covers it.