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_positional(invoke)[source]ΒΆ
Click drop-in: an explicit version may be the first positional argument.
@version_option("1.2.3.4")is equivalent to thefields={"version": "1.2.3.4"}form used bytest_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_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_debug_version_run_resolves_no_host_name(invoke, monkeypatch)[source]ΒΆ
A
--verbosity DEBUGrun 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.metamust not evaluate unrelated fields.Ensures that the
_LazyVersionDictdefers property evaluation: readingclick_extra.versionshould not trigger expensive properties like the git fields.
A subcommand with its own
--versionsharesctx.metawith 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_versionfalls back to parent packageβs__version__.Simulates the Nuitka use-case: a CLI whose module is
myapp.__main__(no__version__), with the parent packagemyappproviding it.
- 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()answersNonefor the CLIβs own package just as it does for ecosystem plumbing, and the CLI module resolves toclick_extra.__main__. That entry point must survive: traded for the root commandβs callback module, it loses the__main__exemption and--versionrenders nothing at all.
- tests.test_version.test_prebaked_fields_read_from_the_package(monkeypatch, module_name)[source]ΒΆ
A field baked into the package
__init__.pyreaches the CLI module beside it.click-extra prebake allwrites its dunders into the__init__.pyof 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
CliRunnernever 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 orchardruns the entry point under the__main__name, while a console script or a compiled binary imports it asorchard.__main__.
- tests.test_version.test_package_version_resolves_import_name_to_distribution(monkeypatch)[source]ΒΆ
When
package_nameis an import name that differs from its installed distribution name (PILvsPillow),package_versionresolves it viapackages_distributions()instead of returningNone.
- tests.test_version.test_package_version_ambiguous_import_name_returns_none(monkeypatch)[source]ΒΆ
When an import name maps to several installed distributions,
package_versionreturnsNonerather than guessing.
- tests.test_version.test_package_version_unknown_returns_none(monkeypatch)[source]ΒΆ
When the name resolves to no distribution,
package_versionreturnsNone.
- 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_namefalls back to the scriptβs file name andpackage_nametoNone. The version is read from a__version__variable defined alongside the CLI, and staysNonewhen 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 -cresolves 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_namekeeps 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 -mprints.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__. Anatexithook 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 themain()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 importedclick.testinghas no such runner to check against: a test process always has, so only a new interpreter can tell.
- tests.test_version.test_version_fields_forwarded_to_version_option(invoke)[source]ΒΆ
version_fieldson@commandforwards toVersionOption.
- tests.test_version.test_version_fields_forwarded_on_group(invoke)[source]ΒΆ
version_fieldsworks on@grouptoo.
- 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.commandonly decolors--versionwhen--no-colorprecedes it on the command line.Click evaluates eager parameters in the order the user typed them (callback evaluation order), so a
--no-colorplaced after--versionlands too late: the version screen has already rendered in color and exited.click-extraβs own
@commandsettles the color options in a pre-pass before any eager screen renders, so the color choice is honored whatever its position. SeeCommand._resolve_presentation_eagerlyand the order-independenttest_color_settles_before_eager_help_and_versionintest_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
.devversion gets a+hashsuffix 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 prebakeresolves its target from[tool.click-extra.prebake].
- tests.test_version.test_prebake_dev_version(init_file)[source]ΒΆ
A
.devversion gets+hashappended 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__returnsNone.
- tests.test_version.test_prebake_missing_local_version_raises(init_file)[source]ΒΆ
Calling without
local_versionraisesTypeError.
- 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_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
.gitentry, a file included.
- 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_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
apricotdistribution, with adirect_url.jsonholding direct_url unless it isNone.- Return type:
- 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.rendermeasures itself against.The function is patched, and
COLUMNSdeliberately left alone.shutil.get_terminal_sizereads 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 raisesTypeErrorthere and aborts the run with anINTERNALERROR. See #1934.
- tests.test_version.PLAIN_LOGO = ('###', '#', '#####')ΒΆ
A deliberately ragged mark: no caller should have to square its own artwork.
- tests.test_version.STYLED_LOGO = ('\x1b[32m###\x1b[0m', '\x1b[31m#\x1b[0m')ΒΆ
The same, carrying color, to prove the measurement discounts escape sequences.
- tests.test_version.test_screen_measures_its_logo(logo, width)[source]ΒΆ
The width comes from the artwork, never from the caller.
- tests.test_version.test_screen_squares_a_ragged_logo(logo)[source]ΒΆ
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.ljustcounts 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
--versionis 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_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.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_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]ΒΆ
autoreadssys.stdout, an explicit tri-state never looks at it.The stream half is the part worth pinning:
click.echoreaches stdout through a wrapper whose public accessor Click deprecates, and this resolution has to answer whatechoanswers without it.
- tests.test_version.test_sphinx_runner_resets_version_resolution()[source]ΒΆ
Each documented invocation resolves
--versionfor 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
--versionexample 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-buildhas, so it holdsClickRunner.invoketo 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
Noneon every documented invocation.