tests.test_version moduleΒΆ

Test the --version option.

tests.test_version.test_standalone_version_option(invoke, cmd_decorator, option_decorator)[source]ΒΆ
tests.test_version.test_debug_output(invoke, cmd_decorator, option_decorator, assert_output_regex)[source]ΒΆ
tests.test_version.test_set_version(invoke)[source]ΒΆ
tests.test_version.test_set_version_positional(invoke)[source]ΒΆ

Click drop-in: an explicit version may be the first positional argument.

@version_option("1.2.3.4") is equivalent to the fields={"version": "1.2.3.4"} form used by test_set_version().

tests.test_version.test_version_option_dashed_positional_is_flag()[source]ΒΆ

A leading-dash positional names the option flag, not the version.

tests.test_version.test_version_option_conflicting_version()[source]ΒΆ

The version cannot be supplied both positionally and via fields=.

tests.test_version.test_custom_message(invoke, cmd_decorator, message, regex_stdout, assert_output_regex)[source]ΒΆ
tests.test_version.test_style_reset(invoke, cmd_decorator)[source]ΒΆ
tests.test_version.test_custom_message_style(invoke, cmd_decorator)[source]ΒΆ
tests.test_version.test_build_resolver_answers_and_is_a_template_field(field_id)[source]ΒΆ

Every build resolver answers on the host running it, and has a field.

A build fact is unlike a git one on both counts: nothing can fail to resolve, since the host is right here, and nothing can fall back later, so a resolver whose field is missing from the template would bake a value no message could ever print.

tests.test_version.test_build_time_honors_source_date_epoch(monkeypatch)[source]ΒΆ

A reproducible build pins the stamp instead of reading the clock.

tests.test_version.test_context_meta(invoke, cmd_decorator, assert_output_regex)[source]ΒΆ
tests.test_version.test_debug_version_run_resolves_no_host_name(invoke, monkeypatch)[source]ΒΆ

A --verbosity DEBUG run lists every version field without a DNS query.

A reverse DNS query blocks for as long as the resolver takes to answer, which has no bound on a host whose resolver never does.

Asserting on the calls rather than on a duration keeps the guard away from a timing threshold, which a loaded runner would flake on.

tests.test_version.test_debug_listing_reaches_a_level_below_debug(invoke, caplog)[source]ΒΆ

A logger set below DEBUG, like a custom trace level, lists the fields too.

tests.test_version.test_context_meta_laziness(invoke, cmd_decorator)[source]ΒΆ

Accessing a single field from ctx.meta must not evaluate unrelated fields.

Ensures that the _LazyVersionDict defers property evaluation: reading click_extra.version should not trigger expensive properties like the git fields.

tests.test_version.test_subcommand_version_shares_meta(invoke, group_version)[source]ΒΆ

A subcommand with its own --version shares ctx.meta with its group.

The group reads what the callback of the subcommand stores there, and the subcommand reads the version it declares.

tests.test_version.test_module_version_parent_package_fallback(monkeypatch)[source]ΒΆ

module_version falls back to parent package’s __version__.

Simulates the Nuitka use-case: a CLI whose module is myapp.__main__ (no __version__), with the parent package myapp providing it.

tests.test_version.test_is_main_module(module_name, expected)[source]ΒΆ
tests.test_version.test_main_entry_point_survives_absent_distribution_metadata(monkeypatch)[source]ΒΆ

A compiled binary reads its version off the package it started in.

A Nuitka standalone binary ships no distribution metadata, so distribution_of() answers None for the CLI’s own package just as it does for ecosystem plumbing, and the CLI module resolves to click_extra.__main__. That entry point must survive: traded for the root command’s callback module, it loses the __main__ exemption and --version renders nothing at all.

tests.test_version.test_prebaked_fields_read_from_the_package(monkeypatch, module_name)[source]ΒΆ

A field baked into the package __init__.py reaches the CLI module beside it.

click-extra prebake all writes its dunders into the __init__.py of each package a console script enters, while the CLI sits in a sibling module.

tests.test_version.test_prebaked_fields_skip_the_package_of_ecosystem_plumbing(monkeypatch)[source]ΒΆ

A walk ending on a CliRunner never reads Click Extra’s own baked fields.

tests.test_version.test_exec_name(module_name, package, expected)[source]ΒΆ

A __main__ entry point is named after its package, however it started.

python -m orchard runs the entry point under the __main__ name, while a console script or a compiled binary imports it as orchard.__main__.

tests.test_version.test_package_version_resolves_import_name_to_distribution(monkeypatch)[source]ΒΆ

When package_name is an import name that differs from its installed distribution name (PIL vs Pillow), package_version resolves it via packages_distributions() instead of returning None.

tests.test_version.test_package_version_ambiguous_import_name_returns_none(monkeypatch)[source]ΒΆ

When an import name maps to several installed distributions, package_version returns None rather than guessing.

tests.test_version.test_package_version_unknown_returns_none(monkeypatch)[source]ΒΆ

When the name resolves to no distribution, package_version returns None.

tests.test_version.test_cli_frame_fallback()[source]ΒΆ

cli_frame() falls back to the outermost frame when all frames are from the Click ecosystem.

tests.test_version.STANDALONE_SCRIPT = 'import click\nfrom click_extra import echo, version_option\n\n\n@click.command\n@version_option(\n    message=(\n        "{prog_name} | {exec_name} | {package_name}"\n        " | {module_version} | {version}"\n    )\n)\ndef weather():\n    echo("Sunny.")\n\n\nif __name__ == "__main__":\n    weather()\n'ΒΆ

A CLI run straight from a file as __main__, with no package around it.

Its version message renders every field the unpackaged case resolves on its own, so a single invocation pins them all.

tests.test_version.test_standalone_script(tmp_path, dunder, expected_version)[source]ΒΆ

A standalone script is named after its file, and versioned by __version__.

With no package to read metadata from, exec_name falls back to the script’s file name and package_name to None. The version is read from a __version__ variable defined alongside the CLI, and stays None when the script defines none.

Only a real interpreter reaches that code path: a CLI declared inside a test function belongs to the test module, and that module has a package.

tests.test_version.COMMAND_STRING_CLI = 'from click_extra import command, echo\n\n__version__ = "1.2.3"\n\n\n@command\ndef weather():\n    echo("Sunny.")\n\n\nweather()\n'ΒΆ

A Click Extra CLI for python -c, which runs it with no file behind it.

tests.test_version.test_command_string_cli(tmp_path, args, expected)[source]ΒΆ

A CLI run by python -c resolves its version fields without a file.

The version option of a Click Extra command looks for the CLI’s module on every run, not only on --version. The debug level renders every field, including the ones read off the module’s file: exec_name keeps the __main__ module name.

tests.test_version.NUITKA_RUNNER = 'import sys\nimport {module} as user_main\nsys.exit(user_main.{function}())\n'ΒΆ

What Nuitka compiles for --main-entry-point={{name}}={{module}}:{{function}}.

A __main__ with no package and no __version__, which binds the entry point module and calls its function: a compiled binary starts here.

tests.test_version.test_nuitka_runner_resolves_the_entry_point_version(tmp_path, module, function)[source]ΒΆ

The runner of a compiled binary prints the version python -m prints.

An entry point function stops the stack walk in its own module. A command object leaves no frame of its module on the stack, and the walk ends on the runner: the module of the command’s callback then names the CLI.

tests.test_version.test_package_version_without_a_cli_frame(tmp_path, runner_source)[source]ΒΆ

A CLI reads its package version when no frame of its module is on the stack.

No distribution provides the package, as in a compiled binary, so the version can only come from the __version__ of its __init__.py. The runner Nuitka compiles calls the command object from a packageless __main__. An atexit hook calls it once the main module has left the stack, like the target of _thread.start_new_thread() or a call from a C host: the walk then sees only the Click ecosystem, and falls back to the main() of the command. A thread and an executor call it from the standard library, where the walk stops.

tests.test_version.SUBCOMMAND_VERSION_CLI = 'from click_extra import echo, group, version_option\n\n\n@group\ndef orchard():\n    pass\n\n\n@orchard.command()\n@version_option(fields={"version": "1.2.3"})\ndef harvest():\n    echo("Apples picked.")\n\n\norchard()\n'ΒΆ

A group whose subcommand carries a version option of its own.

tests.test_version.test_subcommand_version_option_outside_a_test_runner(tmp_path, args, expected)[source]ΒΆ

The version option of a subcommand resolves where no test runner is loaded.

The stack walk of a subcommand crosses the invoke() of its group, which it checks against the test runner of Click. A process that never imported click.testing has no such runner to check against: a test process always has, so only a new interpreter can tell.

tests.test_version.test_integrated_version_option_precedence(invoke, params)[source]ΒΆ
tests.test_version.test_version_fields_forwarded_to_version_option(invoke)[source]ΒΆ

version_fields on @command forwards to VersionOption.

tests.test_version.test_version_fields_forwarded_on_group(invoke)[source]ΒΆ

version_fields works on @group too.

tests.test_version.test_version_fields_multiple(invoke)[source]ΒΆ

Multiple fields can be overridden at once.

tests.test_version.test_version_fields_rejects_unknown(invoke)[source]ΒΆ

Unknown field names raise TypeError.

tests.test_version.test_unknown_field_is_rejected_the_same_way_everywhere(build)[source]ΒΆ

Every way to name a template field rejects an unknown one alike.

tests.test_version.test_color_option_precedence(invoke)[source]ΒΆ

A plain click.command only decolors --version when --no-color precedes it on the command line.

Click evaluates eager parameters in the order the user typed them (callback evaluation order), so a --no-color placed after --version lands too late: the version screen has already rendered in color and exited.

click-extra’s own @command settles the color options in a pre-pass before any eager screen renders, so the color choice is honored whatever its position. See Command._resolve_presentation_eagerly and the order-independent test_color_settles_before_eager_help_and_version in test_color.py. This test pins the residual behavior of the plain-Click path, which that pre-pass does not reach.

tests.test_version.test_dev_version_appends_git_hash(invoke, cmd_decorator)[source]ΒΆ

A .dev version gets a +hash suffix appended (or not, if git is unavailable).

tests.test_version.test_prebaked_dev_version_not_double_suffixed(invoke, cmd_decorator)[source]ΒΆ

A version with an existing + is returned as-is: no second hash appended.

tests.test_version.test_release_version_unchanged(invoke, cmd_decorator)[source]ΒΆ

A non-dev version is never modified.

tests.test_version.init_file(tmp_path)[source]ΒΆ

Helper that creates a temporary __init__.py with the given content.

tests.test_version.test_prebake_cli_resolves_module_from_config(invoke, tmp_path, monkeypatch)[source]ΒΆ

click-extra prebake resolves its target from [tool.click-extra.prebake].

tests.test_version.test_prebake_dev_version(init_file)[source]ΒΆ

A .dev version gets +hash appended in the file.

tests.test_version.test_prebake_single_quotes(init_file)[source]ΒΆ

Single-quoted __version__ is also handled.

tests.test_version.test_prebake_already_baked_skipped(init_file)[source]ΒΆ

A version with existing + is left untouched.

tests.test_version.test_prebake_release_skipped(init_file)[source]ΒΆ

A release version (no .dev) is not modified.

tests.test_version.test_prebake_no_version_in_file(init_file)[source]ΒΆ

A file without __version__ returns None.

tests.test_version.test_prebake_missing_local_version_raises(init_file)[source]ΒΆ

Calling without local_version raises TypeError.

tests.test_version.test_prebake_idempotent(init_file)[source]ΒΆ

Running prebake twice does not double-suffix.

tests.test_version.test_prebake_preserves_surrounding_content(init_file)[source]ΒΆ

Content around __version__ is not disturbed.

tests.test_version.test_prebake_dunder_empty_replaced(init_file)[source]ΒΆ

An empty dunder variable gets replaced.

tests.test_version.test_prebake_dunder_single_quotes(init_file)[source]ΒΆ

Single-quoted empty dunder is also handled.

tests.test_version.test_prebake_dunder_nonempty_skipped(init_file)[source]ΒΆ

A dunder with an existing non-empty value is left untouched.

tests.test_version.test_prebake_dunder_not_found(init_file)[source]ΒΆ

A file without the target dunder returns None.

tests.test_version.test_prebake_dunder_idempotent(init_file)[source]ΒΆ

Running prebake_dunder twice does not overwrite.

tests.test_version.test_prebake_dunder_preserves_surrounding_content(init_file)[source]ΒΆ

Content around the target dunder is not disturbed.

tests.test_version.test_prebake_dunder_full_sha(init_file)[source]ΒΆ

A full 40-character SHA is handled correctly.

tests.test_version.test_prebaked_git_branch()[source]ΒΆ

A pre-baked __git_branch__ dunder is used over subprocess.

tests.test_version.test_prebaked_git_long_hash()[source]ΒΆ

A pre-baked __git_long_hash__ dunder is used over subprocess.

tests.test_version.test_prebaked_git_tag_sha()[source]ΒΆ

A pre-baked __git_tag_sha__ dunder is resolved.

tests.test_version.test_prebaked_empty_dunder_ignored()[source]ΒΆ

An empty dunder is not treated as a pre-baked value.

tests.test_version.test_prebaked_non_string_ignored()[source]ΒΆ

A non-string dunder is not treated as a pre-baked value.

tests.test_version.test_prebaked_git_distance()[source]ΒΆ

A pre-baked __git_distance__ dunder is used over subprocess.

tests.test_version.test_prebaked_git_dirty()[source]ΒΆ

A pre-baked __git_dirty__ dunder is used over subprocess.

tests.test_version.test_resolve_git_distance(monkeypatch, describe_output, expected)[source]ΒΆ
tests.test_version.test_resolve_git_dirty(monkeypatch, status_output, expected)[source]ΒΆ
tests.test_version.test_run_git_decodes_a_branch_name(tmp_path, branch, expected)[source]ΒΆ

A branch name decodes as UTF-8, and a byte that is not becomes a replacement character.

The branch goes in packed-refs: a loose ref is a file, and APFS refuses a file name that is no UTF-8.

tests.test_version.test_run_git_returns_nothing_or_raises_on_failure(tmp_path, monkeypatch)[source]ΒΆ

A failing command returns None, or raises when asked to.

tests.test_version.test_run_git_gives_up_after_its_timeout(tmp_path, monkeypatch)[source]ΒΆ

A command running past its timeout returns None, or raises when asked.

tests.test_version.test_run_git_keeps_an_empty_last_field(tmp_path)[source]ΒΆ

Only the final newline goes: the empty field ending a line stays.

A lightweight tag points at no tag object, so the date read through one is empty, which ends the line on a tab.

tests.test_version.test_find_git_root(tmp_path)[source]ΒΆ

The walk stops at the nearest .git entry, a file included.

tests.test_version.test_archival_field_substituted(field_id, expected)[source]ΒΆ
tests.test_version.test_archival_field_unsubstituted_ignored(field_id)[source]ΒΆ

A raw checkout keeps the $Format placeholders, which must be ignored.

tests.test_version.test_archival_field_exact_tag_distance_zero()[source]ΒΆ

A bare describe-name (no -N-g suffix) means distance zero.

tests.test_version.test_read_archival_roundtrip(tmp_path)[source]ΒΆ
tests.test_version.test_read_archival_invalid_json(tmp_path)[source]ΒΆ
tests.test_version.test_find_archival_file_walks_up(tmp_path)[source]ΒΆ
tests.test_version.test_find_archival_file_absent(tmp_path)[source]ΒΆ
tests.test_version.test_archival_resolves_git_fields(tmp_path)[source]ΒΆ

Git fields resolve from .git_archival.json when there is no live git.

tests.test_version.install_distribution(tmp_path, monkeypatch, direct_url)[source]ΒΆ

Install a fake apricot distribution, with a direct_url.json holding direct_url unless it is None.

Return type:

None

tests.test_version.test_direct_url_field(data, field_id, expected)[source]ΒΆ
tests.test_version.test_read_direct_url(tmp_path, monkeypatch, direct_url, expected)[source]ΒΆ
tests.test_version.test_read_direct_url_unknown_distribution(distribution)[source]ΒΆ
tests.test_version.test_direct_url_resolves_git_hashes(tmp_path, monkeypatch)[source]ΒΆ

A CLI installed from a Git URL names its commit, with no live git and no archive.

tests.test_version.test_version_dev_hash_assembly(module_version, expected)[source]ΒΆ

The git short hash is appended to dev releases only.

tests.test_version.terminal_width(monkeypatch)[source]ΒΆ

Pin the width VersionScreen.render measures itself against.

The function is patched, and COLUMNS deliberately left alone. shutil.get_terminal_size reads that variable before it measures the terminal, so exporting it would pin the width just as well. It would also resize pytest’s own reports for as long as a narrow pin is held.

The replacement accepts keyword arguments because pytest measures its progress line with shutil.get_terminal_size(fallback=(80, 24)). A positional-only stand-in raises TypeError there and aborts the run with an INTERNALERROR. See #1934.

A deliberately ragged mark: no caller should have to square its own artwork.

The same, carrying color, to prove the measurement discounts escape sequences.

The width comes from the artwork, never from the caller.

Every line is padded out to the widest, so the facts seat against a block.

Padding here rather than demanding it is what lets a caller hand over whatever its renderer produced. It cannot repair this afterwards even if it wanted to: str.ljust counts the escape sequences it cannot see, so on a styled line it silently does nothing.

tests.test_version.test_screen_padding_closes_an_open_style()[source]ΒΆ

Padding after an unterminated style cannot inherit it.

A mark whose last run is left open would otherwise paint its own trailing blanks, and the gutter with them, dragging a background across the facts.

tests.test_version.test_screen_facts_may_be_deferred()[source]ΒΆ

A callable is not called until the screen is drawn.

The point of accepting one: a CLI reporting something costly should pay for it when --version is asked for, not on every invocation that might have been.

tests.test_version.test_screen_draws_a_callable_logo_once_per_render(terminal_width)[source]ΒΆ

Measuring and padding the mark share one drawing of it.

tests.test_version.test_screen_sizes_its_label_column_to_the_longest_label()[source]ΒΆ

No declared width to outgrow: the column is measured, then gutter-separated.

tests.test_version.test_screen_facts_replace_a_default_row_in_place()[source]ΒΆ

| over the defaults swaps a row where it sits, and appends a new one.

tests.test_version.test_env_summary_joins_the_default_facts()[source]ΒΆ

The one-line summary says what the version screen’s default rows say.

tests.test_version.test_screen_declines_when_too_narrow(terminal_width)[source]ΒΆ
tests.test_version.test_screen_opens_on_a_blank_line_and_never_trails_whitespace(terminal_width)[source]ΒΆ
tests.test_version.test_screen_seats_every_fact_at_one_column(terminal_width)[source]ΒΆ

The facts start at the same column on every line that carries one.

tests.test_version.test_screen_uses_the_options_own_styles(terminal_width)[source]ΒΆ

The header is painted like the plain message, not like something new.

tests.test_version.screen_cli()[source]ΒΆ
tests.test_version.test_version_option_without_a_screen_is_unchanged(invoke)[source]ΒΆ

A CLI that never asks for a screen keeps the one-line message.

tests.test_version.test_screen_skipped_without_color(invoke, screen_cli)[source]ΒΆ
tests.test_version.test_screen_drawn_with_color(invoke, screen_cli, terminal_width)[source]ΒΆ
tests.test_version.test_screen_skipped_when_accessible(invoke, screen_cli, terminal_width)[source]ΒΆ
tests.test_version.test_version_message_follows_the_theme(invoke, theme, expected)[source]ΒΆ

The message is painted from the active palette, not from a frozen copy.

A palette captured at import would print the program name bright white under every theme, which on a light terminal is white on white.

tests.test_version.test_colors_reach_output(monkeypatch, color, stdout_is_a_tty, expected)[source]ΒΆ

auto reads sys.stdout, an explicit tri-state never looks at it.

The stream half is the part worth pinning: click.echo reaches stdout through a wrapper whose public accessor Click deprecates, and this resolution has to answer what echo answers without it.

tests.test_version.test_sphinx_runner_resets_version_resolution()[source]ΒΆ

Each documented invocation resolves --version for itself.

The resolution chain opens on a stack walk and memoizes, which suits a CLI: one invocation, one process. A documentation build renders many commands in one process, and a render carrying no CLI frame (writing roff for a command tree, say) resolves the chain from a stack the walk cannot read. That answer would stand for the rest of the build, and a later --version example would publish a screen with no version on it.

This guards the mechanism, not the build: it plants a stale resolution by hand rather than reproducing the frame shape a real sphinx-build has, so it holds ClickRunner.invoke to clearing the memo and says nothing about which renders would otherwise poison it.

tests.test_version.test_reset_resolution_clears_every_memoized_field()[source]ΒΆ

reset_resolution() drops the whole chain, not a chosen few fields.

tests.test_version.test_reset_resolution_keeps_a_pinned_field()[source]ΒΆ

A field pinned through fields= is not a resolution, so it survives.

An override lands in the instance dict under the name of the property it shadows, so a reset that goes by name alone erases it. That silently turns a pinned version into None on every documented invocation.

tests.test_version.test_deep_copy_keeps_pinned_fields_and_drops_resolved_ones()[source]ΒΆ

A copy carries the fields the caller pinned, and resolves the others anew.

click_extra.multicall deep-copies the options of a group to build each personality, which must report the version the group pins.