test_arguments.py
This file
Path
tests/test_arguments.py
191 lines · 5.0 KiB · Python
Last changed by
Fixed a bug in argument handlingArmin Ronacherbefore adoptiona6af25d
History of this fileTake it
The walk stopped after 200 commits: lines older than e45ef4c point at it.
Merge pull request #88 from julen/doc-fixesArmin Ronacherbefore adoptione45ef4c
lines 1–74why, one step back1# -*- coding: utf-8 -*-2import click345def test_nargs_star(runner):6 @click.command()7 @click.argument('src', nargs=-1)8 @click.argument('dst')9 def copy(src, dst):10 click.echo('src=%s' % '|'.join(src))11 click.echo('dst=%s' % dst)1213 result = runner.invoke(copy, ['foo.txt', 'bar.txt', 'dir'])14 assert not result.exception15 assert result.output.splitlines() == [16 'src=foo.txt|bar.txt',17 'dst=dir',18 ]192021def test_nargs_tup(runner):22 @click.command()23 @click.argument('name', nargs=1)24 @click.argument('point', nargs=2, type=click.INT)25 def copy(name, point):26 click.echo('name=%s' % name)27 click.echo('point=%d/%d' % point)2829 result = runner.invoke(copy, ['peter', '1', '2'])30 assert not result.exception31 assert result.output.splitlines() == [32 'name=peter',33 'point=1/2',34 ]353637def test_nargs_err(runner):38 @click.command()39 @click.argument('x')40 def copy(x):41 click.echo(x)4243 result = runner.invoke(copy, ['foo'])44 assert not result.exception45 assert result.output == 'foo\n'4647 result = runner.invoke(copy, ['foo', 'bar'])48 assert result.exit_code == 249 assert 'Got unexpected extra argument (bar)' in result.output505152def test_file_args(runner):53 @click.command()54 @click.argument('input', type=click.File('rb'))55 @click.argument('output', type=click.File('wb'))56 def inout(input, output):57 while True:58 chunk = input.read(1024)59 if not chunk:60 break61 output.write(chunk)6263 with runner.isolated_filesystem():64 result = runner.invoke(inout, ['-', 'hello.txt'], input='Hey!')65 assert result.output == ''66 assert result.exit_code == 067 with open('hello.txt', 'rb') as f:68 assert f.read() == b'Hey!'6970 result = runner.invoke(inout, ['hello.txt', '-'])71 assert result.output == 'Hey!'72 assert result.exit_code == 07374Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25
lines 75–94why, one step back75def test_file_atomics(runner):76 @click.command()77 @click.argument('output', type=click.File('wb', atomic=True))78 def inout(output):79 output.write(b'Foo bar baz\n')80 output.flush()81 with open(output.name, 'rb') as f:82 old_content = f.read()83 assert old_content == b'OLD\n'8485 with runner.isolated_filesystem():86 with open('foo.txt', 'wb') as f:87 f.write(b'OLD\n')88 result = runner.invoke(inout, ['foo.txt'], input='Hey!')89 assert result.output == ''90 assert result.exit_code == 091 with open('foo.txt', 'rb') as f:92 assert f.read() == b'Foo bar baz\n'9394Merge branch '1.0-maintenance'Armin Ronacherbefore adoption8b057c7
lines 95–106why, one step back95def test_stdout_default(runner):96 @click.command()97 @click.argument('output', type=click.File('w'), default='-')98 def inout(output):99 output.write('Foo bar baz\n')100 output.flush()101102 result = runner.invoke(inout, [])103 assert not result.exception104 assert result.output == 'Foo bar baz\n'105106Merge pull request #88 from julen/doc-fixesArmin Ronacherbefore adoptione45ef4c
lines 107–125why, one step back107def test_nargs_envvar(runner):108 @click.command()109 @click.option('--arg', nargs=-1)110 def cmd(arg):111 click.echo('|'.join(arg))112113 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',114 env={'TEST_ARG': 'foo bar'})115 assert not result.exception116 assert result.output == 'foo|bar\n'117118 @click.command()119 @click.option('--arg', envvar='X', nargs=-1)120 def cmd(arg):121 click.echo('|'.join(arg))122123 result = runner.invoke(cmd, [], env={'X': 'foo bar'})124 assert not result.exception125 assert result.output == 'foo|bar\n'Added test for "--"Armin Ronacherbefore adoptiona1f69d0
lines 126–127why, one step back126127Added a test for empty nargsArmin Ronacherbefore adoption4a30d5c
lines 128–129why, one step back128def test_empty_nargs(runner):129 @click.command()Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418
line 130why, one step back130 @click.argument('arg', nargs=-1)Added a test for empty nargsArmin Ronacherbefore adoption4a30d5c
lines 131–136why, one step back131 def cmd(arg):132 click.echo('arg:' + '|'.join(arg))133134 result = runner.invoke(cmd, [])135 assert result.exit_code == 0136 assert result.output == 'arg:\n'Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418
lines 137–145why, one step back137138 @click.command()139 @click.argument('arg', nargs=-1, required=True)140 def cmd2(arg):141 click.echo('arg:' + '|'.join(arg))142143 result = runner.invoke(cmd2, [])144 assert result.exit_code == 2145 assert 'Missing argument "arg"' in result.outputAdded a test for empty nargsArmin Ronacherbefore adoption4a30d5c
lines 146–147why, one step back146147Fixed a bug in argument handlingArmin Ronacherbefore adoptiona6af25d
lines 148–158why, one step back148def test_missing_arg(runner):149 @click.command()150 @click.argument('arg')151 def cmd(arg):152 click.echo('arg:' + arg)153154 result = runner.invoke(cmd, [])155 assert result.exit_code == 2156 assert 'Missing argument "arg".' in result.output157158setting a default for arguments now implicitly makes it non requiredArmin Ronacherbefore adoption71da0b4
lines 159–169why, one step back159def test_implicit_non_required(runner):160 @click.command()161 @click.argument('f', default='test')162 def cli(f):163 click.echo(f)164165 result = runner.invoke(cli, [])166 assert result.exit_code == 0167 assert result.output == 'test\n'168169Added test for "--"Armin Ronacherbefore adoptiona1f69d0
lines 170–191why, one step back170def test_eat_options(runner):171 @click.command()172 @click.option('-f')173 @click.argument('files', nargs=-1)174 def cmd(f, files):175 for filename in files:176 click.echo(filename)177 click.echo(f)178179 result = runner.invoke(cmd, ['--', '-foo', 'bar'])180 assert result.output.splitlines() == [181 '-foo',182 'bar',183 '',184 ]185186 result = runner.invoke(cmd, ['-f', '-x', '--', '-foo', 'bar'])187 assert result.output.splitlines() == [188 '-foo',189 'bar',190 '-x',191 ]