Skip to content
Myr

test_basic.py

This file

Path

tests/test_basic.py

339 lines · 9.9 KiB · Python

Last changed by

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

History of this file

Take it

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

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

Added tests for lazy flagsArmin Ronacherbefore adoption85e4667

line 2why, one step back
2import os

Added UUID type supportArmin Ronacherbefore adoption4ed961d

line 3why, one step back
3import uuid

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 4–13why, one step back
4import click
567def test_basic_functionality(runner):8 @click.command()9 def cli():10 """Hello World!"""11 click.echo('I EXECUTED')1213 result = runner.invoke(cli, ['--help'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 15–20why, one step back
15 assert 'Hello World!' in result.output
16 assert 'Show this message and exit.' in result.output17 assert result.exit_code == 018 assert 'I EXECUTED' not in result.output1920 result = runner.invoke(cli, [])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 22–37why, one step back
22 assert 'I EXECUTED' in result.output
23 assert result.exit_code == 0242526def test_basic_group(runner):27 @click.group()28 def cli():29 """This is the root."""30 click.echo('ROOT EXECUTED')3132 @cli.command()33 def subcommand():34 """This is a subcommand."""35 click.echo('SUBCOMMAND EXECUTED')3637 result = runner.invoke(cli, ['--help'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 39–44why, one step back
39 assert 'This is the root' in result.output
40 assert 'This is a subcommand.' in result.output41 assert result.exit_code == 042 assert 'ROOT EXECUTED' not in result.output4344 result = runner.invoke(cli, ['subcommand'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 46–57why, one step back
46 assert result.exit_code == 0
47 assert 'ROOT EXECUTED' in result.output48 assert 'SUBCOMMAND EXECUTED' in result.output495051def test_basic_option(runner):52 @click.command()53 @click.option('--foo', default='no value')54 def cli(foo):55 click.echo('FOO:[%s]' % foo)5657 result = runner.invoke(cli, [])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 59–61why, one step back
59 assert 'FOO:[no value]' in result.output
6061 result = runner.invoke(cli, ['--foo=42'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 63–65why, one step back
63 assert 'FOO:[42]' in result.output
6465 result = runner.invoke(cli, ['--foo'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

line 66why, one step back
66 assert result.exception

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 67–69why, one step back
67 assert '--foo option requires an argument' in result.output
6869 result = runner.invoke(cli, ['--foo='])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 71–73why, one step back
71 assert 'FOO:[]' in result.output
7273 result = runner.invoke(cli, [u'--foo=\N{SNOWMAN}'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

line 75why, one step back
75 assert u'FOO:[\N{SNOWMAN}]' in result.output

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 76–94why, one step back
76
7778def test_int_option(runner):79 @click.command()80 @click.option('--foo', default=42)81 def cli(foo):82 click.echo('FOO:[%s]' % (foo * 2))8384 result = runner.invoke(cli, [])85 assert not result.exception86 assert 'FOO:[84]' in result.output8788 result = runner.invoke(cli, ['--foo=23'])89 assert not result.exception90 assert 'FOO:[46]' in result.output9192 result = runner.invoke(cli, ['--foo=bar'])93 assert result.exception94 assert 'Invalid value for "--foo": bar is not a valid integer' \

Added UUID type supportArmin Ronacherbefore adoption4ed961d

lines 95–116why, one step back
95 in result.output
969798def test_uuid_option(runner):99 @click.command()100 @click.option('--u', default='ba122011-349f-423b-873b-9d6a79c688ab',101 type=click.UUID)102 def cli(u):103 assert type(u) is uuid.UUID104 click.echo('U:[%s]' % u)105106 result = runner.invoke(cli, [])107 assert not result.exception108 assert 'U:[ba122011-349f-423b-873b-9d6a79c688ab]' in result.output109110 result = runner.invoke(cli, ['--u=821592c1-c50e-4971-9cd6-e89dc6832f86'])111 assert not result.exception112 assert 'U:[821592c1-c50e-4971-9cd6-e89dc6832f86]' in result.output113114 result = runner.invoke(cli, ['--u=bar'])115 assert result.exception116 assert 'Invalid value for "--u": bar is not a valid UUID value' \

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 117–173why, one step back
117 in result.output
118119120def test_float_option(runner):121 @click.command()122 @click.option('--foo', default=42, type=click.FLOAT)123 def cli(foo):124 assert type(foo) is float125 click.echo('FOO:[%s]' % foo)126127 result = runner.invoke(cli, [])128 assert not result.exception129 assert 'FOO:[42.0]' in result.output130131 result = runner.invoke(cli, ['--foo=23.5'])132 assert not result.exception133 assert 'FOO:[23.5]' in result.output134135 result = runner.invoke(cli, ['--foo=bar'])136 assert result.exception137 assert 'Invalid value for "--foo": bar is not a valid float' \138 in result.output139140141def test_boolean_option(runner):142 for default in True, False:143 @click.command()144 @click.option('--with-foo/--without-foo', default=default)145 def cli(with_foo):146 click.echo(with_foo)147148 result = runner.invoke(cli, ['--with-foo'])149 assert not result.exception150 assert result.output == 'True\n'151 result = runner.invoke(cli, ['--without-foo'])152 assert not result.exception153 assert result.output == 'False\n'154 result = runner.invoke(cli, [])155 assert not result.exception156 assert result.output == '%s\n' % default157158 for default in True, False:159 @click.command()160 @click.option('--flag', is_flag=True, default=default)161 def cli(flag):162 click.echo(flag)163164 result = runner.invoke(cli, ['--flag'])165 assert not result.exception166 assert result.output == '%s\n' % (not default)167 result = runner.invoke(cli, [])168 assert not result.exception169 assert result.output == '%s\n' % (default)170171172def test_file_option(runner):173 @click.command()

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 174why, one step back
174 @click.option('--file', type=click.File('w'))

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 175–178why, one step back
175 def input(file):
176 file.write('Hello World!\n')177178 @click.command()

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 179why, one step back
179 @click.option('--file', type=click.File('r'))

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 180–190why, one step back
180 def output(file):
181 click.echo(file.read())182183 with runner.isolated_filesystem():184 result_in = runner.invoke(input, ['--file=example.txt'])185 result_out = runner.invoke(output, ['--file=example.txt'])186187 assert not result_in.exception188 assert result_in.output == ''189 assert not result_out.exception190 assert result_out.output == 'Hello World!\n\n'

Added tests for lazy flagsArmin Ronacherbefore adoption85e4667

lines 191–231why, one step back
191
192193def test_file_lazy_mode(runner):194 do_io = False195196 @click.command()197 @click.option('--file', type=click.File('w'))198 def input(file):199 if do_io:200 file.write('Hello World!\n')201202 @click.command()203 @click.option('--file', type=click.File('r'))204 def output(file):205 pass206207 with runner.isolated_filesystem():208 os.mkdir('example.txt')209210 do_io = True211 result_in = runner.invoke(input, ['--file=example.txt'])212 assert result_in.exit_code == 1213214 do_io = False215 result_in = runner.invoke(input, ['--file=example.txt'])216 assert result_in.exit_code == 0217218 result_out = runner.invoke(output, ['--file=example.txt'])219 assert result_out.exception220221 @click.command()222 @click.option('--file', type=click.File('w', lazy=False))223 def input_non_lazy(file):224 file.write('Hello World!\n')225226 with runner.isolated_filesystem():227 os.mkdir('example.txt')228 result_in = runner.invoke(input_non_lazy, ['--file=example.txt'])229 assert result_in.exit_code == 2230 assert 'Invalid value for "--file": Could not open file: example.txt' \231 in result_in.output

Added tests for choice typeArmin Ronacherbefore adoption4a8cf56

lines 232–247why, one step back
232
233234def test_choice_option(runner):235 @click.command()236 @click.option('--method', type=click.Choice(['foo', 'bar', 'baz']))237 def cli(method):238 click.echo(method)239240 result = runner.invoke(cli, ['--method=foo'])241 assert not result.exception242 assert result.output == 'foo\n'243244 result = runner.invoke(cli, ['--method=meh'])245 assert result.exit_code == 2246 assert 'Invalid value for "--method": invalid choice: meh. ' \247 '(choose from foo, bar, baz)' in result.output

Added metavars for choice testArmin Ronacherbefore adoptione67212b

lines 248–250why, one step back
248
249 result = runner.invoke(cli, ['--help'])250 assert '--method [foo|bar|baz]' in result.output

Added test for requiredArmin Ronacherbefore adoptiona6f9401

lines 251–252why, one step back
251
252

Added count support and int rangesArmin Ronacherbefore adoption81930b5

lines 253–285why, one step back
253def test_int_range_option(runner):
254 @click.command()255 @click.option('--x', type=click.IntRange(0, 5))256 def cli(x):257 click.echo(x)258259 result = runner.invoke(cli, ['--x=5'])260 assert not result.exception261 assert result.output == '5\n'262263 result = runner.invoke(cli, ['--x=6'])264 assert result.exit_code == 2265 assert 'Invalid value for "--x": 6 is not in the valid range of 0 to 5.\n' \266 in result.output267268 @click.command()269 @click.option('--x', type=click.IntRange(0, 5, clamp=True))270 def clamp(x):271 click.echo(x)272273 result = runner.invoke(clamp, ['--x=5'])274 assert not result.exception275 assert result.output == '5\n'276277 result = runner.invoke(clamp, ['--x=6'])278 assert not result.exception279 assert result.output == '5\n'280281 result = runner.invoke(clamp, ['--x=-1'])282 assert not result.exception283 assert result.output == '0\n'284285

Added test for requiredArmin Ronacherbefore adoptiona6f9401

lines 286–294why, one step back
286def test_required_option(runner):
287 @click.command()288 @click.option('--foo', required=True)289 def cli(foo):290 click.echo(foo)291292 result = runner.invoke(cli, [])293 assert result.exit_code == 2294 assert 'Missing option "--foo"' in result.output

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 295–339why, one step back
295
296297def test_evaluation_order(runner):298 called = []299300 def memo(ctx, value):301 called.append(value)302 return value303304 @click.command()305 @click.option('--missing', default='missing',306 is_eager=False, callback=memo)307 @click.option('--eager-flag1', flag_value='eager1',308 is_eager=True, callback=memo)309 @click.option('--eager-flag2', flag_value='eager2',310 is_eager=True, callback=memo)311 @click.option('--eager-flag3', flag_value='eager3',312 is_eager=True, callback=memo)313 @click.option('--normal-flag1', flag_value='normal1',314 is_eager=False, callback=memo)315 @click.option('--normal-flag2', flag_value='normal2',316 is_eager=False, callback=memo)317 @click.option('--normal-flag3', flag_value='normal3',318 is_eager=False, callback=memo)319 def cli(**x):320 pass321322 result = runner.invoke(cli, ['--eager-flag2',323 '--eager-flag1',324 '--normal-flag2',325 '--eager-flag3',326 '--normal-flag3',327 '--normal-flag3',328 '--normal-flag1',329 '--normal-flag1'])330 assert not result.exception331 assert called == [332 'eager2',333 'eager1',334 'eager3',335 'normal2',336 'normal3',337 'normal1',338 'missing',339 ]