test_bashcomplete.py
This file
Path
tests/test_bashcomplete.py
269 lines · 10.3 KiB · Python
Last changed by
Fix bashautocompletion of arguments on chained commandsNicholas Wilesbefore adoption92c8053
History of this fileTake it
bashcomplete: Adapt to changes for chain commandsMarkus Unterwaditzerbefore adoption37011e7
lines 1–5why, one step back1# -*- coding: utf-8 -*-23import click4from click._bashcomplete import get_choices5Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 6–16why, one step back67def 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', [], '')) == []1516Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 17–52why, one step back17def 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'], '')) == []5152Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 53why, one step back53def test_small_chain():bashcomplete: Adapt to changes for chain commandsMarkus Unterwaditzerbefore adoption37011e7
lines 54–67why, one step back54 @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 back686970def 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 pass85Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 86–92why, one step back86 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 back93 @bsub.command('csub')Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 94–97why, one step back94 @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 back98 pass99100 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 back106 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 back113114Fix bashautocompletion of arguments on chained commandsNicholas Wilesbefore adoption92c8053
lines 115–140why, one step back115def 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')128 @click.argument('arg', type=click.Choice(['arg1', 'arg2']))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']136 assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['arg1', 'arg2', 'asub']137 assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']138 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']139140Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 141–269why, one step back141def test_argument_choice():142 @click.command()143 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))144 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))145 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))146 def cli():147 pass148149 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']150 assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12']151 assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']152 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument']153 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']154155156def test_option_choice():157 @click.command()158 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))159 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))160 @click.option('--opt3', type=click.Choice(['opt', 'option']))161 def cli():162 pass163164 assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3']165 assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3']166 assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12']167 assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22']168 assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22']169 assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22']170 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']171 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']172 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22']173 assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3']174 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3']175 assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12']176 assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option']177178 assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']179180181def test_option_and_arg_choice():182 @click.command()183 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))184 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))185 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))186 def cli():187 pass188189 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']190 assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12']191 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']192 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']193194195def test_boolean_flag_choice():196 @click.command()197 @click.option('--shout/--no-shout', default=False)198 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))199 def cli(local_opt):200 pass201202 assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']203 assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2']204205206def test_multi_value_option_choice():207 @click.command()208 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))209 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))210 def cli(local_opt):211 pass212213 assert list(get_choices(cli, 'lol', ['--pos'], '')) == ['pos1', 'pos2']214 assert list(get_choices(cli, 'lol', ['--pos', 'pos1'], '')) == ['pos1', 'pos2']215 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2'], '')) == ['arg1', 'arg2']216 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2', 'arg1'], '')) == []217218219def test_multi_option_choice():220 @click.command()221 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))222 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))223 def cli(local_opt):224 pass225226 assert list(get_choices(cli, 'lol', ['-m'], '')) == ['m1', 'm2']227 assert list(get_choices(cli, 'lol', ['-m', 'm1', '-m'], '')) == ['m1', 'm2']228 assert list(get_choices(cli, 'lol', ['-m', 'm1'], '')) == ['arg1', 'arg2']229230231def test_variadic_argument_choice():232 @click.command()233 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))234 def cli(local_opt):235 pass236237 assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']238239240def test_long_chain_choice():241 @click.group()242 def cli():243 pass244245 @cli.group('sub')246 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))247 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))248 def sub(sub_opt):249 pass250251 @sub.command('bsub')252 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))253 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))254 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))255 def bsub(bsub_opt):256 pass257258 assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2']259 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2']260 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \261 ['subarg1', 'subarg2']262 assert list(get_choices(cli, 'lol',263 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt']264 assert list(get_choices(cli, 'lol',265 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \266 ['bsubopt1', 'bsubopt2']267 assert list(get_choices(cli, 'lol',268 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],269 '')) == ['bbsubarg1', 'bbsubarg2']