Skip to content
Myr

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 file

Take it

1# -*- 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 == 0737475def 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'939495def 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'105106107def 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'126127128def test_empty_nargs(runner):129    @click.command()130    @click.argument('arg', nargs=-1)131    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'137138    @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.output146147148def 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.output157158159def 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'168169170def 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    ]