python-click/tests/test_chain.py

246 lines
5.7 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(
2021-10-10 03:31:57 +02:00
f"{sys._getframe(1).f_code.co_name}"
f"={'|'.join(click.get_current_context().args)}"
2020-07-21 08:23:42 +02:00
)
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
]
2022-11-30 09:52:01 +01:00
@pytest.mark.parametrize(
("args", "expect"),
[
(["--help"], "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."),
(["--help"], "ROOT HELP"),
(["sdist", "--help"], "SDIST HELP"),
(["bdist", "--help"], "BDIST HELP"),
(["bdist", "sdist", "--help"], "SDIST HELP"),
],
)
def test_chaining_help(runner, args, expect):
2014-10-16 20:40:34 +02:00
@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
2022-11-30 09:52:01 +01:00
result = runner.invoke(cli, args)
2014-10-16 20:40:34 +02:00
assert not result.exception
2022-11-30 09:52:01 +01:00
assert expect 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):
2021-10-10 03:31:57 +02:00
click.echo(f"sdist called {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):
2021-10-10 03:31:57 +02:00
click.echo(f"bdist called {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
2022-11-30 09:52:01 +01:00
@pytest.mark.parametrize(("chain", "expect"), [(False, "1"), (True, "[]")])
2021-10-10 03:31:57 +02:00
def test_no_command_result_callback(runner, chain, expect):
"""When a group has ``invoke_without_command=True``, the result
callback is always invoked. A regular group invokes it with
2022-11-30 09:52:01 +01:00
its return value, a chained group with ``[]``.
2021-10-10 03:31:57 +02:00
"""
@click.group(invoke_without_command=True, chain=chain)
def cli():
2022-11-30 09:52:01 +01:00
return 1
2021-10-10 03:31:57 +02:00
@cli.result_callback()
def process_result(result):
2022-11-30 09:52:01 +01:00
click.echo(result, nl=False)
2021-10-10 03:31:57 +02:00
result = runner.invoke(cli, [])
assert result.output == expect
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):
2021-10-10 03:31:57 +02:00
click.echo(f"sdist called {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):
2021-10-10 03:31:57 +02:00
click.echo(f"bdist called {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
2022-11-30 09:52:01 +01:00
@pytest.mark.parametrize(
("args", "input", "expect"),
[
(["-f", "-"], "foo\nbar", ["foo", "bar"]),
(["-f", "-", "strip"], "foo \n bar", ["foo", "bar"]),
(["-f", "-", "strip", "uppercase"], "foo \n bar", ["FOO", "BAR"]),
],
)
def test_pipeline(runner, args, input, expect):
2014-10-16 20:40:34 +02:00
@click.group(chain=True, invoke_without_command=True)
2022-11-30 09:52:01 +01:00
@click.option("-f", type=click.File("r"))
def cli(f):
2014-10-16 20:40:34 +02:00
pass
2021-10-10 03:31:57 +02:00
@cli.result_callback()
2022-11-30 09:52:01 +01:00
def process_pipeline(processors, f):
iterator = (x.rstrip("\r\n") for x in f)
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
2022-11-30 09:52:01 +01:00
result = runner.invoke(cli, args, input=input)
2014-10-16 20:40:34 +02:00
assert not result.exception
2022-11-30 09:52:01 +01:00
assert result.output.splitlines() == expect
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):
2021-10-10 03:31:57 +02:00
click.echo(f"cli:{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="]