diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2014-08-12 11:36:00 +0200 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2014-08-12 11:36:00 +0200 |
commit | 60714161d638fc036d59b927f200b7045f9ec5cc (patch) | |
tree | c5c6fc6b9f35b6e8cc40292bba3061a95bea233a /tests/test_chain.py | |
parent | bd61da2632589a7df74a790dc5f799532827bb97 (diff) | |
download | click-60714161d638fc036d59b927f200b7045f9ec5cc.tar.gz |
Added a test for pipelines
Diffstat (limited to 'tests/test_chain.py')
-rw-r--r-- | tests/test_chain.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test_chain.py b/tests/test_chain.py index 4116814..64ebb85 100644 --- a/tests/test_chain.py +++ b/tests/test_chain.py @@ -127,3 +127,54 @@ def test_context_subcommand_info_sync(): ctx.invoked_subcommands = ['foo', 'bar'] assert ctx.invoked_subcommand == '*' assert ctx.invoked_subcommands == ['foo', 'bar'] + + +def test_pipeline(runner): + @click.group(chain=True, invoke_without_command=True) + @click.option('-i', '--input', type=click.File('r')) + def cli(input): + pass + + @cli.resultcallback() + def process_pipeline(processors, input): + iterator = (x.rstrip('\r\n') for x in input) + for processor in processors: + iterator = processor(iterator) + for item in iterator: + click.echo(item) + + @cli.command('uppercase') + def make_uppercase(): + def processor(iterator): + for line in iterator: + yield line.upper() + return processor + + @cli.command('strip') + def make_strip(): + def processor(iterator): + for line in iterator: + yield line.strip() + return processor + + result = runner.invoke(cli, ['-i', '-'], input='foo\nbar') + assert not result.exception + assert result.output.splitlines() == [ + 'foo', + 'bar', + ] + + result = runner.invoke(cli, ['-i', '-', 'strip'], input='foo \n bar') + assert not result.exception + assert result.output.splitlines() == [ + 'foo', + 'bar', + ] + + result = runner.invoke(cli, ['-i', '-', 'strip', 'uppercase'], + input='foo \n bar') + assert not result.exception + assert result.output.splitlines() == [ + 'FOO', + 'BAR', + ] |