tests.test_context moduleΒΆ
Tests for click_extra.context, and the context class that carries it.
Covers four surfaces:
The registry of
ctx.metakey constants.Context, Click Extraβscloup.Contextsubclass, defined inclick_extra.commands._LazyMetaDict, the lazyctx._metaproxy used byVersionOption.
- tests.test_context.KEY_CONSTANTS: tuple[tuple[str, str], ...] = (('RAW_ARGS', 'click_extra.raw_args'), ('INVOCATION_NAME', 'click_extra.invocation_name'), ('CONF_SOURCE', 'click_extra.conf_source'), ('CONF_FULL', 'click_extra.conf_full'), ('CONF_SOURCES', 'click_extra.conf_sources'), ('TOOL_CONFIG', 'click_extra.tool_config'), ('SUBCOMMANDS_APPLIED', 'click_extra.subcommands_applied'), ('VERBOSITY_LEVEL', 'click_extra.verbosity_level'), ('VERBOSITY', 'click_extra.verbosity'), ('DEBUG', 'click_extra.debug'), ('VERBOSE', 'click_extra.verbose'), ('QUIET', 'click_extra.quiet'), ('START_TIME', 'click_extra.start_time'), ('JOBS', 'click_extra.jobs'), ('TABLE_FORMAT', 'click_extra.table_format'), ('SORT_BY', 'click_extra.sort_by'), ('TABLE_SORT_KEY', 'click_extra.table_sort_key'), ('COLUMNS', 'click_extra.columns'), ('THEME', 'click_extra.theme.active'), ('THEME_OVERRIDES', 'click_extra.theme.overrides'), ('TELEMETRY', 'click_extra.telemetry'), ('PROGRESS', 'click_extra.progress'), ('ACCESSIBLE', 'click_extra.accessible'), ('ZERO_EXIT', 'click_extra.zero_exit'))ΒΆ
Pairs of
(attribute name, raw string key)for every registeredctx.metaentry. Single source of truth used by every parametrized registry test below.
- tests.test_context.test_key_constant_value(attr, expected)[source]ΒΆ
Each registered constant binds to its documented string value.
Pins the spelling of every key so a rename of the literal does not silently break downstream code that reads
ctx.meta["click_extra.X"].- Return type:
- tests.test_context.test_key_uses_namespace_prefix(key)[source]ΒΆ
Every registered key sits under
META_NAMESPACE.- Return type:
- tests.test_context.test_keys_are_unique()[source]ΒΆ
No two registry constants share a string value.
- Return type:
- tests.test_context.test_registry_covers_all_module_constants()[source]ΒΆ
KEY_CONSTANTSlists every public namespace-prefixed constant.Drift detector: when a new
ctx.metakey is added toclick_extra.contextbut not toKEY_CONSTANTS, this test fails so the maintainer remembers to update both sides.- Return type:
- tests.test_context.NON_META_KEYS = frozenset({'click_extra.config_docstring_heading', 'click_extra.config_path', 'click_extra.extension', 'click_extra.normalize_keys', 'click_extra.tabs_vertical', 'click_extra.todo_unrendered'})ΒΆ
Literals sharing
META_NAMESPACEthat name noctx.metakey.
- tests.test_context.test_meta_keys_are_spelled_in_context_only()[source]ΒΆ
No module but
click_extra.contextspells actx.metakey.Every other module reads its keys from there: none builds one from
META_NAMESPACE, or writes one as a literal.- Return type:
- tests.test_context.test_internal_keys_are_private_and_unique()[source]ΒΆ
The keys only Click Extra reads sit under
click_extra._, apart from the public ones.- Return type:
- tests.test_context.test_get_returns_default_for_missing_key()[source]ΒΆ
get()mirrorsdict.getsemantics.- Return type:
- tests.test_context.test_set_then_get_round_trip()[source]ΒΆ
set()is observable throughget().- Return type:
- tests.test_context.test_setdefault_stores_the_factory_value_once()[source]ΒΆ
setdefault()creates a missing value, then keeps it.- Return type:
- tests.test_context.test_context_uses_help_formatter()[source]ΒΆ
Contextinstalls Click Extraβs colorized formatter.- Return type:
- tests.test_context.test_context_meta_kwarg_seeds_ctx_meta()[source]ΒΆ
The
meta=kwarg populatesctx.metaat construction time.- Return type:
- tests.test_context.test_context_meta_kwarg_omitted_leaves_meta_empty()[source]ΒΆ
Without
meta=,ctx.metais an empty dict, notNone.- Return type:
- tests.test_context.test_context_color(monkeypatch, parent_color, child_color, expected)[source]ΒΆ
Contextcolor resolution covers every parent/child path.Root contexts without an explicit
color=resolve the GNU auto default: with no color environment variable they stay atNone(TTY detection). Child contexts inherit the parentβs color unless they override it explicitly.- Return type:
- tests.test_context.test_context_posixly_correct(monkeypatch, env_present, expected)[source]ΒΆ
POSIXLY_CORRECTflipsallow_interspersed_argsoff when present.A plain
click.Commanddefaults the flag toTrue, so the unset case leaves interspersing enabled and the set case disables it.- Return type:
- tests.test_context.test_posixly_correct_presence_overrides_explicit_true(monkeypatch)[source]ΒΆ
An empty
POSIXLY_CORRECTstill wins over an explicitTrue.Presence alone triggers POSIX parsing (matching GNU getopt), regardless of value, and it takes precedence over a developer-supplied
allow_interspersed_args=True.- Return type:
- tests.test_context.test_posixly_correct_stops_option_parsing_at_first_argument()[source]ΒΆ
End-to-end: parsing stops at the first positional under POSIXLY_CORRECT.
Without the variable, the option interleaves with arguments (GNU style). With it set, the first positional ends option parsing, so the option keeps its default and the remaining tokens fall into the variadic argument.
- Return type:
- tests.test_context.test_lazy_meta_dict_resolves_on_first_access()[source]ΒΆ
Reading a lazy key triggers exactly one source attribute access.
- Return type:
- tests.test_context.test_lazy_meta_dict_caches_after_resolution()[source]ΒΆ
Subsequent reads of the same lazy key do not re-hit the source.
- Return type:
- tests.test_context.test_lazy_meta_dict_get_resolves_lazy_key()[source]ΒΆ
.get(lazy_key)triggers resolution like__getitem__.- Return type:
- tests.test_context.test_lazy_meta_dict_get_returns_default_for_unknown_key()[source]ΒΆ
.get(unknown, default)returns the default without touching source.- Return type:
- tests.test_context.test_lazy_meta_dict_contains_includes_lazy_keys_without_resolving()[source]ΒΆ
inreturnsTruefor declared lazy keys before any resolution.- Return type:
- tests.test_context.test_lazy_meta_dict_preserves_base_entries()[source]ΒΆ
Entries from the wrapped base dict remain accessible.
- Return type:
- tests.test_context.test_lazy_meta_dict_independent_keys_resolve_independently()[source]ΒΆ
Resolving one lazy key does not resolve sibling lazy keys.
- Return type:
- tests.test_context.LAZY_ENTRIES = {'click_extra.alpha': 'alpha-value', 'click_extra.beta': 'beta-value', 'existing.key': 'existing-value'}ΒΆ
Every entry of the lazy dicts below once resolved, in iteration order.
- tests.test_context.test_lazy_meta_dict_lists_pending_keys_without_resolving()[source]ΒΆ
len(), iteration andkeys()count the keysinclaims.Listing a key does not read its value, so nothing resolves.
- Return type:
- tests.test_context.test_lazy_meta_dict_reads_pending_values(read)[source]ΒΆ
Whatever reads every value resolves the pending keys, and sees them all.
- Return type:
- tests.test_context.test_lazy_meta_dict_equality_reads_pending_values()[source]ΒΆ
Equality compares every entry, pending ones included, on both sides.
- Return type:
- tests.test_context.test_lazy_meta_dict_copy_keeps_pending_keys_pending()[source]ΒΆ
A copy lists the pending keys, and resolves them only when read.
- Return type:
- tests.test_context.test_lazy_meta_dict_removed_key_stays_removed(remove, removed)[source]ΒΆ
A pending key removed from the mapping does not come back.
- Return type:
- tests.test_context.test_lazy_meta_dict_pop_returns_pending_values()[source]ΒΆ
pop()andpopitem()resolve the pending key they remove.- Return type:
- tests.test_context.test_lazy_meta_dict_setdefault_resolves_pending_key()[source]ΒΆ
setdefault()keeps the value of a pending key rather than the default.- Return type:
- tests.test_context.test_lazy_meta_dict_wraps_another_without_resolving()[source]ΒΆ
Wrapping a lazy dict keeps its pending keys pending.
The new dict copies only the entries the wrapped one stores.
- Return type:
- tests.test_context.test_lazy_meta_dict_added_source_takes_over_its_fields()[source]ΒΆ
A source added later resolves the fields it declares, resolved ones too.
A copy keeps the source of each pending key.
- Return type:
- tests.test_context.test_pass_context_typed_for_enhanced_context(invoke)[source]ΒΆ
@pass_context accepts a handler typed with the enhanced Context.
Clickβs own pass_context is typed for the base click.Context, so annotating the handler with click-extraβs Context (to reach its extra helpers) would fail static type checks by parameter contravariance. This must both type-check (mypy covers this file) and forward the active enhanced Context at runtime.