test_basic.py
This file
Path
tests/test_basic.py
388 lines · 11.5 KiB · Python
Last changed by
Fixed lack of clear reporting for pathsArmin Ronacherbefore adoption39b5df2
History of this fileTake it
The walk stopped after 200 commits: lines older than 052ee21 point at it.
Removed some more dead codeArmin Ronacherbefore adoption052ee21
line 1why, one step back1# -*- coding: utf-8 -*-Added tests for lazy flagsArmin Ronacherbefore adoption85e4667
line 2why, one step back2import osAdded UUID type supportArmin Ronacherbefore adoption4ed961d
line 3why, one step back3import uuidRemoved some more dead codeArmin Ronacherbefore adoption052ee21
lines 4–94why, one step back4import click567def test_basic_functionality(runner):8 @click.command()9 def cli():10 """Hello World!"""11 click.echo('I EXECUTED')1213 result = runner.invoke(cli, ['--help'])14 assert not result.exception15 assert 'Hello World!' in result.output16 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, [])21 assert not result.exception22 assert 'I EXECUTED' in result.output23 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'])38 assert not result.exception39 assert 'This is the root' in result.output40 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'])45 assert not result.exception46 assert result.exit_code == 047 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, [])58 assert not result.exception59 assert 'FOO:[no value]' in result.output6061 result = runner.invoke(cli, ['--foo=42'])62 assert not result.exception63 assert 'FOO:[42]' in result.output6465 result = runner.invoke(cli, ['--foo'])66 assert result.exception67 assert '--foo option requires an argument' in result.output6869 result = runner.invoke(cli, ['--foo='])70 assert not result.exception71 assert 'FOO:[]' in result.output7273 result = runner.invoke(cli, [u'--foo=\N{SNOWMAN}'])74 assert not result.exception75 assert u'FOO:[\N{SNOWMAN}]' in result.output767778def 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 back95 in result.output969798def 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' \Removed some more dead codeArmin Ronacherbefore adoption052ee21
lines 117–190why, one step back117 in result.output118119120def 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()174 @click.option('--file', type=click.File('w'))175 def input(file):176 file.write('Hello World!\n')177178 @click.command()179 @click.option('--file', type=click.File('r'))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 back191192193def 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.outputAdded tests for choice typeArmin Ronacherbefore adoption4a8cf56
lines 232–233why, one step back232233Implemented the Path typeArmin Ronacherbefore adoption2e7c39c
lines 234–251why, one step back234def test_path_option(runner):235 @click.command()236 @click.option('-O', type=click.Path(file_okay=False, exists=True,237 writable=True))238 def write_to_dir(o):239 with open(os.path.join(o, 'foo.txt'), 'wb') as f:240 f.write(b'meh\n')241242 with runner.isolated_filesystem():243 os.mkdir('test')244245 result = runner.invoke(write_to_dir, ['-O', 'test'])246 assert not result.exception247248 with open('test/foo.txt', 'rb') as f:249 assert f.read() == b'meh\n'250251 result = runner.invoke(write_to_dir, ['-O', 'test/foo.txt'])Fixed lack of clear reporting for pathsArmin Ronacherbefore adoption39b5df2
line 252why, one step back252 assert 'Invalid value for "-O": Directory "test/foo.txt" is a file.' \Implemented the Path typeArmin Ronacherbefore adoption2e7c39c
lines 253–262why, one step back253 in result.output254255 @click.command()256 @click.option('-f', type=click.Path(exists=True))257 def showtype(f):258 click.echo('is_file=%s' % os.path.isfile(f))259 click.echo('is_dir=%s' % os.path.isdir(f))260261 with runner.isolated_filesystem():262 result = runner.invoke(showtype, ['-f', 'xxx'])Fixed lack of clear reporting for pathsArmin Ronacherbefore adoption39b5df2
line 263why, one step back263 assert 'Error: Invalid value for "-f": Path "xxx" does not exist' \Implemented the Path typeArmin Ronacherbefore adoption2e7c39c
lines 264–282why, one step back264 in result.output265266 result = runner.invoke(showtype, ['-f', '.'])267 assert 'is_file=False' in result.output268 assert 'is_dir=True' in result.output269270 @click.command()271 @click.option('-f', type=click.Path())272 def exists(f):273 click.echo('exists=%s' % os.path.exists(f))274275 with runner.isolated_filesystem():276 result = runner.invoke(exists, ['-f', 'xxx'])277 assert 'exists=False' in result.output278279 result = runner.invoke(exists, ['-f', '.'])280 assert 'exists=True' in result.output281282Added tests for choice typeArmin Ronacherbefore adoption4a8cf56
lines 283–296why, one step back283def test_choice_option(runner):284 @click.command()285 @click.option('--method', type=click.Choice(['foo', 'bar', 'baz']))286 def cli(method):287 click.echo(method)288289 result = runner.invoke(cli, ['--method=foo'])290 assert not result.exception291 assert result.output == 'foo\n'292293 result = runner.invoke(cli, ['--method=meh'])294 assert result.exit_code == 2295 assert 'Invalid value for "--method": invalid choice: meh. ' \296 '(choose from foo, bar, baz)' in result.outputAdded metavars for choice testArmin Ronacherbefore adoptione67212b
lines 297–299why, one step back297298 result = runner.invoke(cli, ['--help'])299 assert '--method [foo|bar|baz]' in result.outputAdded test for requiredArmin Ronacherbefore adoptiona6f9401
lines 300–301why, one step back300301Added count support and int rangesArmin Ronacherbefore adoption81930b5
lines 302–334why, one step back302def test_int_range_option(runner):303 @click.command()304 @click.option('--x', type=click.IntRange(0, 5))305 def cli(x):306 click.echo(x)307308 result = runner.invoke(cli, ['--x=5'])309 assert not result.exception310 assert result.output == '5\n'311312 result = runner.invoke(cli, ['--x=6'])313 assert result.exit_code == 2314 assert 'Invalid value for "--x": 6 is not in the valid range of 0 to 5.\n' \315 in result.output316317 @click.command()318 @click.option('--x', type=click.IntRange(0, 5, clamp=True))319 def clamp(x):320 click.echo(x)321322 result = runner.invoke(clamp, ['--x=5'])323 assert not result.exception324 assert result.output == '5\n'325326 result = runner.invoke(clamp, ['--x=6'])327 assert not result.exception328 assert result.output == '5\n'329330 result = runner.invoke(clamp, ['--x=-1'])331 assert not result.exception332 assert result.output == '0\n'333334Added test for requiredArmin Ronacherbefore adoptiona6f9401
lines 335–343why, one step back335def test_required_option(runner):336 @click.command()337 @click.option('--foo', required=True)338 def cli(foo):339 click.echo(foo)340341 result = runner.invoke(cli, [])342 assert result.exit_code == 2343 assert 'Missing option "--foo"' in result.outputAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 344–388why, one step back344345346def test_evaluation_order(runner):347 called = []348349 def memo(ctx, value):350 called.append(value)351 return value352353 @click.command()354 @click.option('--missing', default='missing',355 is_eager=False, callback=memo)356 @click.option('--eager-flag1', flag_value='eager1',357 is_eager=True, callback=memo)358 @click.option('--eager-flag2', flag_value='eager2',359 is_eager=True, callback=memo)360 @click.option('--eager-flag3', flag_value='eager3',361 is_eager=True, callback=memo)362 @click.option('--normal-flag1', flag_value='normal1',363 is_eager=False, callback=memo)364 @click.option('--normal-flag2', flag_value='normal2',365 is_eager=False, callback=memo)366 @click.option('--normal-flag3', flag_value='normal3',367 is_eager=False, callback=memo)368 def cli(**x):369 pass370371 result = runner.invoke(cli, ['--eager-flag2',372 '--eager-flag1',373 '--normal-flag2',374 '--eager-flag3',375 '--normal-flag3',376 '--normal-flag3',377 '--normal-flag1',378 '--normal-flag1'])379 assert not result.exception380 assert called == [381 'eager2',382 'eager1',383 'eager3',384 'normal2',385 'normal3',386 'normal1',387 'missing',388 ]