tests.test_cli_wrapper module¶
Tests for the CLI wrapper feature.
- tests.test_cli_wrapper.GREET_SCRIPT = 'import click\n\n@click.command()\n@click.option("--name", default="World", help="Name to greet.")\ndef hello(name):\n """Greet someone."""\n click.echo(f"Hello, {name}")\n\nif __name__ == "__main__":\n hello()\n'¶
Plain
@click.command()script: patched via decorator defaults.
- tests.test_cli_wrapper.CUSTOM_CLS_SCRIPT = 'import click\n\nclass RecipeGroup(click.Group):\n """Custom group like Flask\'s FlaskGroup."""\n\n@click.command(cls=RecipeGroup)\ndef kitchen():\n """Manage recipes and ingredients."""\n\n@kitchen.command()\n@click.option("--servings", default=4, help="Number of servings.")\ndef bake(servings):\n """Bake a cake."""\n click.echo(f"Baking for {servings}")\n\nif __name__ == "__main__":\n kitchen()\n'¶
Script with explicit
cls=RecipeGroup: patched via method patching.
- tests.test_cli_wrapper.MULTI_OPTION_SCRIPT = 'import click\n\n@click.command()\n@click.option("--city", default="Paris", help="City name.")\n@click.option("--unit", default="celsius", help="Temperature unit.")\n@click.option("--verbose", is_flag=True, help="Show details.")\ndef weather(city, unit, verbose):\n """Check the weather."""\n msg = f"{city}: 22 {unit}"\n if verbose:\n msg += " (detailed)"\n click.echo(msg)\n\nif __name__ == "__main__":\n weather()\n'¶
Script with multiple options for config passthrough tests.
- tests.test_cli_wrapper.PACKAGE_GROUP_SRC = 'import click\n\n@click.group()\ndef cli():\n """Manage a produce stand."""\n\n@cli.command()\ndef restock():\n """Restock the shelves."""\n click.echo("Restocked")\n'¶
Module-level Click group, exposed by a package inside a project directory.
- tests.test_cli_wrapper.ORCHARD_SCRIPT = 'import click\n\n@click.group(context_settings={"auto_envvar_prefix": "ORCHARD"})\ndef orchard():\n """Tend the orchard."""\n\n@orchard.command(context_settings={"auto_envvar_prefix": "PRESS"})\n@click.option("--batch", type=int, default=3, help="Apples per batch.")\ndef press(batch):\n """Press apples into cider."""\n click.echo(f"Pressing {batch} apples")\n\nif __name__ == "__main__":\n orchard()\n'¶
Group whose subcommand sets an environment variable prefix of its own.
- tests.test_cli_wrapper.greet_script(tmp_path)[source]¶
A minimal Click CLI script for wrapping tests.
- tests.test_cli_wrapper.custom_cls_script(tmp_path)[source]¶
A Click CLI with explicit
cls=CustomGroup(like Flask’s FlaskGroup).
- tests.test_cli_wrapper.weather_script(tmp_path)[source]¶
A Click CLI with multiple options for config tests.
- tests.test_cli_wrapper.make_project(tmp_path)[source]¶
Build a local project directory exposing a Click group at module level.
The returned factory writes packaging metadata (
pyproject.tomlorsetup.cfg) plus a package, in either the flat or src layout. On teardown it undoes thesys.pathandsys.modulesmutations that resolving a project directory triggers, keeping the tests isolated.
- tests.test_cli_wrapper.test_patched_command_no_extra_params()[source]¶
Patched commands carry no default_params.
- tests.test_cli_wrapper.test_resolve_py_file_missing(tmp_path)[source]¶
A .py path that doesn’t exist falls through to module resolution.
- tests.test_cli_wrapper.test_resolve_directory_flat_layout(make_project)[source]¶
A project directory resolves via its pyproject.toml entry point.
- tests.test_cli_wrapper.test_resolve_directory_src_layout(make_project)[source]¶
A src-layout project puts its src/ directory on sys.path.
- tests.test_cli_wrapper.test_resolve_directory_setup_cfg(make_project)[source]¶
console_scripts in setup.cfg resolve when no pyproject.toml is present.
- tests.test_cli_wrapper.test_resolve_directory_duplicate_scripts(make_project)[source]¶
Multiple script names pointing to one target resolve unambiguously.
- tests.test_cli_wrapper.test_resolve_directory_run(runner, make_project)[source]¶
The default run mode imports and executes a project directory’s CLI.
- tests.test_cli_wrapper.test_resolve_directory_introspect(make_project)[source]¶
Introspection discovers the command tree of a project directory.
- tests.test_cli_wrapper.test_resolve_directory_no_scripts(tmp_path)[source]¶
A project without console scripts raises an actionable error.
- tests.test_cli_wrapper.test_resolve_directory_multiple_scripts(make_project)[source]¶
Distinct entry-point targets cannot be disambiguated and raise.
- tests.test_cli_wrapper.test_resolve_directory_package_not_found(tmp_path)[source]¶
An entry point whose package is absent on disk raises.
- tests.test_cli_wrapper.test_run_invokes_target(runner, script_fixture, target_args, expected_text, request)[source]¶
The run subcommand forwards arguments to the target CLI.
- tests.test_cli_wrapper.test_run_colorizes(runner, script_fixture, target_args, request)[source]¶
Help output contains ANSI escape codes.
- tests.test_cli_wrapper.test_run_highlights_keywords_with_custom_cls(runner, custom_cls_script)[source]¶
Options and subcommands are individually styled, not just headings.
- tests.test_cli_wrapper.test_wrapped_crash_gets_no_bug_report(runner, tmp_path, source, wrap_args)[source]¶
A crash of the wrapped CLI is a bug of that CLI: Click Extra reports none.
The
click-extraCLI has areport_url, so its root would otherwise end the run with a report for the issues of Click Extra.
- tests.test_cli_wrapper.test_run_usage_line_ignores_the_launcher(runner, make_project, outer_package, monkeypatch)[source]¶
The wrapped target’s usage line names the script, not click-extra’s launcher.
A target whose Click command detects its own program name reads
__main__.__package__, which states how click-extra itself was launched. Left alone, a documentation build runningpython -m sphinxpaints a wrapped usage line aspython -m sphinx.flask. Pinning the detection on the script name keeps it identical whatever the launcher.
- tests.test_cli_wrapper.test_resolve_target_command_returns_command_and_context(greet_script)[source]¶
The shared resolver returns the target’s command object and a context.
- tests.test_cli_wrapper.test_resolve_target_command_drills_subcommand(custom_cls_script)[source]¶
Extra subcommands navigate into nested groups.
- tests.test_cli_wrapper.test_resolve_target_command_rejects_a_wrong_path(custom_cls_script, path, message)[source]¶
- tests.test_cli_wrapper.test_module_scan_finds_the_one_command(tmp_path, source, expected)[source]¶
A module without an entry point is scanned for its one command.
- tests.test_cli_wrapper.test_module_scan_refuses_an_ambiguous_module(tmp_path, source, message)[source]¶
- tests.test_cli_wrapper.test_params_follow_a_subcommands_own_context_settings(runner, tmp_path)[source]¶
A subcommand’s own
context_settingsshape the context it is described in.Click applies them when it invokes the subcommand, so the environment variable the parameter table names is the one the run reads.
- tests.test_cli_wrapper.test_wrapper_group_runs_a_lazy_subcommand(runner, tmp_path)[source]¶
A lazy subcommand not imported yet runs, rather than being wrapped.
- tests.test_cli_wrapper.test_params_follow_the_targets_own_context_settings(runner, tmp_path)[source]¶
Every
context_settingskey of the target reaches the parameter table.A target answering to
-has well as--helplists both spellings, as its own help screen does.
Leading words navigate, and everything from the first option is replayed.
- tests.test_cli_wrapper.test_wrap_man_reads_manual(runner, greet_script)[source]¶
click-extra wrap --man -- SCRIPTreads the target’s manual and exits.A CLI shipping no man page of its own still gets one to read, typeset from its own command tree.
- tests.test_cli_wrapper.test_wrap_help_format_man_emits_source(runner, greet_script)[source]¶
The roff a packager installs comes from the format, not from –man.
- tests.test_cli_wrapper.test_wrap_man_custom_class_group(runner, custom_cls_script)[source]¶
--manresolves a custom-class group target via the shared scanner.
- tests.test_cli_wrapper.test_wrap_man_drills_into_subcommand(runner, custom_cls_script)[source]¶
Extra arguments after SCRIPT render the nested subcommand’s page.
- tests.test_cli_wrapper.test_wrap_man_output_dir_writes_tree(runner, custom_cls_script, tmp_path)[source]¶
wrap --help-format man --output-dirwrites one .1 per (sub)command.--output-dirmust appear before SCRIPT, because wrap runs withallow_interspersed_args=Falseso that anything after SCRIPT is treated as a sub-command path rather than a click-extra flag.
- tests.test_cli_wrapper.test_wrap_man_output_dir_creates_missing_directory(runner, greet_script, tmp_path)[source]¶
--output-dircreates the target dir when it does not exist yet.
- tests.test_cli_wrapper.test_wrap_man_pages_reject_subcommand(runner, custom_cls_script, tmp_path, monkeypatch, destination)[source]¶
Writing man pages always emits the full tree; mixing in a SUBCOMMAND arg is rejected so the user cannot accidentally produce a tree of pages named after a partial path.
- tests.test_cli_wrapper.test_wrap_man_flag_refuses_a_destination(runner, greet_script, tmp_path)[source]¶
--manreads the manual: the roff to write somewhere is a format’s.
- tests.test_cli_wrapper.test_group_dispatches_to_wrap(runner, greet_script, args, expected)[source]¶
All invocation forms reach the target CLI.
- tests.test_cli_wrapper.test_group_options_work_with_wrap(runner, greet_script, group_opts)[source]¶
Default Group options are accepted alongside the wrap subcommand.
- tests.test_cli_wrapper.test_wrap_honors_group_theme(runner, greet_script, theme, styled_heading)[source]¶
A group-level
--themereaches the wrapped CLI’s help screen.ThemeOption records the pick on the shared context meta, but the wrapped target runs under its own fresh context; wrap must bridge the pick to the process default (through patch_click) for the target’s help to pick it up.
- tests.test_cli_wrapper.test_wrap_honors_theme_envvar(runner, greet_script, monkeypatch)[source]¶
CLICK_EXTRA_THEMEreaches a wrapped CLI’s help screen too.The group resolves the machine-wide variable like any other source of a palette, and wrap bridges whatever it settled on to the process default the wrapped target renders under.
- tests.test_cli_wrapper.test_wrap_pins_group_color_on_every_target_kind(runner, request, script_fixture, color_opt, colored)[source]¶
The group’s tri-state color decision reaches the wrapped target.
Both kinds of target must obey it: one built by the patched
@click.command()decorator (so it is a_HelpColorsMixin) and one carrying an explicitcls=(so it stays plain Click). They travel different code paths insidepatch_click, so each direction of the flag is checked on both.The runner is left on its own auto-detection here: forcing colors at the stream level would paint the output whatever the flag decided.
- tests.test_cli_wrapper.test_group_known_subcommands_not_wrapped(runner, subcommand)[source]¶
Known demo subcommands are dispatched directly, not to wrap.
- tests.test_cli_wrapper.test_cli_startup_leaves_the_test_tooling_alone()[source]¶
Loading the CLI imports neither the test-suite engine nor
click.testing.Only the
test-suitecommand needs them, andclick.testingdragspdbandasyncioin behind it: every other command would pay for them at startup.
- tests.test_cli_wrapper.test_unpatch_click_restores_the_default_theme(patch_themes)[source]¶
Unpatching hands back the process-wide theme patching replaced.
The default in place before the patch is not the built-in
darkone, so a restore to the built-in default fails here. A second patch must not save the theme of the first as the one to restore.
- tests.test_cli_wrapper.test_unpatch_click_keeps_a_default_theme_it_did_not_replace()[source]¶
A patch that sets no theme leaves the default theme alone on unpatching.
A default set while patched therefore outlives the patch.
- tests.test_cli_wrapper.test_config_verbosity(runner, greet_script, create_config)[source]¶
verbosity = "DEBUG"in pyproject.toml activates debug logging.
- tests.test_cli_wrapper.test_config_group_theme(runner, greet_script, create_config)[source]¶
A
[tool.click-extra]themekey sets the help-screen theme.
- tests.test_cli_wrapper.test_config_target_string(runner, greet_script, create_config)[source]¶
A string config value is forwarded as
--key value.
- tests.test_cli_wrapper.test_config_target_bool_true(runner, weather_script, create_config)[source]¶
A
trueconfig value is forwarded as--flag.
- tests.test_cli_wrapper.test_config_target_bool_false_is_noop(runner, weather_script, create_config)[source]¶
A
falseconfig value is skipped: the flag is simply not passed.
- tests.test_cli_wrapper.test_config_target_multiple_keys(runner, weather_script, create_config)[source]¶
Multiple config keys are all forwarded.
- tests.test_cli_wrapper.test_config_target_cli_overrides(runner, greet_script, create_config)[source]¶
Explicit CLI args override config target defaults.
- tests.test_cli_wrapper.test_config_target_wrong_section_ignored(runner, greet_script, create_config)[source]¶
Config for a different script name has no effect.
- tests.test_cli_wrapper.test_config_target_empty_section(runner, greet_script, create_config)[source]¶
An empty target section produces no extra args.
- tests.test_cli_wrapper.test_config_target_no_config(runner, greet_script)[source]¶
No config file at all: target runs with its own defaults.
- tests.test_cli_wrapper.test_config_target_invalid_option(runner, greet_script, create_config)[source]¶
An invalid config key is caught by the target CLI.
- tests.test_cli_wrapper.test_config_args_no_wrap_section()[source]¶
Config exists but has no wrap section.
- tests.test_cli_wrapper.test_wrap_help_format_describes_a_foreign_cli(runner, greet_script, help_format, expected)[source]¶
A CLI that never heard of Click Extra is rendered from the outside.
Same posture as
--paramsand--man: the target is loaded and walked, never asked to cooperate, so machine-readable help needs no opt-in from the author of the wrapped CLI.
- tests.test_cli_wrapper.test_wrap_help_format_json_is_parseable(runner, greet_script)[source]¶
The JSON rendering of a foreign CLI parses, and carries its options.
- tests.test_cli_wrapper.test_wrap_help_format_stays_colorless(runner, greet_script)[source]¶
Machine-readable output carries no ANSI, whatever the group asked for.
Every format here is meant to be piped into a parser, which has no use for escape codes:
--color=alwayspaints the help screen, not the export.
- tests.test_cli_wrapper.test_wrap_install_writes_where_the_consumer_looks(runner, greet_script, tmp_path, monkeypatch, help_format, artifact)[source]¶
--installputs each rendering where the tool reading it looks.One destination flag across both formats, each module resolving its own canonical directory: Carapace’s spec directory, and the user’s man directory. Both honor their XDG variable at call time.
- tests.test_cli_wrapper.test_wrap_destination_refused_for_documents(runner, greet_script, help_format)[source]¶
A document has nowhere canonical to be installed, and says so.
- tests.test_cli_wrapper.test_target_prog_name_is_what_a_user_would_type(script, expected)[source]¶
Every rendering is titled with the name the target runs under.
- tests.test_cli_wrapper.test_every_rendering_agrees_on_the_target_name(runner, tmp_path)[source]¶
One rule, so the man page, spec, Markdown and tree cannot disagree.
The script file is named after neither its command object nor the directory holding it, so a renderer picking the wrong source shows it.