Skip to content
Myr

test_options.py

This file

Path

tests/test_options.py

363 lines · 10.9 KiB · Python

Last changed by

Merge pull request #799 from ericfrederich/option_namesMarkus Unterwaditzerbefore adoptione634d7f

History of this file

Take it

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

Merge branch '3.x-maintenance'Armin Ronacherbefore adoptionf1249f8

lines 1–6why, one step back
1# -*- coding: utf-8 -*-
2import re3import os4import click5import pytest6

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

line 7why, one step back
7from click._compat import text_type

Merge branch '3.x-maintenance'Armin Ronacherbefore adoptionf1249f8

line 8why, one step back
8

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

line 9why, one step back
9

Merge branch '3.x-maintenance'Armin Ronacherbefore adoptionf1249f8

lines 10–38why, one step back
10def test_prefixes(runner):
11 @click.command()12 @click.option('++foo', is_flag=True, help='das foo')13 @click.option('--bar', is_flag=True, help='das bar')14 def cli(foo, bar):15 click.echo('foo=%s bar=%s' % (foo, bar))1617 result = runner.invoke(cli, ['++foo', '--bar'])18 assert not result.exception19 assert result.output == 'foo=True bar=True\n'2021 result = runner.invoke(cli, ['--help'])22 assert re.search(r'\+\+foo\s+das foo', result.output) is not None23 assert re.search(r'--bar\s+das bar', result.output) is not None242526def test_invalid_option(runner):27 try:28 @click.command()29 @click.option('foo')30 def cli(foo):31 pass32 except TypeError as e:33 assert 'No options defined but a name was passed (foo).' \34 in str(e)35 else:36 assert False, 'Expected a type error because of an invalid option.'3738

Error on nargs=-1 for options. This fixes #222Armin Ronacherbefore adoption9da1791

lines 39–50why, one step back
39def test_invalid_nargs(runner):
40 try:41 @click.command()42 @click.option('--foo', nargs=-1)43 def cli(foo):44 pass45 except TypeError as e:46 assert 'Options cannot have nargs < 0' in str(e)47 else:48 assert False, 'Expected a type error because of an invalid option.'4950

Added support for tuple types. This fixes #239Armin Ronacherbefore adoptionf7c124e

lines 51–52why, one step back
51def test_nargs_tup_composite_mult(runner):
52 @click.command()

Fixed up tests for Python 3Armin Ronacherbefore adoptione948c69

line 53why, one step back
53 @click.option('--item', type=(str, int), multiple=True)

Added support for tuple types. This fixes #239Armin Ronacherbefore adoptionf7c124e

lines 54–65why, one step back
54 def copy(item):
55 for item in item:56 click.echo('name=%s id=%d' % item)5758 result = runner.invoke(copy, ['--item', 'peter', '1', '--item', 'max', '2'])59 assert not result.exception60 assert result.output.splitlines() == [61 'name=peter id=1',62 'name=max id=2',63 ]6465

Merge branch '3.x-maintenance'Armin Ronacherbefore adoptionf1249f8

lines 66–146why, one step back
66def test_counting(runner):
67 @click.command()68 @click.option('-v', count=True, help='Verbosity',69 type=click.IntRange(0, 3))70 def cli(v):71 click.echo('verbosity=%d' % v)7273 result = runner.invoke(cli, ['-vvv'])74 assert not result.exception75 assert result.output == 'verbosity=3\n'7677 result = runner.invoke(cli, ['-vvvv'])78 assert result.exception79 assert 'Invalid value for "-v": 4 is not in the valid range of 0 to 3.' \80 in result.output8182 result = runner.invoke(cli, [])83 assert not result.exception84 assert result.output == 'verbosity=0\n'8586 result = runner.invoke(cli, ['--help'])87 assert re.search('-v\s+Verbosity', result.output) is not None888990@pytest.mark.parametrize('unknown_flag', ['--foo', '-f'])91def test_unknown_options(runner, unknown_flag):92 @click.command()93 def cli():94 pass9596 result = runner.invoke(cli, [unknown_flag])97 assert result.exception98 assert 'no such option: {0}'.format(unknown_flag) in result.output99100101def test_multiple_required(runner):102 @click.command()103 @click.option('-m', '--message', multiple=True, required=True)104 def cli(message):105 click.echo('\n'.join(message))106107 result = runner.invoke(cli, ['-m', 'foo', '-mbar'])108 assert not result.exception109 assert result.output == 'foo\nbar\n'110111 result = runner.invoke(cli, [])112 assert result.exception113 assert 'Error: Missing option "-m" / "--message".' in result.output114115116def test_multiple_envvar(runner):117 @click.command()118 @click.option('--arg', multiple=True)119 def cmd(arg):120 click.echo('|'.join(arg))121122 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',123 env={'TEST_ARG': 'foo bar baz'})124 assert not result.exception125 assert result.output == 'foo|bar|baz\n'126127 @click.command()128 @click.option('--arg', multiple=True, envvar='X')129 def cmd(arg):130 click.echo('|'.join(arg))131132 result = runner.invoke(cmd, [], env={'X': 'foo bar baz'})133 assert not result.exception134 assert result.output == 'foo|bar|baz\n'135136 @click.command()137 @click.option('--arg', multiple=True, type=click.Path())138 def cmd(arg):139 click.echo('|'.join(arg))140141 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',142 env={'TEST_ARG': 'foo%sbar' % os.path.pathsep})143 assert not result.exception144 assert result.output == 'foo|bar\n'145146

Merge pull request #252 from ulope/multiple_show_defaultMarkus Unterwaditzerbefore adoption39e51d9

lines 147–148why, one step back
147def test_multiple_default_help(runner):
148 @click.command()

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

line 149why, one step back
149 @click.option('--arg1', multiple=True, default=('foo', 'bar'),

Merge pull request #252 from ulope/multiple_show_defaultMarkus Unterwaditzerbefore adoption39e51d9

line 150why, one step back
150 show_default=True)

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

line 151why, one step back
151 @click.option('--arg2', multiple=True, default=(1, 2), type=int,

Merge pull request #252 from ulope/multiple_show_defaultMarkus Unterwaditzerbefore adoption39e51d9

lines 152–157why, one step back
152 show_default=True)
153 def cmd(arg, arg2):154 pass155156 result = runner.invoke(cmd, ['--help'])157 assert not result.exception

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

lines 158–159why, one step back
158 assert 'foo, bar' in result.output
159 assert '1, 2' in result.output

Merge pull request #252 from ulope/multiple_show_defaultMarkus Unterwaditzerbefore adoption39e51d9

line 160why, one step back
160

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

line 161why, one step back
161

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoption750ba75

lines 162–163why, one step back
162def test_multiple_default_type(runner):
163 @click.command()

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

lines 164–165why, one step back
164 @click.option('--arg1', multiple=True, default=('foo', 'bar'))
165 @click.option('--arg2', multiple=True, default=(1, 'a'))

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoption750ba75

line 166why, one step back
166 def cmd(arg1, arg2):

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

lines 167–168why, one step back
167 assert all(isinstance(e[0], text_type) for e in arg1)
168 assert all(isinstance(e[1], text_type) for e in arg1)

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoption750ba75

line 169why, one step back
169

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

lines 170–171why, one step back
170 assert all(isinstance(e[0], int) for e in arg2)
171 assert all(isinstance(e[1], text_type) for e in arg2)

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoption750ba75

line 172why, one step back
172

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

lines 173–174why, one step back
173 result = runner.invoke(cmd, '--arg1 a b --arg1 test 1 --arg2 2 '
174 'two --arg2 4 four'.split())

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoption750ba75

line 175why, one step back
175 assert not result.exception

Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73

line 176why, one step back
176

Merge pull request #252 from ulope/multiple_show_defaultMarkus Unterwaditzerbefore adoption39e51d9

line 177why, one step back
177

Merge branch '3.x-maintenance'Armin Ronacherbefore adoptionf1249f8

lines 178–279why, one step back
178def test_nargs_envvar(runner):
179 @click.command()180 @click.option('--arg', nargs=2)181 def cmd(arg):182 click.echo('|'.join(arg))183184 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',185 env={'TEST_ARG': 'foo bar'})186 assert not result.exception187 assert result.output == 'foo|bar\n'188189 @click.command()190 @click.option('--arg', nargs=2, multiple=True)191 def cmd(arg):192 for item in arg:193 click.echo('|'.join(item))194195 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',196 env={'TEST_ARG': 'x 1 y 2'})197 assert not result.exception198 assert result.output == 'x|1\ny|2\n'199200201def test_custom_validation(runner):202 def validate_pos_int(ctx, value):203 if value < 0:204 raise click.BadParameter('Value needs to be positive')205 return value206207 @click.command()208 @click.option('--foo', callback=validate_pos_int, default=1)209 def cmd(foo):210 click.echo(foo)211212 result = runner.invoke(cmd, ['--foo', '-1'])213 assert 'Invalid value for "--foo": Value needs to be positive' \214 in result.output215216 result = runner.invoke(cmd, ['--foo', '42'])217 assert result.output == '42\n'218219220def test_winstyle_options(runner):221 @click.command()222 @click.option('/debug;/no-debug', help='Enables or disables debug mode.')223 def cmd(debug):224 click.echo(debug)225226 result = runner.invoke(cmd, ['/debug'], help_option_names=['/?'])227 assert result.output == 'True\n'228 result = runner.invoke(cmd, ['/no-debug'], help_option_names=['/?'])229 assert result.output == 'False\n'230 result = runner.invoke(cmd, [], help_option_names=['/?'])231 assert result.output == 'False\n'232 result = runner.invoke(cmd, ['/?'], help_option_names=['/?'])233 assert '/debug; /no-debug Enables or disables debug mode.' in result.output234 assert '/? Show this message and exit.' in result.output235236237def test_legacy_options(runner):238 @click.command()239 @click.option('-whatever')240 def cmd(whatever):241 click.echo(whatever)242243 result = runner.invoke(cmd, ['-whatever', '42'])244 assert result.output == '42\n'245 result = runner.invoke(cmd, ['-whatever=23'])246 assert result.output == '23\n'247248249def test_missing_choice(runner):250 @click.command()251 @click.option('--foo', type=click.Choice(['foo', 'bar']),252 required=True)253 def cmd(foo):254 click.echo(foo)255256 result = runner.invoke(cmd)257 assert result.exit_code == 2258 assert 'Error: Missing option "--foo". Choose from foo, bar.' \259 in result.output260261262def test_multiline_help(runner):263 @click.command()264 @click.option('--foo', help="""265 hello266267 i am268269 multiline270 """)271 def cmd(foo):272 click.echo(foo)273274 result = runner.invoke(cmd, ['--help'])275 assert result.exit_code == 0276 out = result.output.splitlines()277 assert ' --foo TEXT hello' in out278 assert ' i am' in out279 assert ' multiline' in out

Merge pull request #285 from slafs/optional-param-clsMarkus Unterwaditzerbefore adoption1c3dfac

lines 280–311why, one step back
280
281282def test_argument_custom_class(runner):283 class CustomArgument(click.Argument):284 def get_default(self, ctx):285 '''a dumb override of a default value for testing'''286 return 'I am a default'287288 @click.command()289 @click.argument('testarg', cls=CustomArgument, default='you wont see me')290 def cmd(testarg):291 click.echo(testarg)292293 result = runner.invoke(cmd)294 assert 'I am a default' in result.output295 assert 'you wont see me' not in result.output296297298def test_option_custom_class(runner):299 class CustomOption(click.Option):300 def get_help_record(self, ctx):301 '''a dumb override of a help text for testing'''302 return ('--help', 'I am a help text')303304 @click.command()305 @click.option('--testoption', cls=CustomOption, help='you wont see me')306 def cmd(testoption):307 click.echo(testoption)308309 result = runner.invoke(cmd, ['--help'])310 assert 'I am a help text' in result.output311 assert 'you wont see me' not in result.output

Make it possible to define aliases for the secondary option of boolean flags. This fixes #420Armin Ronacherbefore adoption89d3bfd

lines 312–337why, one step back
312
313314def test_aliases_for_flags(runner):315 @click.command()316 @click.option('--warnings/--no-warnings', ' /-W', default=True)317 def cli(warnings):318 click.echo(warnings)319320 result = runner.invoke(cli, ['--warnings'])321 assert result.output == 'True\n'322 result = runner.invoke(cli, ['--no-warnings'])323 assert result.output == 'False\n'324 result = runner.invoke(cli, ['-W'])325 assert result.output == 'False\n'326327 @click.command()328 @click.option('--warnings/--no-warnings', '-w', default=True)329 def cli_alt(warnings):330 click.echo(warnings)331332 result = runner.invoke(cli_alt, ['--warnings'])333 assert result.output == 'True\n'334 result = runner.invoke(cli_alt, ['--no-warnings'])335 assert result.output == 'False\n'336 result = runner.invoke(cli_alt, ['-w'])337 assert result.output == 'True\n'

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption945b4d3

lines 338–347why, one step back
338
339@pytest.mark.parametrize('option_args,expected', [340 (['--aggressive', '--all', '-a'], 'aggressive'),341 (['--first', '--second', '--third', '-a', '-b', '-c'], 'first'),342 (['--apple', '--banana', '--cantaloupe', '-a', '-b', '-c'], 'apple'),343 (['--cantaloupe', '--banana', '--apple', '-c', '-b', '-a'], 'cantaloupe'),344 (['-a', '-b', '-c'], 'a'),345 (['-c', '-b', '-a'], 'c'),346 (['-a', '--apple', '-b', '--banana', '-c', '--cantaloupe'], 'apple'),347 (['-c', '-a', '--cantaloupe', '-b', '--banana', '--apple'], 'cantaloupe'),

Merge pull request #799 from ericfrederich/option_namesMarkus Unterwaditzerbefore adoptione634d7f

lines 348–349why, one step back
348 (['--from', '-f', '_from'], '_from'),
349 (['--return', '-r', '_ret'], '_ret'),

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption945b4d3

line 350why, one step back
350])

Merge pull request #799 from ericfrederich/option_namesMarkus Unterwaditzerbefore adoptione634d7f

lines 351–352why, one step back
351def test_option_names(runner, option_args, expected):
352

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption945b4d3

lines 353–360why, one step back
353 @click.command()
354 @click.option(*option_args, is_flag=True)355 def cmd(**kwargs):356 click.echo(str(kwargs[expected]))357358 assert cmd.params[0].name == expected359360 for form in option_args:

Merge pull request #799 from ericfrederich/option_namesMarkus Unterwaditzerbefore adoptione634d7f

lines 361–363why, one step back
361 if form.startswith('-'):
362 result = runner.invoke(cmd, [form])363 assert result.output == 'True\n'