test_chain.py
This file
Path
tests/test_chain.py
252 lines · 5.6 KiB · Python
Last changed by
Merge branch '6.x-maintenance'Armin Ronacherbefore adoption6fcda3e
History of this fileTake it
The walk stopped after 200 commits: lines older than be17393 point at it.
Disallow arguments in chain mode and no longer expose leftover args.Armin Ronacherbefore adoption1c7e1d3
line 1why, one step back1import sysAdded missing changelog entries.Armin Ronacherbefore adoptionbe17393
line 2why, one step back2import clickDisallow arguments in chain mode and no longer expose leftover args.Armin Ronacherbefore adoption1c7e1d3
line 3why, one step back3import pytestAdded missing changelog entries.Armin Ronacherbefore adoptionbe17393
lines 4–5why, one step back45Implemented nesting below chain commands.Armin Ronacherbefore adoption0a2919f
lines 6–12why, one step back6def debug():7 click.echo('%s=%s' % (8 sys._getframe(1).f_code.co_name,9 '|'.join(click.get_current_context().args),10 ))1112Added missing changelog entries.Armin Ronacherbefore adoptionbe17393
lines 13–163why, one step back13def test_basic_chaining(runner):14 @click.group(chain=True)15 def cli():16 pass1718 @cli.command('sdist')19 def sdist():20 click.echo('sdist called')2122 @cli.command('bdist')23 def bdist():24 click.echo('bdist called')2526 result = runner.invoke(cli, ['bdist', 'sdist', 'bdist'])27 assert not result.exception28 assert result.output.splitlines() == [29 'bdist called',30 'sdist called',31 'bdist called',32 ]333435def test_chaining_help(runner):36 @click.group(chain=True)37 def cli():38 """ROOT HELP"""39 pass4041 @cli.command('sdist')42 def sdist():43 """SDIST HELP"""44 click.echo('sdist called')4546 @cli.command('bdist')47 def bdist():48 """BDIST HELP"""49 click.echo('bdist called')5051 result = runner.invoke(cli, ['--help'])52 assert not result.exception53 assert 'COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...' in result.output54 assert 'ROOT HELP' in result.output5556 result = runner.invoke(cli, ['sdist', '--help'])57 assert not result.exception58 assert 'SDIST HELP' in result.output5960 result = runner.invoke(cli, ['bdist', '--help'])61 assert not result.exception62 assert 'BDIST HELP' in result.output6364 result = runner.invoke(cli, ['bdist', 'sdist', '--help'])65 assert not result.exception66 assert 'SDIST HELP' in result.output676869def test_chaining_with_options(runner):70 @click.group(chain=True)71 def cli():72 pass7374 @cli.command('sdist')75 @click.option('--format')76 def sdist(format):77 click.echo('sdist called %s' % format)7879 @cli.command('bdist')80 @click.option('--format')81 def bdist(format):82 click.echo('bdist called %s' % format)8384 result = runner.invoke(cli, ['bdist', '--format=1', 'sdist', '--format=2'])85 assert not result.exception86 assert result.output.splitlines() == [87 'bdist called 1',88 'sdist called 2',89 ]909192def test_chaining_with_arguments(runner):93 @click.group(chain=True)94 def cli():95 pass9697 @cli.command('sdist')98 @click.argument('format')99 def sdist(format):100 click.echo('sdist called %s' % format)101102 @cli.command('bdist')103 @click.argument('format')104 def bdist(format):105 click.echo('bdist called %s' % format)106107 result = runner.invoke(cli, ['bdist', '1', 'sdist', '2'])108 assert not result.exception109 assert result.output.splitlines() == [110 'bdist called 1',111 'sdist called 2',112 ]113114115def test_pipeline(runner):116 @click.group(chain=True, invoke_without_command=True)117 @click.option('-i', '--input', type=click.File('r'))118 def cli(input):119 pass120121 @cli.resultcallback()122 def process_pipeline(processors, input):123 iterator = (x.rstrip('\r\n') for x in input)124 for processor in processors:125 iterator = processor(iterator)126 for item in iterator:127 click.echo(item)128129 @cli.command('uppercase')130 def make_uppercase():131 def processor(iterator):132 for line in iterator:133 yield line.upper()134 return processor135136 @cli.command('strip')137 def make_strip():138 def processor(iterator):139 for line in iterator:140 yield line.strip()141 return processor142143 result = runner.invoke(cli, ['-i', '-'], input='foo\nbar')144 assert not result.exception145 assert result.output.splitlines() == [146 'foo',147 'bar',148 ]149150 result = runner.invoke(cli, ['-i', '-', 'strip'], input='foo \n bar')151 assert not result.exception152 assert result.output.splitlines() == [153 'foo',154 'bar',155 ]156157 result = runner.invoke(cli, ['-i', '-', 'strip', 'uppercase'],158 input='foo \n bar')159 assert not result.exception160 assert result.output.splitlines() == [161 'FOO',162 'BAR',163 ]Disallow arguments in chain mode and no longer expose leftover args.Armin Ronacherbefore adoption1c7e1d3
lines 164–192why, one step back164165166def test_args_and_chain(runner):167 @click.group(chain=True)168 def cli():169 debug()170171 @cli.command()172 def a():173 debug()174175 @cli.command()176 def b():177 debug()178179 @cli.command()180 def c():181 debug()182183 result = runner.invoke(cli, ['a', 'b', 'c'])184 assert not result.exception185 assert result.output.splitlines() == [186 'cli=',187 'a=',188 'b=',189 'c=',190 ]191192Support non optional arguments on commands.Armin Ronacherbefore adoptione68766c
line 193why, one step back193def test_multicommand_arg_behavior(runner):Disallow arguments in chain mode and no longer expose leftover args.Armin Ronacherbefore adoption1c7e1d3
lines 194–195why, one step back194 with pytest.raises(RuntimeError):195 @click.group(chain=True)Support non optional arguments on commands.Armin Ronacherbefore adoptione68766c
lines 196–197why, one step back196 @click.argument('forbidden', required=False)197 def bad_cli():Disallow arguments in chain mode and no longer expose leftover args.Armin Ronacherbefore adoption1c7e1d3
line 198why, one step back198 passSupport non optional arguments on commands.Armin Ronacherbefore adoptione68766c
lines 199–219why, one step back199200 with pytest.raises(RuntimeError):201 @click.group(chain=True)202 @click.argument('forbidden', nargs=-1)203 def bad_cli2():204 pass205206 @click.group(chain=True)207 @click.argument('arg')208 def cli(arg):209 click.echo('cli:%s' % arg)210211 @cli.command()212 def a():213 click.echo('a')214215 result = runner.invoke(cli, ['foo', 'a'])216 assert not result.exception217 assert result.output.splitlines() == [218 'cli:foo',219 'a',Implemented nesting below chain commands.Armin Ronacherbefore adoption0a2919f
lines 220–222why, one step back220 ]221222Merge branch '6.x-maintenance'Armin Ronacherbefore adoption6fcda3e
line 223why, one step back223@pytest.mark.xfailImplemented nesting below chain commands.Armin Ronacherbefore adoption0a2919f
lines 224–251why, one step back224def test_multicommand_chaining(runner):225 @click.group(chain=True)226 def cli():227 debug()228229 @cli.group()230 def l1a():231 debug()232233 @l1a.command()234 def l2a():235 debug()236237 @l1a.command()238 def l2b():239 debug()240241 @cli.command()242 def l1b():243 debug()244245 result = runner.invoke(cli, ['l1a', 'l2a', 'l1b'])246 assert not result.exception247 assert result.output.splitlines() == [248 'cli=',249 'l1a=',250 'l2a=',251 'l1b=',Support non optional arguments on commands.Armin Ronacherbefore adoptione68766c
line 252why, one step back252 ]