2014-10-16 20:40:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
|
|
import uuid
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
import click
|
|
|
|
|
|
|
|
|
|
|
|
def test_basic_functionality(runner):
|
|
|
|
@click.command()
|
|
|
|
def cli():
|
|
|
|
"""Hello World!"""
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("I EXECUTED")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "Hello World!" in result.output
|
|
|
|
assert "Show this message and exit." in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exit_code == 0
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "I EXECUTED" not in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "I EXECUTED" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exit_code == 0
|
|
|
|
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
def test_repr():
|
|
|
|
@click.command()
|
|
|
|
def command():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def group():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@group.command()
|
|
|
|
def subcommand():
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert repr(command) == "<Command command>"
|
|
|
|
assert repr(group) == "<Group group>"
|
|
|
|
assert repr(subcommand) == "<Command subcommand>"
|
|
|
|
|
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
def test_return_values():
|
|
|
|
@click.command()
|
|
|
|
def cli():
|
|
|
|
return 42
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
with cli.make_context("foo", []) as ctx:
|
2014-10-16 20:40:34 +02:00
|
|
|
rv = cli.invoke(ctx)
|
2020-07-21 08:23:42 +02:00
|
|
|
assert rv == 42
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_basic_group(runner):
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
"""This is the root."""
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("ROOT EXECUTED")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def subcommand():
|
|
|
|
"""This is a subcommand."""
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("SUBCOMMAND EXECUTED")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "COMMAND [ARGS]..." in result.output
|
|
|
|
assert "This is the root" in result.output
|
|
|
|
assert "This is a subcommand." in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exit_code == 0
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "ROOT EXECUTED" not in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["subcommand"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.exit_code == 0
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "ROOT EXECUTED" in result.output
|
|
|
|
assert "SUBCOMMAND EXECUTED" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_basic_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--foo", default="no value")
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(foo):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo(u"FOO:[{}]".format(foo))
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[no value]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo=42"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[42]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "--foo option requires an argument" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo="])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, [u"--foo=\N{SNOWMAN}"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert u"FOO:[\N{SNOWMAN}]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_int_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--foo", default=42)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(foo):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("FOO:[{}]".format(foo * 2))
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[84]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo=23"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[46]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo=bar"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "Invalid value for '--foo': bar is not a valid integer" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_uuid_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option(
|
|
|
|
"--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID
|
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(u):
|
|
|
|
assert type(u) is uuid.UUID
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("U:[{}]".format(u))
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "U:[ba122011-349f-423b-873b-9d6a79c688ab]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--u=bar"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "Invalid value for '--u': bar is not a valid UUID value" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_float_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--foo", default=42, type=click.FLOAT)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(foo):
|
|
|
|
assert type(foo) is float
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("FOO:[{}]".format(foo))
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[42.0]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo=23.5"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "FOO:[23.5]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--foo=bar"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "Invalid value for '--foo': bar is not a valid float" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_boolean_option(runner):
|
|
|
|
for default in True, False:
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--with-foo/--without-foo", default=default)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(with_foo):
|
|
|
|
click.echo(with_foo)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--with-foo"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "True\n"
|
|
|
|
result = runner.invoke(cli, ["--without-foo"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "False\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "{}\n".format(default)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
for default in True, False:
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--flag", is_flag=True, default=default)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(flag):
|
|
|
|
click.echo(flag)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--flag"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "{}\n".format(not default)
|
2014-10-16 20:40:34 +02:00
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "{}\n".format(default)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
def test_boolean_conversion(runner):
|
|
|
|
for default in True, False:
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--flag", default=default, type=bool)
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(flag):
|
|
|
|
click.echo(flag)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
for value in "true", "t", "1", "yes", "y":
|
|
|
|
result = runner.invoke(cli, ["--flag", value])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "True\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
for value in "false", "f", "0", "no", "n":
|
|
|
|
result = runner.invoke(cli, ["--flag", value])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "False\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "{}\n".format(default)
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
def test_file_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--file", type=click.File("w"))
|
2014-10-16 20:40:34 +02:00
|
|
|
def input(file):
|
2020-07-21 08:23:42 +02:00
|
|
|
file.write("Hello World!\n")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--file", type=click.File("r"))
|
2014-10-16 20:40:34 +02:00
|
|
|
def output(file):
|
|
|
|
click.echo(file.read())
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-07-21 08:23:42 +02:00
|
|
|
result_in = runner.invoke(input, ["--file=example.txt"])
|
|
|
|
result_out = runner.invoke(output, ["--file=example.txt"])
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
assert not result_in.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result_in.output == ""
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result_out.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result_out.output == "Hello World!\n\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_file_lazy_mode(runner):
|
|
|
|
do_io = False
|
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--file", type=click.File("w"))
|
2014-10-16 20:40:34 +02:00
|
|
|
def input(file):
|
|
|
|
if do_io:
|
2020-07-21 08:23:42 +02:00
|
|
|
file.write("Hello World!\n")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--file", type=click.File("r"))
|
2014-10-16 20:40:34 +02:00
|
|
|
def output(file):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-07-21 08:23:42 +02:00
|
|
|
os.mkdir("example.txt")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
do_io = True
|
2020-07-21 08:23:42 +02:00
|
|
|
result_in = runner.invoke(input, ["--file=example.txt"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result_in.exit_code == 1
|
|
|
|
|
|
|
|
do_io = False
|
2020-07-21 08:23:42 +02:00
|
|
|
result_in = runner.invoke(input, ["--file=example.txt"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result_in.exit_code == 0
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result_out = runner.invoke(output, ["--file=example.txt"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result_out.exception
|
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--file", type=click.File("w", lazy=False))
|
2014-10-16 20:40:34 +02:00
|
|
|
def input_non_lazy(file):
|
2020-07-21 08:23:42 +02:00
|
|
|
file.write("Hello World!\n")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-07-21 08:23:42 +02:00
|
|
|
os.mkdir("example.txt")
|
|
|
|
result_in = runner.invoke(input_non_lazy, ["--file=example.txt"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result_in.exit_code == 2
|
2020-07-21 08:23:42 +02:00
|
|
|
assert (
|
|
|
|
"Invalid value for '--file': Could not open file: example.txt"
|
2014-10-16 20:40:34 +02:00
|
|
|
in result_in.output
|
2020-07-21 08:23:42 +02:00
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_path_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("-O", type=click.Path(file_okay=False, exists=True, writable=True))
|
2014-10-16 20:40:34 +02:00
|
|
|
def write_to_dir(o):
|
2020-07-21 08:23:42 +02:00
|
|
|
with open(os.path.join(o, "foo.txt"), "wb") as f:
|
|
|
|
f.write(b"meh\n")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-07-21 08:23:42 +02:00
|
|
|
os.mkdir("test")
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(write_to_dir, ["-O", "test"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
with open("test/foo.txt", "rb") as f:
|
|
|
|
assert f.read() == b"meh\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(write_to_dir, ["-O", "test/foo.txt"])
|
|
|
|
assert "is a file" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("-f", type=click.Path(exists=True))
|
2014-10-16 20:40:34 +02:00
|
|
|
def showtype(f):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("is_file={}".format(os.path.isfile(f)))
|
|
|
|
click.echo("is_dir={}".format(os.path.isdir(f)))
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(showtype, ["-f", "xxx"])
|
|
|
|
assert "does not exist" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(showtype, ["-f", "."])
|
|
|
|
assert "is_file=False" in result.output
|
|
|
|
assert "is_dir=True" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("-f", type=click.Path())
|
2014-10-16 20:40:34 +02:00
|
|
|
def exists(f):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("exists={}".format(os.path.exists(f)))
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(exists, ["-f", "xxx"])
|
|
|
|
assert "exists=False" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(exists, ["-f", "."])
|
|
|
|
assert "exists=True" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_choice_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--method", type=click.Choice(["foo", "bar", "baz"]))
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(method):
|
|
|
|
click.echo(method)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--method=foo"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "foo\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--method=meh"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exit_code == 2
|
2020-07-21 08:23:42 +02:00
|
|
|
assert (
|
|
|
|
"Invalid value for '--method': invalid choice: meh."
|
|
|
|
" (choose from foo, bar, baz)" in result.output
|
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
|
|
|
assert "--method [foo|bar|baz]" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
2019-01-07 17:51:19 +01:00
|
|
|
def test_datetime_option_default(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--start_date", type=click.DateTime())
|
2019-01-07 17:51:19 +01:00
|
|
|
def cli(start_date):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))
|
2019-01-07 17:51:19 +01:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--start_date=2015-09-29"])
|
2019-01-07 17:51:19 +01:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "2015-09-29T00:00:00\n"
|
2019-01-07 17:51:19 +01:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--start_date=2015-09-29T09:11:22"])
|
2019-01-07 17:51:19 +01:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "2015-09-29T09:11:22\n"
|
2019-01-07 17:51:19 +01:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--start_date=2015-09"])
|
2019-01-07 17:51:19 +01:00
|
|
|
assert result.exit_code == 2
|
2020-07-21 08:23:42 +02:00
|
|
|
assert (
|
|
|
|
"Invalid value for '--start_date':"
|
|
|
|
" invalid datetime format: 2015-09."
|
|
|
|
" (choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S, %Y-%m-%d %H:%M:%S)"
|
|
|
|
) in result.output
|
2019-01-07 17:51:19 +01:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
|
|
|
assert (
|
|
|
|
"--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output
|
|
|
|
)
|
2019-01-07 17:51:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_datetime_option_custom(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--start_date", type=click.DateTime(formats=["%A %B %d, %Y"]))
|
2019-01-07 17:51:19 +01:00
|
|
|
def cli(start_date):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))
|
2019-01-07 17:51:19 +01:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--start_date=Wednesday June 05, 2010"])
|
2019-01-07 17:51:19 +01:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "2010-06-05T00:00:00\n"
|
2019-01-07 17:51:19 +01:00
|
|
|
|
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
def test_int_range_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--x", type=click.IntRange(0, 5))
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(x):
|
|
|
|
click.echo(x)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--x=5"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "5\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--x=6"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exit_code == 2
|
2020-07-21 08:23:42 +02:00
|
|
|
assert (
|
|
|
|
"Invalid value for '--x': 6 is not in the valid range of 0 to 5.\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
in result.output
|
2020-07-21 08:23:42 +02:00
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--x", type=click.IntRange(0, 5, clamp=True))
|
2014-10-16 20:40:34 +02:00
|
|
|
def clamp(x):
|
|
|
|
click.echo(x)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(clamp, ["--x=5"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "5\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(clamp, ["--x=6"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "5\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(clamp, ["--x=-1"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "0\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
def test_float_range_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--x", type=click.FloatRange(0, 5))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(x):
|
|
|
|
click.echo(x)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--x=5.0"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "5.0\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--x=6.0"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert result.exit_code == 2
|
2020-07-21 08:23:42 +02:00
|
|
|
assert (
|
|
|
|
"Invalid value for '--x': 6.0 is not in the valid range of 0 to 5.\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
in result.output
|
2020-07-21 08:23:42 +02:00
|
|
|
)
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--x", type=click.FloatRange(0, 5, clamp=True))
|
2018-09-06 20:55:10 +02:00
|
|
|
def clamp(x):
|
|
|
|
click.echo(x)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(clamp, ["--x=5.0"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "5.0\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(clamp, ["--x=6.0"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "5\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(clamp, ["--x=-1.0"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "0\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
def test_required_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--foo", required=True)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(foo):
|
|
|
|
click.echo(foo)
|
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert result.exit_code == 2
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "Missing option '--foo'" in result.output
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_evaluation_order(runner):
|
|
|
|
called = []
|
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
def memo(ctx, param, value):
|
2014-10-16 20:40:34 +02:00
|
|
|
called.append(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--missing", default="missing", is_eager=False, callback=memo)
|
|
|
|
@click.option("--eager-flag1", flag_value="eager1", is_eager=True, callback=memo)
|
|
|
|
@click.option("--eager-flag2", flag_value="eager2", is_eager=True, callback=memo)
|
|
|
|
@click.option("--eager-flag3", flag_value="eager3", is_eager=True, callback=memo)
|
|
|
|
@click.option("--normal-flag1", flag_value="normal1", is_eager=False, callback=memo)
|
|
|
|
@click.option("--normal-flag2", flag_value="normal2", is_eager=False, callback=memo)
|
|
|
|
@click.option("--normal-flag3", flag_value="normal3", is_eager=False, callback=memo)
|
2014-10-16 20:40:34 +02:00
|
|
|
def cli(**x):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--eager-flag2",
|
|
|
|
"--eager-flag1",
|
|
|
|
"--normal-flag2",
|
|
|
|
"--eager-flag3",
|
|
|
|
"--normal-flag3",
|
|
|
|
"--normal-flag3",
|
|
|
|
"--normal-flag1",
|
|
|
|
"--normal-flag1",
|
|
|
|
],
|
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert called == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"eager2",
|
|
|
|
"eager1",
|
|
|
|
"eager3",
|
|
|
|
"normal2",
|
|
|
|
"normal3",
|
|
|
|
"normal1",
|
|
|
|
"missing",
|
2014-10-16 20:40:34 +02:00
|
|
|
]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_hidden_option(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--nope", hidden=True)
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(nope):
|
|
|
|
click.echo(nope)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert result.exit_code == 0
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "--nope" not in result.output
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_hidden_command(runner):
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command(hidden=True)
|
|
|
|
def nope():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert result.exit_code == 0
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "nope" not in result.output
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_hidden_group(runner):
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.group(hidden=True)
|
|
|
|
def subgroup():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@subgroup.command()
|
|
|
|
def nope():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert result.exit_code == 0
|
2020-07-21 08:23:42 +02:00
|
|
|
assert "subgroup" not in result.output
|
|
|
|
assert "nope" not in result.output
|