test_arguments.py
This file
Path
tests/test_arguments.py
298 lines · 7.7 KiB · Python
Last changed by
Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
History of this fileTake it
The walk stopped after 200 commits: lines older than bf1ddce point at it.
Add zlib as import to windowsArmin Ronacherbefore adoptionbf1ddce
line 1why, one step back1# -*- coding: utf-8 -*-Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
line 2why, one step back2import pytestAdd zlib as import to windowsArmin Ronacherbefore adoptionbf1ddce
lines 3–109why, one step back3import click4from click._compat import PY2567def test_nargs_star(runner):8 @click.command()9 @click.argument('src', nargs=-1)10 @click.argument('dst')11 def copy(src, dst):12 click.echo('src=%s' % '|'.join(src))13 click.echo('dst=%s' % dst)1415 result = runner.invoke(copy, ['foo.txt', 'bar.txt', 'dir'])16 assert not result.exception17 assert result.output.splitlines() == [18 'src=foo.txt|bar.txt',19 'dst=dir',20 ]212223def test_nargs_default(runner):24 try:25 @click.command()26 @click.argument('src', nargs=-1, default=42)27 def copy(src):28 pass29 except TypeError as e:30 assert 'nargs=-1' in str(e)31 else:32 assert False333435def test_nargs_tup(runner):36 @click.command()37 @click.argument('name', nargs=1)38 @click.argument('point', nargs=2, type=click.INT)39 def copy(name, point):40 click.echo('name=%s' % name)41 click.echo('point=%d/%d' % point)4243 result = runner.invoke(copy, ['peter', '1', '2'])44 assert not result.exception45 assert result.output.splitlines() == [46 'name=peter',47 'point=1/2',48 ]495051def test_nargs_tup_composite(runner):52 variations = [53 dict(type=(str, int)),54 dict(type=click.Tuple([str, int])),55 dict(nargs=2, type=click.Tuple([str, int])),56 dict(nargs=2, type=(str, int)),57 ]5859 for opts in variations:60 @click.command()61 @click.argument('item', **opts)62 def copy(item):63 click.echo('name=%s id=%d' % item)6465 result = runner.invoke(copy, ['peter', '1'])66 assert not result.exception67 assert result.output.splitlines() == [68 'name=peter id=1',69 ]707172def test_nargs_err(runner):73 @click.command()74 @click.argument('x')75 def copy(x):76 click.echo(x)7778 result = runner.invoke(copy, ['foo'])79 assert not result.exception80 assert result.output == 'foo\n'8182 result = runner.invoke(copy, ['foo', 'bar'])83 assert result.exit_code == 284 assert 'Got unexpected extra argument (bar)' in result.output858687def test_file_args(runner):88 @click.command()89 @click.argument('input', type=click.File('rb'))90 @click.argument('output', type=click.File('wb'))91 def inout(input, output):92 while True:93 chunk = input.read(1024)94 if not chunk:95 break96 output.write(chunk)9798 with runner.isolated_filesystem():99 result = runner.invoke(inout, ['-', 'hello.txt'], input='Hey!')100 assert result.output == ''101 assert result.exit_code == 0102 with open('hello.txt', 'rb') as f:103 assert f.read() == b'Hey!'104105 result = runner.invoke(inout, ['hello.txt', '-'])106 assert result.output == 'Hey!'107 assert result.exit_code == 0108109Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoptiond40fc8c
lines 110–120why, one step back110def test_path_args(runner):111 @click.command()112 @click.argument('input', type=click.Path(dir_okay=False, allow_dash=True))113 def foo(input):114 click.echo(input)115116 result = runner.invoke(foo, ['-'])117 assert result.output == '-\n'118 assert result.exit_code == 0119120Add zlib as import to windowsArmin Ronacherbefore adoptionbf1ddce
lines 121–191why, one step back121def test_file_atomics(runner):122 @click.command()123 @click.argument('output', type=click.File('wb', atomic=True))124 def inout(output):125 output.write(b'Foo bar baz\n')126 output.flush()127 with open(output.name, 'rb') as f:128 old_content = f.read()129 assert old_content == b'OLD\n'130131 with runner.isolated_filesystem():132 with open('foo.txt', 'wb') as f:133 f.write(b'OLD\n')134 result = runner.invoke(inout, ['foo.txt'], input='Hey!',135 catch_exceptions=False)136 assert result.output == ''137 assert result.exit_code == 0138 with open('foo.txt', 'rb') as f:139 assert f.read() == b'Foo bar baz\n'140141142def test_stdout_default(runner):143 @click.command()144 @click.argument('output', type=click.File('w'), default='-')145 def inout(output):146 output.write('Foo bar baz\n')147 output.flush()148149 result = runner.invoke(inout, [])150 assert not result.exception151 assert result.output == 'Foo bar baz\n'152153154def test_nargs_envvar(runner):155 @click.command()156 @click.option('--arg', nargs=2)157 def cmd(arg):158 click.echo('|'.join(arg))159160 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',161 env={'TEST_ARG': 'foo bar'})162 assert not result.exception163 assert result.output == 'foo|bar\n'164165 @click.command()166 @click.option('--arg', envvar='X', nargs=2)167 def cmd(arg):168 click.echo('|'.join(arg))169170 result = runner.invoke(cmd, [], env={'X': 'foo bar'})171 assert not result.exception172 assert result.output == 'foo|bar\n'173174175def test_empty_nargs(runner):176 @click.command()177 @click.argument('arg', nargs=-1)178 def cmd(arg):179 click.echo('arg:' + '|'.join(arg))180181 result = runner.invoke(cmd, [])182 assert result.exit_code == 0183 assert result.output == 'arg:\n'184185 @click.command()186 @click.argument('arg', nargs=-1, required=True)187 def cmd2(arg):188 click.echo('arg:' + '|'.join(arg))189190 result = runner.invoke(cmd2, [])191 assert result.exit_code == 2Merge pull request #709 from sirosen/bugfix/error-hints/let-param-decideLokiHokiebefore adoption3cb9973
line 192why, one step back192 assert 'Missing argument "ARG..."' in result.outputAdd zlib as import to windowsArmin Ronacherbefore adoptionbf1ddce
lines 193–202why, one step back193194195def test_missing_arg(runner):196 @click.command()197 @click.argument('arg')198 def cmd(arg):199 click.echo('arg:' + arg)200201 result = runner.invoke(cmd, [])202 assert result.exit_code == 2Merge pull request #709 from sirosen/bugfix/error-hints/let-param-decideLokiHokiebefore adoption3cb9973
line 203why, one step back203 assert 'Missing argument "ARG".' in result.outputAdd zlib as import to windowsArmin Ronacherbefore adoptionbf1ddce
lines 204–272why, one step back204205206def test_implicit_non_required(runner):207 @click.command()208 @click.argument('f', default='test')209 def cli(f):210 click.echo(f)211212 result = runner.invoke(cli, [])213 assert result.exit_code == 0214 assert result.output == 'test\n'215216217def test_eat_options(runner):218 @click.command()219 @click.option('-f')220 @click.argument('files', nargs=-1)221 def cmd(f, files):222 for filename in files:223 click.echo(filename)224 click.echo(f)225226 result = runner.invoke(cmd, ['--', '-foo', 'bar'])227 assert result.output.splitlines() == [228 '-foo',229 'bar',230 '',231 ]232233 result = runner.invoke(cmd, ['-f', '-x', '--', '-foo', 'bar'])234 assert result.output.splitlines() == [235 '-foo',236 'bar',237 '-x',238 ]239240241def test_nargs_star_ordering(runner):242 @click.command()243 @click.argument('a', nargs=-1)244 @click.argument('b')245 @click.argument('c')246 def cmd(a, b, c):247 for arg in (a, b, c):248 click.echo(arg)249250 result = runner.invoke(cmd, ['a', 'b', 'c'])251 assert result.output.splitlines() == [252 PY2 and "(u'a',)" or "('a',)",253 'b',254 'c',255 ]256257258def test_nargs_specified_plus_star_ordering(runner):259 @click.command()260 @click.argument('a', nargs=-1)261 @click.argument('b')262 @click.argument('c', nargs=2)263 def cmd(a, b, c):264 for arg in (a, b, c):265 click.echo(arg)266267 result = runner.invoke(cmd, ['a', 'b', 'c', 'd', 'e', 'f'])268 assert result.output.splitlines() == [269 PY2 and "(u'a', u'b', u'c')" or "('a', 'b', 'c')",270 'd',271 PY2 and "(u'e', u'f')" or "('e', 'f')",272 ]Implemented defaults for args with nargs > 1.Armin Ronacherbefore adoption5f98f57
lines 273–290why, one step back273274275def test_defaults_for_nargs(runner):276 @click.command()277 @click.argument('a', nargs=2, type=int, default=(1, 2))278 def cmd(a):279 x, y = a280 click.echo(x + y)281282 result = runner.invoke(cmd, [])283 assert result.output.strip() == '3'284285 result = runner.invoke(cmd, ['3', '4'])286 assert result.output.strip() == '7'287288 result = runner.invoke(cmd, ['3'])289 assert result.exception is not None290 assert 'argument a takes 2 values' in result.outputMerge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523
lines 291–298why, one step back291292293def test_multiple_param_decls_not_allowed(runner):294 with pytest.raises(TypeError):295 @click.command()296 @click.argument('x', click.Choice(['a', 'b']))297 def copy(x):298 click.echo(x)