Skip to content
Myr

test_bashcomplete.py

This file

Path

tests/test_bashcomplete.py

286 lines · 11.1 KiB · Python

Last changed by

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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–88why, one step back
86 COLORS = ['red', 'green', 'blue']
87 def get_colors(ctx, args, incomplete):88 for c in COLORS:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 89–90why, one step back
89 if c.startswith(incomplete):
90 yield c

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

line 91why, one step back
91

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 92–96why, one step back
92 def search_colors(ctx, args, incomplete):
93 for c in COLORS:94 if incomplete in c:95 yield c96

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

lines 97–98why, one step back
97 CSUB_OPT_CHOICES = ['foo', 'bar']
98 CSUB_CHOICES = ['bar', 'baz']

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

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

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

lines 100–101why, one step back
100 @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES))
101 @click.option('--csub', type=click.Choice(CSUB_CHOICES))

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 102why, one step back
102 @click.option('--search-color', autocompletion=search_colors)

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

lines 103–104why, one step back
103 @click.argument('color', autocompletion=get_colors)
104 def csub(csub_opt, color):

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

lines 105–112why, one step back
105 pass
106107 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']108 assert list(get_choices(cli, 'lol', [], '')) == ['asub']109 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']110 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']111 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']112 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub']

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 113why, one step back
113 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt', '--csub', '--search-color']

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

lines 114–119why, one step back
114 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], '')) == CSUB_OPT_CHOICES
115 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '--csub')) == ['--csub-opt', '--csub']116 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub'], '')) == CSUB_CHOICES117 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], 'f')) == ['foo']118 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == COLORS119 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], 'b')) == ['blue']

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 120why, one step back
120 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--search-color'], 'een')) == ['green']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 121–122why, one step back
121
122

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

lines 123–135why, one step back
123def test_chaining():
124 @click.group('cli', chain=True)125 @click.option('--cli-opt')126 def cli(cli_opt):127 pass128129 @cli.command('asub')130 @click.option('--asub-opt')131 def asub(asub_opt):132 pass133134 @cli.command('bsub')135 @click.option('--bsub-opt')

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

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

lines 137–139why, one step back
137 def bsub(bsub_opt, arg):
138 pass139

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 140–145why, one step back
140 @cli.command('csub')
141 @click.option('--csub-opt')142 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False)143 def csub(csub_opt, arg):144 pass145

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

line 146why, one step back
146 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 147why, one step back
147 assert list(get_choices(cli, 'lol', [], '')) == ['asub', 'bsub', 'csub']

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

line 148why, one step back
148 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

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

lines 152–153why, one step back
152 assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']
153 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 154–155why, one step back
154 assert list(get_choices(cli, 'lol', ['asub', 'csub'], '')) == ['carg1', 'carg2', 'bsub']
155 assert list(get_choices(cli, 'lol', ['asub', 'csub'], '-')) == ['--csub-opt']

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

lines 156–157why, one step back
156
157

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 158–274why, one step back
158def test_argument_choice():
159 @click.command()160 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))161 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))162 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))163 def cli():164 pass165166 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']167 assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12']168 assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']169 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument']170 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']171172173def test_option_choice():174 @click.command()175 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))176 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))177 @click.option('--opt3', type=click.Choice(['opt', 'option']))178 def cli():179 pass180181 assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3']182 assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3']183 assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12']184 assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22']185 assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22']186 assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22']187 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']188 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']189 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22']190 assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3']191 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3']192 assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12']193 assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option']194195 assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']196197198def test_option_and_arg_choice():199 @click.command()200 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))201 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))202 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))203 def cli():204 pass205206 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']207 assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12']208 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']209 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']210211212def test_boolean_flag_choice():213 @click.command()214 @click.option('--shout/--no-shout', default=False)215 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))216 def cli(local_opt):217 pass218219 assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']220 assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2']221222223def test_multi_value_option_choice():224 @click.command()225 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))226 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))227 def cli(local_opt):228 pass229230 assert list(get_choices(cli, 'lol', ['--pos'], '')) == ['pos1', 'pos2']231 assert list(get_choices(cli, 'lol', ['--pos', 'pos1'], '')) == ['pos1', 'pos2']232 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2'], '')) == ['arg1', 'arg2']233 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2', 'arg1'], '')) == []234235236def test_multi_option_choice():237 @click.command()238 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))239 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))240 def cli(local_opt):241 pass242243 assert list(get_choices(cli, 'lol', ['-m'], '')) == ['m1', 'm2']244 assert list(get_choices(cli, 'lol', ['-m', 'm1', '-m'], '')) == ['m1', 'm2']245 assert list(get_choices(cli, 'lol', ['-m', 'm1'], '')) == ['arg1', 'arg2']246247248def test_variadic_argument_choice():249 @click.command()250 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))251 def cli(local_opt):252 pass253254 assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']255256257def test_long_chain_choice():258 @click.group()259 def cli():260 pass261262 @cli.group('sub')263 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))264 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))265 def sub(sub_opt):266 pass267268 @sub.command('bsub')269 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))270 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))271 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))272 def bsub(bsub_opt):273 pass274

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 279–286why, one step back
279 assert list(get_choices(cli, 'lol',
280 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt']281 assert list(get_choices(cli, 'lol',282 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \283 ['bsubopt1', 'bsubopt2']284 assert list(get_choices(cli, 'lol',285 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],286 '')) == ['bbsubarg1', 'bbsubarg2']