| 1 |
import sys |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import click |
| 6 |
|
| 7 |
|
| 8 |
def debug(): |
| 9 |
click.echo( |
| 10 |
f"{sys._getframe(1).f_code.co_name}" |
| 11 |
f"={'|'.join(click.get_current_context().args)}" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
def test_basic_chaining(runner): |
| 16 |
@click.group(chain=True) |
| 17 |
def cli(): |
| 18 |
pass |
| 19 |
|
| 20 |
@cli.command("sdist") |
| 21 |
def sdist(): |
| 22 |
click.echo("sdist called") |
| 23 |
|
| 24 |
@cli.command("bdist") |
| 25 |
def bdist(): |
| 26 |
click.echo("bdist called") |
| 27 |
|
| 28 |
result = runner.invoke(cli, ["bdist", "sdist", "bdist"]) |
| 29 |
assert not result.exception |
| 30 |
assert result.output.splitlines() == [ |
| 31 |
"bdist called", |
| 32 |
"sdist called", |
| 33 |
"bdist called", |
| 34 |
] |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize( |
| 38 |
("args", "expect"), |
| 39 |
[ |
| 40 |
(["--help"], "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..."), |
| 41 |
(["--help"], "ROOT HELP"), |
| 42 |
(["sdist", "--help"], "SDIST HELP"), |
| 43 |
(["bdist", "--help"], "BDIST HELP"), |
| 44 |
(["bdist", "sdist", "--help"], "SDIST HELP"), |
| 45 |
], |
| 46 |
) |
| 47 |
def test_chaining_help(runner, args, expect): |
| 48 |
@click.group(chain=True) |
| 49 |
def cli(): |
| 50 |
"""ROOT HELP""" |
| 51 |
pass |
| 52 |
|
| 53 |
@cli.command("sdist") |
| 54 |
def sdist(): |
| 55 |
"""SDIST HELP""" |
| 56 |
click.echo("sdist called") |
| 57 |
|
| 58 |
@cli.command("bdist") |
| 59 |
def bdist(): |
| 60 |
"""BDIST HELP""" |
| 61 |
click.echo("bdist called") |
| 62 |
|
| 63 |
result = runner.invoke(cli, args) |
| 64 |
assert not result.exception |
| 65 |
assert expect in result.output |
| 66 |
|
| 67 |
|
| 68 |
def test_chaining_with_options(runner): |
| 69 |
@click.group(chain=True) |
| 70 |
def cli(): |
| 71 |
pass |
| 72 |
|
| 73 |
@cli.command("sdist") |
| 74 |
@click.option("--format") |
| 75 |
def sdist(format): |
| 76 |
click.echo(f"sdist called {format}") |
| 77 |
|
| 78 |
@cli.command("bdist") |
| 79 |
@click.option("--format") |
| 80 |
def bdist(format): |
| 81 |
click.echo(f"bdist called {format}") |
| 82 |
|
| 83 |
result = runner.invoke(cli, ["bdist", "--format=1", "sdist", "--format=2"]) |
| 84 |
assert not result.exception |
| 85 |
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"] |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.parametrize(("chain", "expect"), [(False, "1"), (True, "[]")]) |
| 89 |
def test_no_command_result_callback(runner, chain, expect): |
| 90 |
"""When a group has ``invoke_without_command=True``, the result |
| 91 |
callback is always invoked. A regular group invokes it with |
| 92 |
its return value, a chained group with ``[]``. |
| 93 |
""" |
| 94 |
|
| 95 |
@click.group(invoke_without_command=True, chain=chain) |
| 96 |
def cli(): |
| 97 |
return 1 |
| 98 |
|
| 99 |
@cli.result_callback() |
| 100 |
def process_result(result): |
| 101 |
click.echo(result, nl=False) |
| 102 |
|
| 103 |
result = runner.invoke(cli, []) |
| 104 |
assert result.output == expect |
| 105 |
|
| 106 |
|
| 107 |
def test_chaining_with_arguments(runner): |
| 108 |
@click.group(chain=True) |
| 109 |
def cli(): |
| 110 |
pass |
| 111 |
|
| 112 |
@cli.command("sdist") |
| 113 |
@click.argument("format") |
| 114 |
def sdist(format): |
| 115 |
click.echo(f"sdist called {format}") |
| 116 |
|
| 117 |
@cli.command("bdist") |
| 118 |
@click.argument("format") |
| 119 |
def bdist(format): |
| 120 |
click.echo(f"bdist called {format}") |
| 121 |
|
| 122 |
result = runner.invoke(cli, ["bdist", "1", "sdist", "2"]) |
| 123 |
assert not result.exception |
| 124 |
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"] |
| 125 |
|
| 126 |
|
| 127 |
@pytest.mark.parametrize( |
| 128 |
("args", "input", "expect"), |
| 129 |
[ |
| 130 |
(["-f", "-"], "foo\nbar", ["foo", "bar"]), |
| 131 |
(["-f", "-", "strip"], "foo \n bar", ["foo", "bar"]), |
| 132 |
(["-f", "-", "strip", "uppercase"], "foo \n bar", ["FOO", "BAR"]), |
| 133 |
], |
| 134 |
) |
| 135 |
def test_pipeline(runner, args, input, expect): |
| 136 |
@click.group(chain=True, invoke_without_command=True) |
| 137 |
@click.option("-f", type=click.File("r")) |
| 138 |
def cli(f): |
| 139 |
pass |
| 140 |
|
| 141 |
@cli.result_callback() |
| 142 |
def process_pipeline(processors, f): |
| 143 |
iterator = (x.rstrip("\r\n") for x in f) |
| 144 |
for processor in processors: |
| 145 |
iterator = processor(iterator) |
| 146 |
for item in iterator: |
| 147 |
click.echo(item) |
| 148 |
|
| 149 |
@cli.command("uppercase") |
| 150 |
def make_uppercase(): |
| 151 |
def processor(iterator): |
| 152 |
for line in iterator: |
| 153 |
yield line.upper() |
| 154 |
|
| 155 |
return processor |
| 156 |
|
| 157 |
@cli.command("strip") |
| 158 |
def make_strip(): |
| 159 |
def processor(iterator): |
| 160 |
for line in iterator: |
| 161 |
yield line.strip() |
| 162 |
|
| 163 |
return processor |
| 164 |
|
| 165 |
result = runner.invoke(cli, args, input=input) |
| 166 |
assert not result.exception |
| 167 |
assert result.output.splitlines() == expect |
| 168 |
|
| 169 |
|
| 170 |
def test_args_and_chain(runner): |
| 171 |
@click.group(chain=True) |
| 172 |
def cli(): |
| 173 |
debug() |
| 174 |
|
| 175 |
@cli.command() |
| 176 |
def a(): |
| 177 |
debug() |
| 178 |
|
| 179 |
@cli.command() |
| 180 |
def b(): |
| 181 |
debug() |
| 182 |
|
| 183 |
@cli.command() |
| 184 |
def c(): |
| 185 |
debug() |
| 186 |
|
| 187 |
result = runner.invoke(cli, ["a", "b", "c"]) |
| 188 |
assert not result.exception |
| 189 |
assert result.output.splitlines() == ["cli=", "a=", "b=", "c="] |
| 190 |
|
| 191 |
|
| 192 |
def test_group_arg_behavior(runner): |
| 193 |
with pytest.raises(RuntimeError): |
| 194 |
|
| 195 |
@click.group(chain=True) |
| 196 |
@click.argument("forbidden", required=False) |
| 197 |
def bad_cli(): |
| 198 |
pass |
| 199 |
|
| 200 |
with pytest.raises(RuntimeError): |
| 201 |
|
| 202 |
@click.group(chain=True) |
| 203 |
@click.argument("forbidden", nargs=-1) |
| 204 |
def bad_cli2(): |
| 205 |
pass |
| 206 |
|
| 207 |
@click.group(chain=True) |
| 208 |
@click.argument("arg") |
| 209 |
def cli(arg): |
| 210 |
click.echo(f"cli:{arg}") |
| 211 |
|
| 212 |
@cli.command() |
| 213 |
def a(): |
| 214 |
click.echo("a") |
| 215 |
|
| 216 |
result = runner.invoke(cli, ["foo", "a"]) |
| 217 |
assert not result.exception |
| 218 |
assert result.output.splitlines() == ["cli:foo", "a"] |
| 219 |
|
| 220 |
|
| 221 |
@pytest.mark.xfail |
| 222 |
def test_group_chaining(runner): |
| 223 |
@click.group(chain=True) |
| 224 |
def cli(): |
| 225 |
debug() |
| 226 |
|
| 227 |
@cli.group() |
| 228 |
def l1a(): |
| 229 |
debug() |
| 230 |
|
| 231 |
@l1a.command() |
| 232 |
def l2a(): |
| 233 |
debug() |
| 234 |
|
| 235 |
@l1a.command() |
| 236 |
def l2b(): |
| 237 |
debug() |
| 238 |
|
| 239 |
@cli.command() |
| 240 |
def l1b(): |
| 241 |
debug() |
| 242 |
|
| 243 |
result = runner.invoke(cli, ["l1a", "l2a", "l1b"]) |
| 244 |
assert not result.exception |
| 245 |
assert result.output.splitlines() == ["cli=", "l1a=", "l2a=", "l1b="] |