test_bashcomplete.py
This file
Path
tests/test_bashcomplete.py
243 lines · 9.3 KiB · Python
Last changed by
Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
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–243why, one step back113114115def test_argument_choice():116 @click.command()117 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))118 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))119 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))120 def cli():121 pass122123 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']124 assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12']125 assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']126 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument']127 assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']128129130def test_option_choice():131 @click.command()132 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))133 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))134 @click.option('--opt3', type=click.Choice(['opt', 'option']))135 def cli():136 pass137138 assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3']139 assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3']140 assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12']141 assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22']142 assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22']143 assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22']144 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']145 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']146 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22']147 assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3']148 assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3']149 assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12']150 assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option']151152 assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']153154155def test_option_and_arg_choice():156 @click.command()157 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))158 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))159 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))160 def cli():161 pass162163 assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']164 assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12']165 assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']166 assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']167168169def test_boolean_flag_choice():170 @click.command()171 @click.option('--shout/--no-shout', default=False)172 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))173 def cli(local_opt):174 pass175176 assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']177 assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2']178179180def test_multi_value_option_choice():181 @click.command()182 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))183 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))184 def cli(local_opt):185 pass186187 assert list(get_choices(cli, 'lol', ['--pos'], '')) == ['pos1', 'pos2']188 assert list(get_choices(cli, 'lol', ['--pos', 'pos1'], '')) == ['pos1', 'pos2']189 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2'], '')) == ['arg1', 'arg2']190 assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2', 'arg1'], '')) == []191192193def test_multi_option_choice():194 @click.command()195 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))196 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))197 def cli(local_opt):198 pass199200 assert list(get_choices(cli, 'lol', ['-m'], '')) == ['m1', 'm2']201 assert list(get_choices(cli, 'lol', ['-m', 'm1', '-m'], '')) == ['m1', 'm2']202 assert list(get_choices(cli, 'lol', ['-m', 'm1'], '')) == ['arg1', 'arg2']203204205def test_variadic_argument_choice():206 @click.command()207 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))208 def cli(local_opt):209 pass210211 assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']212213214def test_long_chain_choice():215 @click.group()216 def cli():217 pass218219 @cli.group('sub')220 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))221 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))222 def sub(sub_opt):223 pass224225 @sub.command('bsub')226 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))227 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))228 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))229 def bsub(bsub_opt):230 pass231232 assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2']233 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2']234 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \235 ['subarg1', 'subarg2']236 assert list(get_choices(cli, 'lol',237 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt']238 assert list(get_choices(cli, 'lol',239 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \240 ['bsubopt1', 'bsubopt2']241 assert list(get_choices(cli, 'lol',242 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],243 '')) == ['bbsubarg1', 'bbsubarg2']