import logging from contextlib import AbstractContextManager from contextlib import contextmanager from types import TracebackType import pytest import click from click import Context from click import Option from click import Parameter from click.core import ParameterSource from click.decorators import help_option from click.decorators import pass_meta_key def test_ensure_context_objects(runner): class Foo: def __init__(self): self.title = "default" pass_foo = click.make_pass_decorator(Foo, ensure=True) @click.group() @pass_foo def cli(foo): pass @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ["test"]) assert not result.exception assert result.output == "default\n" def test_get_context_objects(runner): class Foo: def __init__(self): self.title = "default" pass_foo = click.make_pass_decorator(Foo, ensure=True) @click.group() @click.pass_context def cli(ctx): ctx.obj = Foo() ctx.obj.title = "test" @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ["test"]) assert not result.exception assert result.output == "test\n" def test_get_context_objects_no_ensuring(runner): class Foo: def __init__(self): self.title = "default" pass_foo = click.make_pass_decorator(Foo) @click.group() @click.pass_context def cli(ctx): ctx.obj = Foo() ctx.obj.title = "test" @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ["test"]) assert not result.exception assert result.output == "test\n" def test_get_context_objects_missing(runner): class Foo: pass pass_foo = click.make_pass_decorator(Foo) @click.group() @click.pass_context def cli(ctx): pass @cli.command() @pass_foo def test(foo): click.echo(foo.title) result = runner.invoke(cli, ["test"]) assert result.exception is not None assert isinstance(result.exception, RuntimeError) assert ( "Managed to invoke callback without a context object of type" " 'Foo' existing" in str(result.exception) ) def test_multi_enter(runner): called = [] @click.command() @click.pass_context def cli(ctx): def callback(): called.append(True) ctx.call_on_close(callback) with ctx: pass assert not called result = runner.invoke(cli, []) assert result.exception is None assert called == [True] def test_global_context_object(runner): @click.command() @click.pass_context def cli(ctx): assert click.get_current_context() is ctx ctx.obj = "FOOBAR" assert click.get_current_context().obj == "FOOBAR" assert click.get_current_context(silent=True) is None runner.invoke(cli, [], catch_exceptions=False) assert click.get_current_context(silent=True) is None def test_context_meta(runner): LANG_KEY = f"{__name__}.lang" def set_language(value): click.get_current_context().meta[LANG_KEY] = value def get_language(): return click.get_current_context().meta.get(LANG_KEY, "en_US") @click.command() @click.pass_context def cli(ctx): assert get_language() == "en_US" set_language("de_DE") assert get_language() == "de_DE" runner.invoke(cli, [], catch_exceptions=False) def test_make_pass_meta_decorator(runner): @click.group() @click.pass_context def cli(ctx): ctx.meta["value"] = "good" @cli.command() @pass_meta_key("value") def show(value): return value result = runner.invoke(cli, ["show"], standalone_mode=False) assert result.return_value == "good" def test_make_pass_meta_decorator_doc(): pass_value = pass_meta_key("value") assert "the 'value' key from :attr:`click.Context.meta`" in pass_value.__doc__ pass_value = pass_meta_key("value", doc_description="the test value") assert "passes the test value" in pass_value.__doc__ def test_hiding_of_unset_sentinel_in_callbacks(): """Fix: https://github.com/pallets/click/issues/3136""" def inspect_other_params(ctx, param, value): """A callback that inspects other parameters' values via the context.""" assert click.get_current_context() is ctx click.echo(f"callback.my_arg: {ctx.params.get('my_arg')!r}") click.echo(f"callback.my_opt: {ctx.params.get('my_opt')!r}") click.echo(f"callback.my_callback: {ctx.params.get('my_callback')!r}") click.echo(f"callback.param: {param!r}") click.echo(f"callback.value: {value!r}") return "hard-coded" class ParameterInternalCheck(Option): """An option that checks internal state during processing.""" def process_value(self, ctx, value): """Check that UNSET values are hidden as None in ctx.params within the callback, and then properly restored afterwards. """ assert click.get_current_context() is ctx click.echo(f"before_process.my_arg: {ctx.params.get('my_arg')!r}") click.echo(f"before_process.my_opt: {ctx.params.get('my_opt')!r}") click.echo(f"before_process.my_callback: {ctx.params.get('my_callback')!r}") value = super().process_value(ctx, value) assert click.get_current_context() is ctx click.echo(f"after_process.my_arg: {ctx.params.get('my_arg')!r}") click.echo(f"after_process.my_opt: {ctx.params.get('my_opt')!r}") click.echo(f"after_process.my_callback: {ctx.params.get('my_callback')!r}") return value def change_other_params(ctx, param, value): """A callback that modifies other parameters' values via the context.""" assert click.get_current_context() is ctx click.echo(f"before_change.my_arg: {ctx.params.get('my_arg')!r}") click.echo(f"before_change.my_opt: {ctx.params.get('my_opt')!r}") click.echo(f"before_change.my_callback: {ctx.params.get('my_callback')!r}") click.echo(f"before_change.param: {param!r}") click.echo(f"before_change.value: {value!r}") ctx.params["my_arg"] = "changed" # Reset to None parameters that where not UNSET to see they are not forced back # to UNSET. ctx.params["my_callback"] = None return value @click.command @click.argument("my-arg", required=False) @click.option("--my-opt") @click.option("--my-callback", callback=inspect_other_params) @click.option("--check-internal", cls=ParameterInternalCheck) @click.option( "--modifying-callback", cls=ParameterInternalCheck, callback=change_other_params ) @click.pass_context def cli(ctx, my_arg, my_opt, my_callback, check_internal, modifying_callback): click.echo(f"cli.my_arg: {my_arg!r}") click.echo(f"cli.my_opt: {my_opt!r}") click.echo(f"cli.my_callback: {my_callback!r}") click.echo(f"cli.check_internal: {check_internal!r}") click.echo(f"cli.modifying_callback: {modifying_callback!r}") runner = click.testing.CliRunner() result = runner.invoke(cli) assert result.stdout.splitlines() == [ # Values of other parameters within the callback are None, not UNSET. "callback.my_arg: None", "callback.my_opt: None", "callback.my_callback: None", "callback.param: