Skip to content
Myr

test_options.py

This file

Path

tests/test_options.py

219 lines · 6.3 KiB · Python

Last changed by

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption1697599

History of this file

Take it

The walk stopped after 200 commits: lines older than 3269a22 point at it.

Merge pull request #91 from invl/masterArmin Ronacherbefore adoption3269a22

lines 1–4why, one step back
1# -*- coding: utf-8 -*-
2import re3import os4import click

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption0c2ab87

line 5why, one step back
5import pytest

Merge pull request #91 from invl/masterArmin Ronacherbefore adoption3269a22

lines 6–23why, one step back
6
78def test_prefixes(runner):9 @click.command()10 @click.option('++foo', is_flag=True, help='das foo')11 @click.option('--bar', is_flag=True, help='das bar')12 def cli(foo, bar):13 click.echo('foo=%s bar=%s' % (foo, bar))1415 result = runner.invoke(cli, ['++foo', '--bar'])16 assert not result.exception17 assert result.output == 'foo=True bar=True\n'1819 result = runner.invoke(cli, ['--help'])20 assert re.search(r'\+\+foo\s+das foo', result.output) is not None21 assert re.search(r'--bar\s+das bar', result.output) is not None2223

Disallow empty options to help users find options that should be arguments.Armin Ronacherbefore adoption1646647

lines 24–36why, one step back
24def test_invalid_option(runner):
25 try:26 @click.command()27 @click.option('foo')28 def cli(foo):29 pass30 except TypeError as e:31 assert 'No options defined but a name was passed (foo).' \32 in str(e)33 else:34 assert False, 'Expected a type error because of an invalid option.'3536

Merge pull request #91 from invl/masterArmin Ronacherbefore adoption3269a22

lines 37–55why, one step back
37def test_counting(runner):
38 @click.command()39 @click.option('-v', count=True, help='Verbosity',40 type=click.IntRange(0, 3))41 def cli(v):42 click.echo('verbosity=%d' % v)4344 result = runner.invoke(cli, ['-vvv'])45 assert not result.exception46 assert result.output == 'verbosity=3\n'4748 result = runner.invoke(cli, ['-vvvv'])49 assert result.exception50 assert 'Invalid value for "-v": 4 is not in the valid range of 0 to 3.' \51 in result.output5253 result = runner.invoke(cli, [])54 assert not result.exception55 assert result.output == 'verbosity=0\n'

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption0c2ab87

line 56why, one step back
56

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption1697599

lines 57–59why, one step back
57 result = runner.invoke(cli, ['--help'])
58 assert re.search('-v\s+Verbosity', result.output) is not None59

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption0c2ab87

lines 60–69why, one step back
60
61@pytest.mark.parametrize('unknown_flag', ['--foo', '-f'])62def test_unknown_options(runner, unknown_flag):63 @click.command()64 def cli():65 pass6667 result = runner.invoke(cli, [unknown_flag])68 assert result.exception69 assert 'no such option: {0}'.format(unknown_flag) in result.output

Merge pull request #91 from invl/masterArmin Ronacherbefore adoption3269a22

lines 70–138why, one step back
70
7172def test_multiple_required(runner):73 @click.command()74 @click.option('-m', '--message', multiple=True, required=True)75 def cli(message):76 click.echo('\n'.join(message))7778 result = runner.invoke(cli, ['-m', 'foo', '-mbar'])79 assert not result.exception80 assert result.output == 'foo\nbar\n'8182 result = runner.invoke(cli, [])83 assert result.exception84 assert 'Error: Missing option "-m" / "--message".' in result.output858687def test_multiple_envvar(runner):88 @click.command()89 @click.option('--arg', multiple=True)90 def cmd(arg):91 click.echo('|'.join(arg))9293 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',94 env={'TEST_ARG': 'foo bar baz'})95 assert not result.exception96 assert result.output == 'foo|bar|baz\n'9798 @click.command()99 @click.option('--arg', multiple=True, envvar='X')100 def cmd(arg):101 click.echo('|'.join(arg))102103 result = runner.invoke(cmd, [], env={'X': 'foo bar baz'})104 assert not result.exception105 assert result.output == 'foo|bar|baz\n'106107 @click.command()108 @click.option('--arg', multiple=True, type=click.Path())109 def cmd(arg):110 click.echo('|'.join(arg))111112 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',113 env={'TEST_ARG': 'foo%sbar' % os.path.pathsep})114 assert not result.exception115 assert result.output == 'foo|bar\n'116117118def test_nargs_envvar(runner):119 @click.command()120 @click.option('--arg', nargs=2)121 def cmd(arg):122 click.echo('|'.join(arg))123124 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',125 env={'TEST_ARG': 'foo bar'})126 assert not result.exception127 assert result.output == 'foo|bar\n'128129 @click.command()130 @click.option('--arg', nargs=2, multiple=True)131 def cmd(arg):132 for item in arg:133 click.echo('|'.join(item))134135 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',136 env={'TEST_ARG': 'x 1 y 2'})137 assert not result.exception138 assert result.output == 'x|1\ny|2\n'

Added support for BadParameter exceptions.Armin Ronacherbefore adoption1407ecb

lines 139–157why, one step back
139
140141def test_custom_validation(runner):142 def validate_pos_int(ctx, value):143 if value < 0:144 raise click.BadParameter('Value needs to be positive')145 return value146147 @click.command()148 @click.option('--foo', callback=validate_pos_int, default=1)149 def cmd(foo):150 click.echo(foo)151152 result = runner.invoke(cmd, ['--foo', '-1'])153 assert 'Invalid value for "--foo": Value needs to be positive' \154 in result.output155156 result = runner.invoke(cmd, ['--foo', '42'])157 assert result.output == '42\n'

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

lines 158–160why, one step back
158
159160def test_winstyle_options(runner):

User interface improvements.Armin Ronacherbefore adoptionc2e187b

line 161why, one step back
161 @click.command()

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

lines 162–165why, one step back
162 @click.option('/debug;/no-debug', help='Enables or disables debug mode.')
163 def cmd(debug):164 click.echo(debug)165

User interface improvements.Armin Ronacherbefore adoptionc2e187b

line 166why, one step back
166 result = runner.invoke(cmd, ['/debug'], help_option_names=['/?'])

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

line 167why, one step back
167 assert result.output == 'True\n'

User interface improvements.Armin Ronacherbefore adoptionc2e187b

line 168why, one step back
168 result = runner.invoke(cmd, ['/no-debug'], help_option_names=['/?'])

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

line 169why, one step back
169 assert result.output == 'False\n'

User interface improvements.Armin Ronacherbefore adoptionc2e187b

line 170why, one step back
170 result = runner.invoke(cmd, [], help_option_names=['/?'])

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

line 171why, one step back
171 assert result.output == 'False\n'

User interface improvements.Armin Ronacherbefore adoptionc2e187b

line 172why, one step back
172 result = runner.invoke(cmd, ['/?'], help_option_names=['/?'])

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

line 173why, one step back
173 assert '/debug; /no-debug Enables or disables debug mode.' in result.output

User interface improvements.Armin Ronacherbefore adoptionc2e187b

line 174why, one step back
174 assert '/? Show this message and exit.' in result.output

Improved handling of dos/windows arguments greatly.Armin Ronacherbefore adoption133c417

lines 175–186why, one step back
175
176177def test_legacy_options(runner):178 @click.command()179 @click.option('-whatever')180 def cmd(whatever):181 click.echo(whatever)182183 result = runner.invoke(cmd, ['-whatever', '42'])184 assert result.output == '42\n'185 result = runner.invoke(cmd, ['-whatever=23'])186 assert result.output == '23\n'

Implemented extra info for missing choice parameters. This fixes #120Armin Ronacherbefore adoption264fa24

lines 187–199why, one step back
187
188189def test_missing_choice(runner):190 @click.command()191 @click.option('--foo', type=click.Choice(['foo', 'bar']),192 required=True)193 def cmd(foo):194 click.echo(foo)195196 result = runner.invoke(cmd)197 assert result.exit_code == 2198 assert 'Error: Missing option "--foo". Choose from foo, bar.' \199 in result.output

Merge pull request #123 from untitaker/issue122Armin Ronacherbefore adoptiond914ad6

lines 200–219why, one step back
200
201202def test_multiline_help(runner):203 @click.command()204 @click.option('--foo', help="""205 hello206207 i am208209 multiline210 """)211 def cmd(foo):212 click.echo(foo)213214 result = runner.invoke(cmd, ['--help'])215 assert result.exit_code == 0216 out = result.output.splitlines()217 assert ' --foo TEXT hello' in out218 assert ' i am' in out219 assert ' multiline' in out