Skip to content
Myr

test_bashcomplete.py

This file

Path

tests/test_bashcomplete.py

270 lines · 10.3 KiB · Python

Last changed by

fix overzealous completion when required options are presentNicholas Wilesbefore adoption65da5cc

History of this file

Take it

bashcomplete: Adapt to changes for chain commandsMarkus Unterwaditzerbefore adoption37011e7

lines 1–5why, one step back
1# -*- coding: utf-8 -*-
23import click4from click._bashcomplete import get_choices5

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

lines 6–16why, one step back
6
7def test_single_command():8 @click.command()9 @click.option('--local-opt')10 def cli(local_opt):11 pass1213 assert list(get_choices(cli, 'lol', [], '-')) == ['--local-opt']14 assert list(get_choices(cli, 'lol', [], '')) == []1516

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 17–52why, one step back
17def test_boolean_flag():
18 @click.command()19 @click.option('--shout/--no-shout', default=False)20 def cli(local_opt):21 pass2223 assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']242526def test_multi_value_option():27 @click.group()28 @click.option('--pos', nargs=2, type=float)29 def cli(local_opt):30 pass3132 @cli.command()33 @click.option('--local-opt')34 def sub(local_opt):35 pass3637 assert list(get_choices(cli, 'lol', [], '-')) == ['--pos']38 assert list(get_choices(cli, 'lol', ['--pos'], '')) == []39 assert list(get_choices(cli, 'lol', ['--pos', '1.0'], '')) == []40 assert list(get_choices(cli, 'lol', ['--pos', '1.0', '1.0'], '')) == ['sub']414243def test_multi_option():44 @click.command()45 @click.option('--message', '-m', multiple=True)46 def cli(local_opt):47 pass4849 assert list(get_choices(cli, 'lol', [], '-')) == ['--message', '-m']50 assert list(get_choices(cli, 'lol', ['-m'], '')) == []5152

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

line 53why, one step back
53def test_small_chain():

bashcomplete: Adapt to changes for chain commandsMarkus Unterwaditzerbefore adoption37011e7

lines 54–67why, one step back
54 @click.group()
55 @click.option('--global-opt')56 def cli(global_opt):57 pass5859 @cli.command()60 @click.option('--local-opt')61 def sub(local_opt):62 pass6364 assert list(get_choices(cli, 'lol', [], '')) == ['sub']65 assert list(get_choices(cli, 'lol', [], '-')) == ['--global-opt']66 assert list(get_choices(cli, 'lol', ['sub'], '')) == []67 assert list(get_choices(cli, 'lol', ['sub'], '-')) == ['--local-opt']

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

lines 68–85why, one step back
68
6970def test_long_chain():71 @click.group('cli')72 @click.option('--cli-opt')73 def cli(cli_opt):74 pass7576 @cli.group('asub')77 @click.option('--asub-opt')78 def asub(asub_opt):79 pass8081 @asub.group('bsub')82 @click.option('--bsub-opt')83 def bsub(bsub_opt):84 pass85

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 86–92why, one step back
86 COLORS = ['red', 'green', 'blue']
87 def get_colors(ctx, args, incomplete):88 for c in COLORS:89 yield c9091 CSUB_OPT_CHOICES = ['foo', 'bar']92 CSUB_CHOICES = ['bar', 'baz']

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

line 93why, one step back
93 @bsub.command('csub')

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 94–97why, one step back
94 @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES))
95 @click.option('--csub', type=click.Choice(CSUB_CHOICES))96 @click.argument('color', autocompletion=get_colors)97 def csub(csub_opt, color):

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

lines 98–105why, one step back
98 pass
99100 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']101 assert list(get_choices(cli, 'lol', [], '')) == ['asub']102 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']103 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']104 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']105 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub']

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 106–112why, one step back
106 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt', '--csub']
107 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], '')) == CSUB_OPT_CHOICES108 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '--csub')) == ['--csub-opt', '--csub']109 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub'], '')) == CSUB_CHOICES110 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], 'f')) == ['foo']111 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == COLORS112 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], 'b')) == ['blue']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 113–114why, one step back
113
114

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 115–127why, one step back
115def test_chaining():
116 @click.group('cli', chain=True)117 @click.option('--cli-opt')118 def cli(cli_opt):119 pass120121 @cli.command('asub')122 @click.option('--asub-opt')123 def asub(asub_opt):124 pass125126 @cli.command('bsub')127 @click.option('--bsub-opt')

fix overzealous completion when required options are presentNicholas Wilesbefore adoption65da5cc

line 128why, one step back
128 @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 129–135why, one step back
129 def bsub(bsub_opt, arg):
130 pass131132 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']133 assert list(get_choices(cli, 'lol', [], '')) == ['asub', 'bsub']134 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']135 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']

fix overzealous completion when required options are presentNicholas Wilesbefore adoption65da5cc

lines 136–137why, one step back
136 assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['arg1', 'arg2']
137 assert list(get_choices(cli, 'lol', ['asub', '--asub-opt'], '')) == []

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 138–141why, one step back
138 assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']
139 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']140141

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 142–258why, one step back
142def test_argument_choice():
143 @click.command()144 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))145 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))146 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))147 def cli():148 pass149150 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']151 assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12']152 assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']153 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument']154 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']155156157def test_option_choice():158 @click.command()159 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))160 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))161 @click.option('--opt3', type=click.Choice(['opt', 'option']))162 def cli():163 pass164165 assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3']166 assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3']167 assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12']168 assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22']169 assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22']170 assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22']171 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']172 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']173 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22']174 assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3']175 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3']176 assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12']177 assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option']178179 assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']180181182def test_option_and_arg_choice():183 @click.command()184 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))185 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))186 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))187 def cli():188 pass189190 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']191 assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12']192 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']193 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']194195196def test_boolean_flag_choice():197 @click.command()198 @click.option('--shout/--no-shout', default=False)199 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))200 def cli(local_opt):201 pass202203 assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']204 assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2']205206207def test_multi_value_option_choice():208 @click.command()209 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))210 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))211 def cli(local_opt):212 pass213214 assert list(get_choices(cli, 'lol', ['--pos'], '')) == ['pos1', 'pos2']215 assert list(get_choices(cli, 'lol', ['--pos', 'pos1'], '')) == ['pos1', 'pos2']216 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2'], '')) == ['arg1', 'arg2']217 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2', 'arg1'], '')) == []218219220def test_multi_option_choice():221 @click.command()222 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))223 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))224 def cli(local_opt):225 pass226227 assert list(get_choices(cli, 'lol', ['-m'], '')) == ['m1', 'm2']228 assert list(get_choices(cli, 'lol', ['-m', 'm1', '-m'], '')) == ['m1', 'm2']229 assert list(get_choices(cli, 'lol', ['-m', 'm1'], '')) == ['arg1', 'arg2']230231232def test_variadic_argument_choice():233 @click.command()234 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))235 def cli(local_opt):236 pass237238 assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']239240241def test_long_chain_choice():242 @click.group()243 def cli():244 pass245246 @cli.group('sub')247 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))248 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))249 def sub(sub_opt):250 pass251252 @sub.command('bsub')253 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))254 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))255 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))256 def bsub(bsub_opt):257 pass258

fix overzealous completion when required options are presentNicholas Wilesbefore adoption65da5cc

line 259why, one step back
259 assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2', 'bsub']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 260–261why, one step back
260 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2']
261 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \

fix overzealous completion when required options are presentNicholas Wilesbefore adoption65da5cc

line 262why, one step back
262 ['subarg1', 'subarg2', 'bsub']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 263–270why, one step back
263 assert list(get_choices(cli, 'lol',
264 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt']265 assert list(get_choices(cli, 'lol',266 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \267 ['bsubopt1', 'bsubopt2']268 assert list(get_choices(cli, 'lol',269 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],270 '')) == ['bbsubarg1', 'bbsubarg2']