Skip to content
Myr

test_options.py

This file

Path

tests/test_options.py

203 lines · 5.9 KiB · Python

Last changed by

Add test for #146Rotem Yaaribefore adoptionf982c31

History of this file

Take it

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

Updated repo script a bitArmin Ronacherbefore adoptionca1dff9

lines 1–2why, one step back
1# -*- coding: utf-8 -*-
2import re

Added support for lists from envvars. This fixes #87Armin Ronacherbefore adoption817ef38

line 3why, one step back
3import os

Updated repo script a bitArmin Ronacherbefore adoptionca1dff9

line 4why, one step back
4import click

Add test for #146Rotem Yaaribefore adoptionf982c31

line 5why, one step back
5import pytest

Updated repo script a bitArmin Ronacherbefore adoptionca1dff9

lines 6–39why, 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 None222324def test_counting(runner):25 @click.command()26 @click.option('-v', count=True, help='Verbosity',27 type=click.IntRange(0, 3))28 def cli(v):29 click.echo('verbosity=%d' % v)3031 result = runner.invoke(cli, ['-vvv'])32 assert not result.exception33 assert result.output == 'verbosity=3\n'3435 result = runner.invoke(cli, ['-vvvv'])36 assert result.exception37 assert 'Invalid value for "-v": 4 is not in the valid range of 0 to 3.' \38 in result.output39

Default count to 0Armin Ronacherbefore adoption848b664

lines 40–42why, one step back
40 result = runner.invoke(cli, [])
41 assert not result.exception42 assert result.output == 'verbosity=0\n'

Add test for #146Rotem Yaaribefore adoptionf982c31

lines 43–53why, one step back
43
4445@pytest.mark.parametrize('unknown_flag', ['--foo', '-f'])46def test_unknown_options(runner, unknown_flag):47 @click.command()48 def cli():49 pass5051 result = runner.invoke(cli, [unknown_flag])52 assert result.exception53 assert 'no such option: {0}'.format(unknown_flag) in result.output

Default count to 0Armin Ronacherbefore adoption848b664

line 54why, one step back
54

Updated repo script a bitArmin Ronacherbefore adoptionca1dff9

lines 55–68why, one step back
55
56def test_multiple_required(runner):57 @click.command()58 @click.option('-m', '--message', multiple=True, required=True)59 def cli(message):60 click.echo('\n'.join(message))6162 result = runner.invoke(cli, ['-m', 'foo', '-mbar'])63 assert not result.exception64 assert result.output == 'foo\nbar\n'6566 result = runner.invoke(cli, [])67 assert result.exception68 assert 'Error: Missing option "-m" / "--message".' in result.output

Added support for lists from envvars. This fixes #87Armin Ronacherbefore adoption817ef38

lines 69–82why, one step back
69
7071def test_multiple_envvar(runner):72 @click.command()73 @click.option('--arg', multiple=True)74 def cmd(arg):75 click.echo('|'.join(arg))7677 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',78 env={'TEST_ARG': 'foo bar baz'})79 assert not result.exception80 assert result.output == 'foo|bar|baz\n'8182 @click.command()

Apply the same splitting logic for explicit envvarsArmin Ronacherbefore adoption5105e6b

lines 83–91why, one step back
83 @click.option('--arg', multiple=True, envvar='X')
84 def cmd(arg):85 click.echo('|'.join(arg))8687 result = runner.invoke(cmd, [], env={'X': 'foo bar baz'})88 assert not result.exception89 assert result.output == 'foo|bar|baz\n'9091 @click.command()

Added support for lists from envvars. This fixes #87Armin Ronacherbefore adoption817ef38

lines 92–99why, one step back
92 @click.option('--arg', multiple=True, type=click.Path())
93 def cmd(arg):94 click.echo('|'.join(arg))9596 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',97 env={'TEST_ARG': 'foo%sbar' % os.path.pathsep})98 assert not result.exception99 assert result.output == 'foo|bar\n'

Also added support for nargs != for values from environment.Armin Ronacherbefore adoptioneeb1378

lines 100–122why, one step back
100
101102def test_nargs_envvar(runner):103 @click.command()104 @click.option('--arg', nargs=2)105 def cmd(arg):106 click.echo('|'.join(arg))107108 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',109 env={'TEST_ARG': 'foo bar'})110 assert not result.exception111 assert result.output == 'foo|bar\n'112113 @click.command()114 @click.option('--arg', nargs=2, multiple=True)115 def cmd(arg):116 for item in arg:117 click.echo('|'.join(item))118119 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',120 env={'TEST_ARG': 'x 1 y 2'})121 assert not result.exception122 assert result.output == 'x|1\ny|2\n'

Added support for BadParameter exceptions.Armin Ronacherbefore adoption1407ecb

lines 123–141why, one step back
123
124125def test_custom_validation(runner):126 def validate_pos_int(ctx, value):127 if value < 0:128 raise click.BadParameter('Value needs to be positive')129 return value130131 @click.command()132 @click.option('--foo', callback=validate_pos_int, default=1)133 def cmd(foo):134 click.echo(foo)135136 result = runner.invoke(cmd, ['--foo', '-1'])137 assert 'Invalid value for "--foo": Value needs to be positive' \138 in result.output139140 result = runner.invoke(cmd, ['--foo', '42'])141 assert result.output == '42\n'

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

lines 142–144why, one step back
142
143144def test_winstyle_options(runner):

User interface improvements.Armin Ronacherbefore adoptionc2e187b

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

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

lines 146–149why, one step back
146 @click.option('/debug;/no-debug', help='Enables or disables debug mode.')
147 def cmd(debug):148 click.echo(debug)149

User interface improvements.Armin Ronacherbefore adoptionc2e187b

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

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

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

User interface improvements.Armin Ronacherbefore adoptionc2e187b

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

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

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

User interface improvements.Armin Ronacherbefore adoptionc2e187b

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

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

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

User interface improvements.Armin Ronacherbefore adoptionc2e187b

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

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

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

User interface improvements.Armin Ronacherbefore adoptionc2e187b

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

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

lines 159–170why, one step back
159
160161def test_legacy_options(runner):162 @click.command()163 @click.option('-whatever')164 def cmd(whatever):165 click.echo(whatever)166167 result = runner.invoke(cmd, ['-whatever', '42'])168 assert result.output == '42\n'169 result = runner.invoke(cmd, ['-whatever=23'])170 assert result.output == '23\n'

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

lines 171–183why, one step back
171
172173def test_missing_choice(runner):174 @click.command()175 @click.option('--foo', type=click.Choice(['foo', 'bar']),176 required=True)177 def cmd(foo):178 click.echo(foo)179180 result = runner.invoke(cmd)181 assert result.exit_code == 2182 assert 'Error: Missing option "--foo". Choose from foo, bar.' \183 in result.output

Merge pull request #123 from untitaker/issue122Armin Ronacherbefore adoptiond914ad6

lines 184–203why, one step back
184
185186def test_multiline_help(runner):187 @click.command()188 @click.option('--foo', help="""189 hello190191 i am192193 multiline194 """)195 def cmd(foo):196 click.echo(foo)197198 result = runner.invoke(cmd, ['--help'])199 assert result.exit_code == 0200 out = result.output.splitlines()201 assert ' --foo TEXT hello' in out202 assert ' i am' in out203 assert ' multiline' in out