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_group_add_command_name(runner): cli = click.Group("cli") cmd = click.Command("a", params=[click.Option(["-x"], required=True)]) cli.add_command(cmd, "b") # Check that the command is accessed through the registered name, # not the original name. result = runner.invoke(cli, ["b"], default_map={"b": {"x": 3}}) assert result.exit_code == 0
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"), )
def test_path_type(runner, cls, expect): cli = click.Command( "cli", params=[click.Argument(["p"], type=click.Path(path_type=cls))], callback=lambda p: p, ) result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False) assert result.exception is None assert result.return_value == expect
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
def test_propagate_show_default_setting(runner): """A context's ``show_default`` setting defaults to the value from the parent context. """ group = click.Group( commands={ "sub": click.Command("sub", params=[click.Option(["-a"], default="a")]), }, context_settings={"show_default": True}, ) result = runner.invoke(group, ["sub", "--help"]) assert "[default: a]" in result.output
def test_cast_multi_default(runner, nargs, multiple, default, expect): if nargs == -1: param = click.Argument(["a"], nargs=nargs, default=default) else: param = click.Option(["-a"], nargs=nargs, multiple=multiple, default=default) cli = click.Command("cli", params=[param], callback=lambda a: a) result = runner.invoke(cli, standalone_mode=False) assert result.exception is None assert result.return_value == expect
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
def test_help_invalid_default(runner): cli = click.Command( "cli", params=[ click.Option( ["-a"], type=click.Path(exists=True), default="not found", show_default=True, ), ], ) result = runner.invoke(cli, ["--help"]) assert result.exit_code == 0 assert "default: not found" in result.output
def test_context_formatter_class(): """A context with a custom ``formatter_class`` should format help using that type. """ class CustomFormatter(click.HelpFormatter): def write_heading(self, heading): heading = click.style(heading, fg="yellow") return super().write_heading(heading) class CustomContext(click.Context): formatter_class = CustomFormatter context = CustomContext(click.Command("test", params=[click.Option(["--value"])]), color=True) assert "\x1b[33mOptions\x1b[0m:" in context.get_help()
def test_formatting_with_options_metavar_empty(runner): cli = click.Command("cli", options_metavar="", params=[click.Argument(["var"])]) result = runner.invoke(cli, ["--help"]) assert "Usage: cli VAR\n" in result.output
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
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
"type": INT_PARAM_TYPE[1], "required": False, "nargs": 1, "multiple": False, "default": 1, "envvar": None, "help": None, "prompt": None, "is_flag": False, "flag_value": False, "count": False, "hidden": False, }, ) HELLO_COMMAND = ( click.Command("hello", params=[NUMBER_OPTION[0]]), { "name": "hello", "params": [NUMBER_OPTION[1], HELP_OPTION[1]], "help": None, "epilog": None, "short_help": None, "hidden": False, "deprecated": False, }, ) HELLO_GROUP = ( click.Group("cli", [HELLO_COMMAND[0]]), { "name": "cli", "params": [HELP_OPTION[1]],
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_confirm_repeat(runner): cli = click.Command( "cli", params=[click.Option(["--a/--no-a"], default=None, prompt=True)] ) result = runner.invoke(cli, input="\ny\n") assert result.output == "A [y/n]: \nError: invalid input\nA [y/n]: y\n"
def test_suggest_possible_options(runner, value, expect): cli = click.Command( "cli", params=[click.Option(["--bound"]), click.Option(["--count"])]) result = runner.invoke(cli, [value]) assert expect in result.output