python-click/tests/test_chain.py

231 lines
5.4 KiB
Python
Raw Normal View History

2015-12-04 16:51:02 +01:00
import sys
2020-07-21 08:23:42 +02:00
2015-12-04 16:51:02 +01:00
import pytest
2020-07-21 08:23:42 +02:00
import click
2015-12-04 16:51:02 +01:00
def debug():
2020-07-21 08:23:42 +02:00
click.echo(
"{}={}".format(
sys._getframe(1).f_code.co_name, "|".join(click.get_current_context().args)
)
)
2014-10-16 20:40:34 +02:00
def test_basic_chaining(runner):
@click.group(chain=True)
def cli():
pass
2020-07-21 08:23:42 +02:00
@cli.command("sdist")
2014-10-16 20:40:34 +02:00
def sdist():
2020-07-21 08:23:42 +02:00
click.echo("sdist called")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@cli.command("bdist")
2014-10-16 20:40:34 +02:00
def bdist():
2020-07-21 08:23:42 +02:00
click.echo("bdist called")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["bdist", "sdist", "bdist"])
2014-10-16 20:40:34 +02:00
assert not result.exception
assert result.output.splitlines() == [
2020-07-21 08:23:42 +02:00
"bdist called",
"sdist called",
"bdist called",
2014-10-16 20:40:34 +02:00
]
def test_chaining_help(runner):
@click.group(chain=True)
def cli():
"""ROOT HELP"""
pass
2020-07-21 08:23:42 +02:00
@cli.command("sdist")
2014-10-16 20:40:34 +02:00
def sdist():
"""SDIST HELP"""
2020-07-21 08:23:42 +02:00
click.echo("sdist called")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@cli.command("bdist")
2014-10-16 20:40:34 +02:00
def bdist():
"""BDIST HELP"""
2020-07-21 08:23:42 +02:00
click.echo("bdist called")
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 "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..." in result.output
assert "ROOT HELP" in result.output
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["sdist", "--help"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert "SDIST HELP" in result.output
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["bdist", "--help"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert "BDIST HELP" in result.output
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["bdist", "sdist", "--help"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert "SDIST HELP" in result.output
2014-10-16 20:40:34 +02:00
def test_chaining_with_options(runner):
@click.group(chain=True)
def cli():
pass
2020-07-21 08:23:42 +02:00
@cli.command("sdist")
@click.option("--format")
2014-10-16 20:40:34 +02:00
def sdist(format):
2020-07-21 08:23:42 +02:00
click.echo("sdist called {}".format(format))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@cli.command("bdist")
@click.option("--format")
2014-10-16 20:40:34 +02:00
def bdist(format):
2020-07-21 08:23:42 +02:00
click.echo("bdist called {}".format(format))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["bdist", "--format=1", "sdist", "--format=2"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
2014-10-16 20:40:34 +02:00
def test_chaining_with_arguments(runner):
@click.group(chain=True)
def cli():
pass
2020-07-21 08:23:42 +02:00
@cli.command("sdist")
@click.argument("format")
2014-10-16 20:40:34 +02:00
def sdist(format):
2020-07-21 08:23:42 +02:00
click.echo("sdist called {}".format(format))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@cli.command("bdist")
@click.argument("format")
2014-10-16 20:40:34 +02:00
def bdist(format):
2020-07-21 08:23:42 +02:00
click.echo("bdist called {}".format(format))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["bdist", "1", "sdist", "2"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
2014-10-16 20:40:34 +02:00
def test_pipeline(runner):
@click.group(chain=True, invoke_without_command=True)
2020-07-21 08:23:42 +02:00
@click.option("-i", "--input", type=click.File("r"))
2014-10-16 20:40:34 +02:00
def cli(input):
pass
@cli.resultcallback()
def process_pipeline(processors, input):
2020-07-21 08:23:42 +02:00
iterator = (x.rstrip("\r\n") for x in input)
2014-10-16 20:40:34 +02:00
for processor in processors:
iterator = processor(iterator)
for item in iterator:
click.echo(item)
2020-07-21 08:23:42 +02:00
@cli.command("uppercase")
2014-10-16 20:40:34 +02:00
def make_uppercase():
def processor(iterator):
for line in iterator:
yield line.upper()
2020-07-21 08:23:42 +02:00
2014-10-16 20:40:34 +02:00
return processor
2020-07-21 08:23:42 +02:00
@cli.command("strip")
2014-10-16 20:40:34 +02:00
def make_strip():
def processor(iterator):
for line in iterator:
yield line.strip()
2020-07-21 08:23:42 +02:00
2014-10-16 20:40:34 +02:00
return processor
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["-i", "-"], input="foo\nbar")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["foo", "bar"]
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["-i", "-", "strip"], input="foo \n bar")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["foo", "bar"]
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["-i", "-", "strip", "uppercase"], input="foo \n bar")
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["FOO", "BAR"]
2015-12-04 16:51:02 +01:00
def test_args_and_chain(runner):
@click.group(chain=True)
def cli():
debug()
@cli.command()
def a():
debug()
@cli.command()
def b():
debug()
@cli.command()
def c():
debug()
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["a", "b", "c"])
2015-12-04 16:51:02 +01:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["cli=", "a=", "b=", "c="]
2015-12-04 16:51:02 +01:00
def test_multicommand_arg_behavior(runner):
with pytest.raises(RuntimeError):
2020-07-21 08:23:42 +02:00
2015-12-04 16:51:02 +01:00
@click.group(chain=True)
2020-07-21 08:23:42 +02:00
@click.argument("forbidden", required=False)
2015-12-04 16:51:02 +01:00
def bad_cli():
pass
with pytest.raises(RuntimeError):
2020-07-21 08:23:42 +02:00
2015-12-04 16:51:02 +01:00
@click.group(chain=True)
2020-07-21 08:23:42 +02:00
@click.argument("forbidden", nargs=-1)
2015-12-04 16:51:02 +01:00
def bad_cli2():
pass
@click.group(chain=True)
2020-07-21 08:23:42 +02:00
@click.argument("arg")
2015-12-04 16:51:02 +01:00
def cli(arg):
2020-07-21 08:23:42 +02:00
click.echo("cli:{}".format(arg))
2015-12-04 16:51:02 +01:00
@cli.command()
def a():
2020-07-21 08:23:42 +02:00
click.echo("a")
2015-12-04 16:51:02 +01:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["foo", "a"])
2015-12-04 16:51:02 +01:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["cli:foo", "a"]
2015-12-04 16:51:02 +01:00
@pytest.mark.xfail
def test_multicommand_chaining(runner):
@click.group(chain=True)
def cli():
debug()
@cli.group()
def l1a():
debug()
@l1a.command()
def l2a():
debug()
@l1a.command()
def l2b():
debug()
@cli.command()
def l1b():
debug()
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["l1a", "l2a", "l1b"])
2015-12-04 16:51:02 +01:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["cli=", "l1a=", "l2a=", "l1b="]