Skip to content
Myr

test_chain.py

This file

Path

tests/test_chain.py

245 lines · 5.7 KiB · Python

Last changed by

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

History of this file

Take it

The walk stopped after 200 commits: lines older than 9ce34f2 point at it.

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 1–165why, one step back
1import sys
23import pytest45import click678def debug():9 click.echo(10 f"{sys._getframe(1).f_code.co_name}"11 f"={'|'.join(click.get_current_context().args)}"12 )131415def test_basic_chaining(runner):16 @click.group(chain=True)17 def cli():18 pass1920 @cli.command("sdist")21 def sdist():22 click.echo("sdist called")2324 @cli.command("bdist")25 def bdist():26 click.echo("bdist called")2728 result = runner.invoke(cli, ["bdist", "sdist", "bdist"])29 assert not result.exception30 assert result.output.splitlines() == [31 "bdist called",32 "sdist called",33 "bdist called",34 ]353637@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)47def test_chaining_help(runner, args, expect):48 @click.group(chain=True)49 def cli():50 """ROOT HELP"""51 pass5253 @cli.command("sdist")54 def sdist():55 """SDIST HELP"""56 click.echo("sdist called")5758 @cli.command("bdist")59 def bdist():60 """BDIST HELP"""61 click.echo("bdist called")6263 result = runner.invoke(cli, args)64 assert not result.exception65 assert expect in result.output666768def test_chaining_with_options(runner):69 @click.group(chain=True)70 def cli():71 pass7273 @cli.command("sdist")74 @click.option("--format")75 def sdist(format):76 click.echo(f"sdist called {format}")7778 @cli.command("bdist")79 @click.option("--format")80 def bdist(format):81 click.echo(f"bdist called {format}")8283 result = runner.invoke(cli, ["bdist", "--format=1", "sdist", "--format=2"])84 assert not result.exception85 assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]868788@pytest.mark.parametrize(("chain", "expect"), [(False, "1"), (True, "[]")])89def test_no_command_result_callback(runner, chain, expect):90 """When a group has ``invoke_without_command=True``, the result91 callback is always invoked. A regular group invokes it with92 its return value, a chained group with ``[]``.93 """9495 @click.group(invoke_without_command=True, chain=chain)96 def cli():97 return 19899 @cli.result_callback()100 def process_result(result):101 click.echo(result, nl=False)102103 result = runner.invoke(cli, [])104 assert result.output == expect105106107def test_chaining_with_arguments(runner):108 @click.group(chain=True)109 def cli():110 pass111112 @cli.command("sdist")113 @click.argument("format")114 def sdist(format):115 click.echo(f"sdist called {format}")116117 @cli.command("bdist")118 @click.argument("format")119 def bdist(format):120 click.echo(f"bdist called {format}")121122 result = runner.invoke(cli, ["bdist", "1", "sdist", "2"])123 assert not result.exception124 assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]125126127@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)135def 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 pass140141 @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)148149 @cli.command("uppercase")150 def make_uppercase():151 def processor(iterator):152 for line in iterator:153 yield line.upper()154155 return processor156157 @cli.command("strip")158 def make_strip():159 def processor(iterator):160 for line in iterator:161 yield line.strip()162163 return processor164165 result = runner.invoke(cli, args, input=input)

Merge branch 'main' into stableDavid Lordbefore adoption81a482f

lines 166–167why, one step back
166 assert not result.exception
167 assert result.output.splitlines() == expect

Make faqs section. Add the first question about windows shell expansion. (#2927)Rowlando13before adoption9ce34f2

lines 168–245why, one step back
168
169170def test_args_and_chain(runner):171 @click.group(chain=True)172 def cli():173 debug()174175 @cli.command()176 def a():177 debug()178179 @cli.command()180 def b():181 debug()182183 @cli.command()184 def c():185 debug()186187 result = runner.invoke(cli, ["a", "b", "c"])188 assert not result.exception189 assert result.output.splitlines() == ["cli=", "a=", "b=", "c="]190191192def test_group_arg_behavior(runner):193 with pytest.raises(RuntimeError):194195 @click.group(chain=True)196 @click.argument("forbidden", required=False)197 def bad_cli():198 pass199200 with pytest.raises(RuntimeError):201202 @click.group(chain=True)203 @click.argument("forbidden", nargs=-1)204 def bad_cli2():205 pass206207 @click.group(chain=True)208 @click.argument("arg")209 def cli(arg):210 click.echo(f"cli:{arg}")211212 @cli.command()213 def a():214 click.echo("a")215216 result = runner.invoke(cli, ["foo", "a"])217 assert not result.exception218 assert result.output.splitlines() == ["cli:foo", "a"]219220221@pytest.mark.xfail222def test_group_chaining(runner):223 @click.group(chain=True)224 def cli():225 debug()226227 @cli.group()228 def l1a():229 debug()230231 @l1a.command()232 def l2a():233 debug()234235 @l1a.command()236 def l2b():237 debug()238239 @cli.command()240 def l1b():241 debug()242243 result = runner.invoke(cli, ["l1a", "l2a", "l1b"])244 assert not result.exception245 assert result.output.splitlines() == ["cli=", "l1a=", "l2a=", "l1b="]