tests.test_commands module

Test defaults of our custom commands, as well as their customizations and attached options, and how they interact with each others.

tests.test_commands.test_module_root_declarations()[source]

Verify click_extra.__all__ is a superset of click and cloup.

Sort order is enforced by ruff (RUF022).

tests.test_commands.test_deprecated_click_exports_are_forwarded(name)[source]

Click’s deprecated names resolve to Click’s objects, never bound eagerly.

A binding would bypass Click’s deprecation shim, and fetching it at import time would warn in every program importing click-extra. Whether Click warns on access depends on its version, so only the forwarding is checked.

tests.test_commands.test_import_raises_no_deprecation_warning(statement)[source]

Importing the package sets off no deprecation warning.

Cloup lists Click’s deprecated names in its __all__, so a plain star import of cloup fetches each of them, and Click warns once per name. A star import of click-extra would do the same for any such name its own __all__ declared.

Runs on every cell of the matrix, not as a once test: a deprecation shows first on the cells tracking the development branches of Click and Cloup, like the warning Click raises when a class is created with an override of a member it retired.

tests.test_commands.test_public_namespace_integrity()[source]

The package namespace and __all__ agree: nothing missing or extra.

click ships no __all__, so its star import would otherwise leak every submodule bound by click’s __init__ (click.core, click.globals, …) into the package namespace, handing out click’s un-enhanced classes and shadowing the globals builtin. Those bindings are scrubbed at the end of click_extra/__init__.py: verify the scrub held, that every public binding is declared in __all__, and that every declared name resolves (eagerly, lazily, or as one of our own submodules).

tests.test_commands.test_no_debugger_ballast_on_import()[source]

Importing the package must not load the test tooling nor the debugger stack.

click_extra.testing imports click.testing, whose module-level import pdb cascades into asyncio and the _pyrepl machinery on Python 3.13+. Loaded eagerly, that chain ends up bundled into every Nuitka-compiled CLI binary, so the test tooling is exported lazily: check from a pristine interpreter that none of it leaks at import time.

tests.test_commands.CLOUP_METADATA_PROBE_VERSION = (4, 0, 0)

Last cloup release importing importlib.metadata at module scope.

Up to this release, cloup reads Click’s version at import time, which loads importlib.metadata and the email tree behind it. janluke/cloup#242 removes that probe, so any later cloup keeps both off the startup path.

tests.test_commands.test_no_heavy_dependency_ballast_on_startup()[source]

Importing the package and rendering a --help must not load the dependencies the package defers.

Each module below is imported where it is used rather than at module scope, so that a CLI pays for it only on the code path that needs it: see the deferred-imports house rule in AGENTS.md. The probe runs a hello-world through decoration and --help in a pristine interpreter, since pytest itself has most of these loaded already.

importlib.metadata and the email tree it drags in join the list above CLOUP_METADATA_PROBE_VERSION only: up to that release, cloup imports them at module scope itself.

tests.test_commands.test_import_module_budget()[source]

import click_extra stays within a module-count budget.

A coarse net under the denylist above: a new eager import of a dependency with a deep tree shows up here as a jump of tens of modules, whichever package it comes from. Measured at 264 modules on Python 3.14 and cloup 4.0.0; the budget leaves room for the platform and version differences of the stdlib.

tests.test_commands.STARTUP_RATIO_TO_CLICK = 3.0

Most a hello-world --help of click-extra may take, as a multiple of Click’s.

Measured at 2.4 on Python 3.14 with Click 8.5.0, for the 10.0.0 release.

tests.test_commands.HELLO_CLICK = 'import click\n\n\n@click.command()\n@click.option("--name", default="World", help="The person to greet.")\ndef hello(name):\n    """Simple program that greets NAME."""\n    click.echo(f"Hello, {name}!")\n\n\nif __name__ == "__main__":\n    hello()\n'

The hello-world CLI the startup comparison times, written with Click.

tests.test_commands.test_startup_time_stays_within_a_ratio_of_click(tmp_path)[source]

A hello-world --help takes at most a few times as long as Click’s own.

Milliseconds depend on the runner, so the bound is relative: both CLIs run in alternation in fresh interpreters, and the best run of each is compared, as the one a loaded runner disturbs least. The children run without the color and coverage variables, which would add work to one side only.

tests.test_commands.test_lazy_test_tooling_exports()[source]

The lazy test-tooling names resolve, cache, and show up in dir().

tests.test_commands.test_lazy_converter_exports()[source]

The converter names resolve on first access, off the startup path.

Checked in a pristine interpreter, since another test may have imported the converters already.

tests.test_commands.LAZY_REGISTRIES = ('_LAZY_BUG_REPORT', '_LAZY_CAPTURES', '_LAZY_CONVERTERS', '_LAZY_DISPLAYS', '_LAZY_TEST_TOOLING')

The registries of names the package root serves through its __getattr__.

tests.test_commands.test_lazy_exports_are_declared_and_typed(registry)[source]

Each lazy name is in __all__, typed, and resolves to its source object.

A lazy name has no binding until its first access, so the TYPE_CHECKING import in click_extra/__init__.py is what shows it to type checkers.

tests.test_commands.test_lazy_export_modules_stay_off_startup()[source]

import click_extra loads none of the modules the lazy names live in.

Checked in a pristine interpreter, since other tests have imported them.

tests.test_commands.test_lazy_export_modules_are_visible_to_freezers()[source]

Each module a lazy name lives in has an import statement in a function.

Nuitka bundles what a live import statement names. It drops the TYPE_CHECKING block as dead code, and cannot follow a computed import_module() call, so a module missing here is missing from every binary built on click-extra, which then crashes on the lazy name.

tests.test_commands.all_command_cli()[source]

A CLI that is mixing all variations and flavors of subcommands.

tests.test_commands.test_unknown_option(invoke, all_command_cli)[source]
tests.test_commands.test_short_option_error_enhancement(invoke, cli_options, args, exit_code, expected_fragment)[source]

Command.parse_args improves error messages for single-dash multi-character tokens whose first character is not a registered short option. Vanilla Click would split -dbgwrong character by character and report “No such option: -d”; we re-raise with the full token and close-match suggestions instead.

The enhancement must not interfere with valid -abc-style combining or with the per-character diagnostic when a later character is unknown.

Upstream context: https://github.com/pallets/click/issues/2779

tests.test_commands.test_unknown_command(invoke, all_command_cli)[source]
tests.test_commands.test_required_command(invoke, all_command_cli, assert_output_regex)[source]
tests.test_commands.test_group_help(invoke, all_command_cli, param, exit_code, assert_output_regex)[source]
tests.test_commands.test_help_eagerness(invoke, all_command_cli, params, exit_code, expect_help, expect_empty_stderr, assert_output_regex)[source]

See: https://click.palletsprojects.com/en/stable/click-concepts/#callback-evaluation-order

tests.test_commands.test_help_custom_name(invoke)[source]

Removes the -h short option as we reserve it for a custom -h/--header option.

See: https://github.com/kdeldycke/mail-deduplicate/issues/762

tests.test_commands.test_subcommand_help(invoke, all_command_cli, cmd_id, param, assert_output_regex)[source]
tests.test_commands.test_subcommand_execution(invoke, all_command_cli, cmd_id)[source]
tests.test_commands.test_integrated_version_value(invoke, all_command_cli)[source]
tests.test_commands.test_colored_bare_help(invoke, cmd_decorator, param)[source]

Extra decorators are always colored.

Even when stripped of their default parameters, as reported in: https://github.com/kdeldycke/click-extra/issues/534 https://github.com/kdeldycke/click-extra/pull/543

tests.test_commands.test_duplicate_option(invoke)[source]

See: - https://kdeldycke.github.io/click-extra/commands.html#change-default-options - https://github.com/kdeldycke/click-extra/issues/232

tests.test_commands.test_no_option_leaks_between_subcommands(invoke, assert_output_regex)[source]

As reported in https://github.com/kdeldycke/click-extra/issues/489.

tests.test_commands.test_option_group_integration(invoke, assert_output_regex)[source]
tests.test_commands.test_show_envvar_parameter(invoke, cmd_decorator, ctx_settings, expected_help)[source]
tests.test_commands.test_show_choices_parameter(ctx_settings, expected)[source]

The show_choices context setting is forced on every option when set.

tests.test_commands.test_raw_args(invoke)[source]

Raw args are expected to be scoped in subcommands.

tests.test_commands.test_lazy_group(invoke, tmp_path, lazy_cmd_decorator, lazy_group_decorator)[source]

Test extends the snippet from Click documentation.

tests.test_commands.write_produce_modules(tmp_path)[source]

Write the command modules the sectioned lazy-group tests import.

tests.test_commands.test_lazy_group_sections(invoke, tmp_path)[source]

A LazySubcommand files its command under the section it declares.

tests.test_commands.test_lazy_group_lists_declared_help_without_importing(invoke, tmp_path)[source]

A lazy subcommand declaring its help is listed without being imported.

tests.test_commands.test_lazy_group_keeps_a_listed_subcommand_in_place(invoke, tmp_path)[source]

Importing a subcommand listed by its declared help keeps its place.

tests.test_commands.test_cli_help_imports_no_lazy_subcommand()[source]

The root help of the click-extra CLI lists its lazy subcommands unimported.

tests.test_commands.test_cli_lazy_subcommand_lists_as_its_command(name)[source]

The help a lazy subcommand declares lists it the way the command does.

The declared text and the command’s own help are two copies of one sentence: this fails the moment they drift apart.

tests.test_commands.test_lazy_group_section_shared_with_eager_subcommand(invoke, tmp_path)[source]

A Section can hold both eagerly and lazily registered subcommands.

tests.test_commands.test_lazy_group_no_default_section(invoke, tmp_path)[source]

fallback_to_default_section=False hides a subcommand but keeps it invocable.

tests.test_commands.test_lazy_subcommand_normalizes_bare_import_paths(tmp_path)[source]

A bare import path is normalized into a LazySubcommand.

tests.test_commands.test_decorator_overrides()[source]

Ensure our decorators are not just alias of Click and Cloup ones.

tests.test_commands.test_decorator_cls_parameter(klass, should_raise)[source]

Decorators accept custom cls parameters.

tests.test_commands.test_help_shows_group_help(invoke)[source]

mycli help produces the same output as mycli --help.

tests.test_commands.test_help_shows_subcommand_help(invoke)[source]

mycli help greet matches mycli greet --help.

tests.test_commands.test_help_nested_group(invoke)[source]

mycli help sub leaf resolves through nested groups.

tests.test_commands.test_help_nonexistent_subcommand(invoke)[source]

mycli help nosuch reports an error.

tests.test_commands.test_help_subcommand_of_non_group(invoke)[source]

mycli help leaf deeper errors when leaf is not a group.

tests.test_commands.test_help_disabled(invoke)[source]

help_command=False suppresses auto-injection.

tests.test_commands.test_help_user_override(invoke)[source]

User-defined help subcommand replaces the auto-injected one.

tests.test_commands.test_help_appears_in_listing(invoke)[source]

The help subcommand is visible in the group’s command list.

mycli help --search term finds matching subcommands.

tests.test_commands.test_help_search_skips_hidden_subcommands(invoke)[source]

A hidden subcommand stays out of the search, as it stays out of help.

tests.test_commands.test_help_search_no_match(invoke)[source]

mycli help --search term with no matches.

tests.test_commands.test_help_in_all_command_cli(invoke, all_command_cli)[source]

The help subcommand works on the fixture CLI.

tests.test_commands.test_help_for_subcommand_in_all_command_cli(invoke, all_command_cli)[source]

help default-subcommand works on the fixture CLI.

tests.test_commands.kitchen_group(**kwargs)[source]

A group whose subcommands are declared out of alphabetical order.

tests.test_commands.listed_subcommands(help_screen)[source]

Names listed under the Commands: heading of a help screen.

tests.test_commands.test_subcommand_order_in_help_screen(invoke, group_kwargs, expected)[source]

Cloup renders the help screen from sections, never from list_commands().

tests.test_commands.test_subcommand_order_in_list_commands(group_kwargs, expected)[source]

The flat listing feeding --tree, man pages, specs and completion.

tests.test_commands.test_subcommand_order_agrees_across_renderers(invoke)[source]

Every rendering of a command tree lists subcommands in the same order.

A renderer reaching for its own accessor would let a group’s help screen disagree with its man page or its completion spec. They all go through list_commands(), and this pins that down for the whole population rather than one renderer at a time.

tests.test_commands.test_iter_subcommand_contexts_builds_a_context_per_subcommand()[source]

Each subcommand comes with a context named after it, under its parent’s.

tests.test_commands.ghost_pantry()[source]

A lazy group also listing a name it cannot resolve, like a broken plugin.

tests.test_commands.test_tree_walks_agree_on_lazy_and_unresolvable_subcommands(invoke, tmp_path)[source]

Every walk of a command tree imports a lazy subcommand and skips a dead name.

A group can list a name it cannot resolve, like a plugin whose module broke. The walks all go through iter_subcommands, so none of them crashes on that name, and none drops a lazy subcommand it has not imported yet. Each walk gets a fresh group, so no walk finds the subcommand imported by another.

tests.test_commands.test_option_order_agrees_across_renderers(invoke)[source]

Every rendering of a command lists options in the same order.

Cloup draws the ungrouped section last, and Click Extra sends its own groups past it, so a renderer reaching for its own accessor would let a help screen disagree with its man page or its completion spec. They all resolve the section order through split_option_groups().

Pins the taxonomy too: each rendering is filtered down to the flags DEFAULT_OPTION_GROUPS declares, so the order they are declared in is the order a reader sees.

tests.test_commands.test_argument_help_agrees_across_renderers(invoke, argument_decorator)[source]

An argument’s help reaches every rendering, whatever its Argument class.

The help screen, the man page and the JSON export all resolve an operand’s description through the same helper, so a plain click.argument carries the same text everywhere.

tests.test_commands.test_deprecated_command_label_matches_click(invoke, deprecated, label)[source]

A deprecated command carries Click’s own marker, reason string included.

The marker is checked on the help screen and on full_short_help(), which feeds --tree, the man page, the JSON export and the completion specs.

tests.test_commands.test_full_short_help_drops_the_no_rewrap_marker()[source]

A first paragraph opening on Click’s \b loses the marker, as in Click.

full_short_help() feeds --tree, the man page, the completion specs and the click:tree summary table: a stray \x08 there moves the terminal cursor back one column, and turns the man page entry into a no-fill block.

tests.test_commands.test_every_default_option_lands_in_a_section()[source]

No option default_params() returns escapes DEFAULT_OPTION_GROUPS.

An option no section claims stays ungrouped, which draws it in the command’s own Options block where it reads as one the CLI author declared. Nothing else reports that: the help screen renders, and every cross-renderer check still agrees, because they all read the same wrong layout.

tests.test_commands.test_default_option_groups_name_no_stale_flag()[source]

Every flag DEFAULT_OPTION_GROUPS declares is one default_params() returns.

A renamed or dropped option otherwise leaves a dead entry behind. And a flag landing in two sections is silently taken by the later one, since _assign_option_groups writes them in order.

tests.test_commands.test_option_swapped_by_a_params_hook_keeps_its_section()[source]

An option a params hook replaces stays in the section of the original.

default_params files the instances it builds, so a replacement built after that call carries none, and cloup draws it in the command’s own Options block where it reads as one the CLI author declared. Each default option is swapped in turn, sections being declared one flag at a time: an option losing its own would pass unseen while its siblings still hold theirs.

tests.test_commands.test_params_hook_runs_once_per_decorated_command()[source]

A params hook builds one set of options per command, and never shares it.

A decorator built once and applied twice still gives each command its own option instances, the second application calling the hook again.

tests.test_commands.test_lazy_group_subcommand_order_is_stable_across_loading(tmp_path, monkeypatch)[source]

A lazy subcommand holds its slot before and after it is imported.

Importing appends the command to self.commands, so registration order read off that dictionary alone would reshuffle mid-run.

tests.test_commands.test_lazy_group_defaults_to_alphabetical_order()[source]

Sorting moved from __init__ to listing time, with the same result.

tests.test_commands.test_option_priorities_leave_processing_order_alone(invoke)[source]

The help screen reorders while params and the callbacks do not.

click.core.iter_params_for_processing breaks eager-option ties on declaration order, which is why --time measures everything and --accessible lowers the --color default before it resolves. Reordering the help screen must not disturb any of that.

tests.test_commands.test_option_priorities_match_flags_then_destination()[source]

A flag pair sharing one destination stays addressable one flag at a time.

tests.test_commands.test_option_priorities_never_reorder_positional_arguments()[source]

Argument order is part of the command’s grammar, not of its presentation.

tests.test_commands.test_command_listing_is_not_cut_by_an_abbreviation()[source]

A subcommand’s line in its parent’s list holds its whole first sentence.

Click ends the listing at the first word closing on a period, so an abbreviation mid-sentence (vs., e.g., etc.) cuts it into a fragment: gradient once read “Render 24-bit RGB gradients vs.”, which says nothing.

The guard is narrower than “the listing reads well”: it only catches a cut landing before the first sentence ends, which is the one failure a docstring can cause without anyone noticing.

tests.test_commands.test_no_help_screen_leaks_a_no_rewrap_marker()[source]

A \b marker never reaches the rendered help of any command.

Click strips the marker only where it opens a paragraph, and paragraphs are split on blank lines. An epilog writing one between two indented blocks without a blank line around it therefore ships the raw backspace character: a terminal hides it, an HTML page does not.