def test_missing_argument_string_cast(): ctx = click.Context(click.Command("")) with pytest.raises(click.MissingParameter) as excinfo: click.Argument(["a"], required=True).process_value(ctx, None) assert str(excinfo.value) == "Missing parameter: a"
def test_multiple_default_type(): opt = click.Option(["-a"], multiple=True, default=(1, 2)) assert opt.nargs == 1 assert opt.multiple assert opt.type is click.INT ctx = click.Context(click.Command("test")) assert opt.get_default(ctx) == (1, 2)
def test_missing_option_string_cast(): ctx = click.Context(click.Command("")) with pytest.raises(click.MissingParameter) as excinfo: click.Option(["-a"], required=True).full_process_value(ctx, None) assert str(excinfo.value) == "missing parameter: a"
def test_multiple_default_composite_type(): opt = click.Option(["-a"], multiple=True, default=[(1, "a")]) assert opt.nargs == 2 assert opt.multiple assert isinstance(opt.type, click.Tuple) assert opt.type.types == [click.INT, click.STRING] ctx = click.Context(click.Command("test")) assert opt.get_default(ctx) == ((1, "a"), )
async def test_context(): ctx = click.Context(HELLO_COMMAND[0]) out = await ctx.to_info_dict() assert out == { "command": HELLO_COMMAND[1], "info_name": None, "allow_extra_args": False, "allow_interspersed_args": True, "ignore_unknown_options": False, "auto_envvar_prefix": None, }
def test_show_default_boolean_flag_value(runner): """When a boolean flag only has one opt, it will show the default value, not the opt name. """ opt = click.Option(("--cache", ), is_flag=True, show_default=True, help="Enable the cache.") ctx = click.Context(click.Command("test")) message = opt.get_help_record(ctx)[1] assert "[default: False]" in message
async def test_with_resource(): @contextmanager def manager(): val = [1] yield val val[0] = 0 ctx = click.Context(click.Command("test")) async with ctx.scope(): rv = ctx.with_resource(manager()) assert rv[0] == 1 assert rv == [0]
def test_show_default_boolean_flag_name(runner, default, expect): """When a boolean flag has distinct True/False opts, it should show the default opt name instead of the default value. It should only show one name even if multiple are declared. """ opt = click.Option( ("--cache/--no-cache", "--c/--nc"), default=default, show_default=True, help="Enable/Disable the cache.", ) ctx = click.Context(click.Command("test")) message = opt.get_help_record(ctx)[1] assert f"[default: {expect}]" in message
async def test_context_mgr(): @contextmanager def manager(): val = [1] yield val val[0] = 0 @click.command() def cli(): pass ctx = click.Context(cli) async with ctx.scope(): rv = ctx.enter_context(manager()) assert rv[0] == 1, rv # Internal assert ctx._depth == 1 assert rv == [0], rv
async def test_context_pushing(): rv = [] @click.command() def cli(): pass ctx = click.Context(cli) @ctx.call_on_close def test_callback(): rv.append(42) async with ctx.scope(cleanup=False): # Internal assert ctx._depth == 2 assert rv == [] async with ctx.scope(): # Internal assert ctx._depth == 1 assert rv == [42]
async def test_command(obj, expect): ctx = click.Context(obj) out = await obj.to_info_dict(ctx) assert out == expect
def test_do_not_show_no_default(runner): """When show_default is True and no default is set do not show None.""" opt = click.Option(["--limit"], show_default=True) ctx = click.Context(click.Command("cli")) message = opt.get_help_record(ctx)[1] assert "[default: None]" not in message
def test_show_default_string(runner): """When show_default is a string show that value as default.""" opt = click.Option(["--limit"], show_default="unlimited") ctx = click.Context(click.Command("cli")) message = opt.get_help_record(ctx)[1] assert "[default: (unlimited)]" in message
def test_intrange_default_help_text(runner, type, expect): option = click.Option(["--count"], type=type, show_default=True, default=2) context = click.Context(click.Command("test")) result = option.get_help_record(context)[1] assert expect in result