Skip to content
Myr

test_arguments.py

This file

Path

tests/test_arguments.py

307 lines · 8.0 KiB · Python

Last changed by

Merge pull request #1306 from Ketzalkotal/PR-1139David Lordbefore adoption0865f79

History of this file

Take it

The walk stopped after 200 commits: lines older than 430c50a point at it.

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 1why, one step back
1# -*- coding: utf-8 -*-

Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523

line 2why, one step back
2import pytest

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 3–191why, one step back
3import click
4from 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 == 0108109110def 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 == 0119120121def 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 == 2

Merge pull request #709 from sirosen/bugfix/error-hints/let-param-decideLokiHokiebefore adoption3cb9973

line 192why, one step back
192 assert 'Missing argument "ARG..."' in result.output

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 193–202why, one step back
193
194195def 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 == 2

Merge pull request #709 from sirosen/bugfix/error-hints/let-param-decideLokiHokiebefore adoption3cb9973

line 203why, one step back
203 assert 'Missing argument "ARG".' in result.output

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 204–205why, one step back
204
205

Merge pull request #1306 from Ketzalkotal/PR-1139David Lordbefore adoption0865f79

lines 206–214why, one step back
206def test_missing_argument_string_cast():
207 ctx = click.Context(click.Command(""))208209 with pytest.raises(click.MissingParameter) as excinfo:210 click.Argument(["a"], required=True).full_process_value(ctx, None)211212 assert str(excinfo.value) == "missing parameter: a"213214

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 215–299why, one step back
215def test_implicit_non_required(runner):
216 @click.command()217 @click.argument('f', default='test')218 def cli(f):219 click.echo(f)220221 result = runner.invoke(cli, [])222 assert result.exit_code == 0223 assert result.output == 'test\n'224225226def test_eat_options(runner):227 @click.command()228 @click.option('-f')229 @click.argument('files', nargs=-1)230 def cmd(f, files):231 for filename in files:232 click.echo(filename)233 click.echo(f)234235 result = runner.invoke(cmd, ['--', '-foo', 'bar'])236 assert result.output.splitlines() == [237 '-foo',238 'bar',239 '',240 ]241242 result = runner.invoke(cmd, ['-f', '-x', '--', '-foo', 'bar'])243 assert result.output.splitlines() == [244 '-foo',245 'bar',246 '-x',247 ]248249250def test_nargs_star_ordering(runner):251 @click.command()252 @click.argument('a', nargs=-1)253 @click.argument('b')254 @click.argument('c')255 def cmd(a, b, c):256 for arg in (a, b, c):257 click.echo(arg)258259 result = runner.invoke(cmd, ['a', 'b', 'c'])260 assert result.output.splitlines() == [261 PY2 and "(u'a',)" or "('a',)",262 'b',263 'c',264 ]265266267def test_nargs_specified_plus_star_ordering(runner):268 @click.command()269 @click.argument('a', nargs=-1)270 @click.argument('b')271 @click.argument('c', nargs=2)272 def cmd(a, b, c):273 for arg in (a, b, c):274 click.echo(arg)275276 result = runner.invoke(cmd, ['a', 'b', 'c', 'd', 'e', 'f'])277 assert result.output.splitlines() == [278 PY2 and "(u'a', u'b', u'c')" or "('a', 'b', 'c')",279 'd',280 PY2 and "(u'e', u'f')" or "('e', 'f')",281 ]282283284def test_defaults_for_nargs(runner):285 @click.command()286 @click.argument('a', nargs=2, type=int, default=(1, 2))287 def cmd(a):288 x, y = a289 click.echo(x + y)290291 result = runner.invoke(cmd, [])292 assert result.output.strip() == '3'293294 result = runner.invoke(cmd, ['3', '4'])295 assert result.output.strip() == '7'296297 result = runner.invoke(cmd, ['3'])298 assert result.exception is not None299 assert 'argument a takes 2 values' in result.output

Merge pull request #1014 from rhgrant10/feature/574David Lordbefore adoptionb0c7523

lines 300–307why, one step back
300
301302def test_multiple_param_decls_not_allowed(runner):303 with pytest.raises(TypeError):304 @click.command()305 @click.argument('x', click.Choice(['a', 'b']))306 def copy(x):307 click.echo(x)