test_commands.py
This file
Path
tests/test_commands.py
316 lines · 8.2 KiB · Python
Last changed by
Merge pull request #1615 from MLH-Fellowship/issue1422David Lordbefore adoptionfb1ca91
History of this fileTake it
The walk stopped after 200 commits: lines older than 752ff79 point at it.
Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 1why, one step back1import reMerge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec
line 2why, one step back2Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 3–12why, one step back3import click456def test_other_command_invoke(runner):7 @click.command()8 @click.pass_context9 def cli(ctx):10 return ctx.invoke(other_cmd, arg=42)1112 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 13why, one step back13 @click.argument("arg", type=click.INT)Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 14–18why, one step back14 def other_cmd(arg):15 click.echo(arg)1617 result = runner.invoke(cli, [])18 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 19why, one step back19 assert result.output == "42\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 20–25why, one step back202122def test_other_command_forward(runner):23 cli = click.Group()2425 @cli.command()Merge branch '7.x'David Lordbefore adoption7239794
line 26why, one step back26 @click.option("--count", default=1)Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 27why, one step back27 def test(count):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 28why, one step back28 click.echo(f"Count: {count:d}")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 29–30why, one step back2930 @cli.command()Merge branch '7.x'David Lordbefore adoption7239794
line 31why, one step back31 @click.option("--count", default=1)Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 32–36why, one step back32 @click.pass_context33 def dist(ctx, count):34 ctx.forward(test)35 ctx.invoke(test, count=42)36Merge branch '7.x'David Lordbefore adoption7239794
line 37why, one step back37 result = runner.invoke(cli, ["dist"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 38why, one step back38 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 39why, one step back39 assert result.output == "Count: 1\nCount: 42\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 40–59why, one step back404142def test_auto_shorthelp(runner):43 @click.group()44 def cli():45 pass4647 @cli.command()48 def short():49 """This is a short text."""5051 @cli.command()52 def special_chars():53 """Login and store the token in ~/.netrc."""5455 @cli.command()56 def long():57 """This is a long text that is too long to show as short help58 and will be truncated instead."""59Merge branch '7.x'David Lordbefore adoption7239794
lines 60–71why, one step back60 result = runner.invoke(cli, ["--help"])61 assert (62 re.search(63 r"Commands:\n\s+"64 r"long\s+This is a long text that is too long to show as short help"65 r"\.\.\.\n\s+"66 r"short\s+This is a short text\.\n\s+"67 r"special-chars\s+Login and store the token in ~/.netrc\.\s*",68 result.output,69 )70 is not None71 )Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 72–73why, one step back7273Merge branch '7.x'David Lordbefore adoptiona438322
lines 74–80why, one step back74def test_no_args_is_help(runner):75 @click.command(no_args_is_help=True)76 def cli():77 pass7879 result = runner.invoke(cli, [])80 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 81why, one step back81 assert "Show this message and exit." in result.outputMerge branch '7.x'David Lordbefore adoptiona438322
lines 82–83why, one step back8283Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 84–89why, one step back84def test_default_maps(runner):85 @click.group()86 def cli():87 pass8889 @cli.command()Merge branch '7.x'David Lordbefore adoption7239794
line 90why, one step back90 @click.option("--name", default="normal")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 91–93why, one step back91 def foo(name):92 click.echo(name)93Merge branch '7.x'David Lordbefore adoption7239794
line 94why, one step back94 result = runner.invoke(cli, ["foo"], default_map={"foo": {"name": "changed"}})Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 95–96why, one step back9596 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 97why, one step back97 assert result.output == "changed\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 98–101why, one step back9899100def test_group_with_args(runner):101 @click.group()Merge branch '7.x'David Lordbefore adoption7239794
line 102why, one step back102 @click.argument("obj")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 103why, one step back103 def cli(obj):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 104why, one step back104 click.echo(f"obj={obj}")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 105–107why, one step back105106 @cli.command()107 def move():Merge branch '7.x'David Lordbefore adoption7239794
line 108why, one step back108 click.echo("move")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 109–111why, one step back109110 result = runner.invoke(cli, [])111 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 112why, one step back112 assert "Show this message and exit." in result.outputMerge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 113why, one step back113Merge branch '7.x'David Lordbefore adoption7239794
line 114why, one step back114 result = runner.invoke(cli, ["obj1"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 115why, one step back115 assert result.exit_code == 2Merge branch '7.x'David Lordbefore adoption7239794
line 116why, one step back116 assert "Error: Missing command." in result.outputMerge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 117why, one step back117Merge branch '7.x'David Lordbefore adoption7239794
line 118why, one step back118 result = runner.invoke(cli, ["obj1", "--help"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 119why, one step back119 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 120why, one step back120 assert "Show this message and exit." in result.outputMerge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 121why, one step back121Merge branch '7.x'David Lordbefore adoption7239794
line 122why, one step back122 result = runner.invoke(cli, ["obj1", "move"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 123why, one step back123 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 124why, one step back124 assert result.output == "obj=obj1\nmove\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 125–156why, one step back125126127def test_base_command(runner):128 import optparse129130 @click.group()131 def cli():132 pass133134 class OptParseCommand(click.BaseCommand):135 def __init__(self, name, parser, callback):136 click.BaseCommand.__init__(self, name)137 self.parser = parser138 self.callback = callback139140 def parse_args(self, ctx, args):141 try:142 opts, args = parser.parse_args(args)143 except Exception as e:144 ctx.fail(str(e))145 ctx.args = args146 ctx.params = vars(opts)147148 def get_usage(self, ctx):149 return self.parser.get_usage()150151 def get_help(self, ctx):152 return self.parser.format_help()153154 def invoke(self, ctx):155 ctx.invoke(self.callback, ctx.args, **ctx.params)156Merge branch '7.x'David Lordbefore adoption7239794
lines 157–168why, one step back157 parser = optparse.OptionParser(usage="Usage: foo test [OPTIONS]")158 parser.add_option(159 "-f", "--file", dest="filename", help="write report to FILE", metavar="FILE"160 )161 parser.add_option(162 "-q",163 "--quiet",164 action="store_false",165 dest="verbose",166 default=True,167 help="don't print status messages to stdout",168 )Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 169–170why, one step back169170 def test_callback(args, filename, verbose):Merge branch '7.x'David Lordbefore adoption7239794
line 171why, one step back171 click.echo(" ".join(args))Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 172–174why, one step back172 click.echo(filename)173 click.echo(verbose)174Merge branch '7.x'David Lordbefore adoption7239794
lines 175–179why, one step back175 cli.add_command(OptParseCommand("test", parser, test_callback))176177 result = runner.invoke(178 cli, ["test", "-f", "test.txt", "-q", "whatever.txt", "whateverelse.txt"]179 )Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 180–181why, one step back180 assert not result.exception181 assert result.output.splitlines() == [Merge branch '7.x'David Lordbefore adoption7239794
lines 182–184why, one step back182 "whatever.txt whateverelse.txt",183 "test.txt",184 "False",Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 185–186why, one step back185 ]186Merge branch '7.x'David Lordbefore adoption7239794
line 187why, one step back187 result = runner.invoke(cli, ["test", "--help"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 188–189why, one step back188 assert not result.exception189 assert result.output.splitlines() == [Merge branch '7.x'David Lordbefore adoption7239794
lines 190–195why, one step back190 "Usage: foo test [OPTIONS]",191 "",192 "Options:",193 " -h, --help show this help message and exit",194 " -f FILE, --file=FILE write report to FILE",195 " -q, --quiet don't print status messages to stdout",Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 196–200why, one step back196 ]197198199def test_object_propagation(runner):200 for chain in False, True:Merge branch '7.x'David Lordbefore adoption7239794
line 201why, one step back201Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 202why, one step back202 @click.group(chain=chain)Merge branch '7.x'David Lordbefore adoption7239794
line 203why, one step back203 @click.option("--debug/--no-debug", default=False)Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 204–207why, one step back204 @click.pass_context205 def cli(ctx, debug):206 if ctx.obj is None:207 ctx.obj = {}Merge branch '7.x'David Lordbefore adoption7239794
line 208why, one step back208 ctx.obj["DEBUG"] = debugMerge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 209–212why, one step back209210 @cli.command()211 @click.pass_context212 def sync(ctx):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 213why, one step back213 click.echo(f"Debug is {'on' if ctx.obj['DEBUG'] else 'off'}")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 214why, one step back214Merge branch '7.x'David Lordbefore adoption7239794
line 215why, one step back215 result = runner.invoke(cli, ["sync"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 216why, one step back216 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 217why, one step back217 assert result.output == "Debug is off\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 218–226why, one step back218219220def test_other_command_invoke_with_defaults(runner):221 @click.command()222 @click.pass_context223 def cli(ctx):224 return ctx.invoke(other_cmd)225226 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 227why, one step back227 @click.option("--foo", type=click.INT, default=42)Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 228–229why, one step back228 @click.pass_context229 def other_cmd(ctx, foo):Merge branch '7.x'David Lordbefore adoption7239794
line 230why, one step back230 assert ctx.info_name == "other-cmd"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 231–234why, one step back231 click.echo(foo)232233 result = runner.invoke(cli, [])234 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 235why, one step back235 assert result.output == "42\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 236–242why, one step back236237238def test_invoked_subcommand(runner):239 @click.group(invoke_without_command=True)240 @click.pass_context241 def cli(ctx):242 if ctx.invoked_subcommand is None:Merge branch '7.x'David Lordbefore adoption7239794
line 243why, one step back243 click.echo("no subcommand, use default")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 244–245why, one step back244 ctx.invoke(sync)245 else:Merge branch '7.x'David Lordbefore adoption7239794
line 246why, one step back246 click.echo("invoke subcommand")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 247–249why, one step back247248 @cli.command()249 def sync():Merge branch '7.x'David Lordbefore adoption7239794
line 250why, one step back250 click.echo("in subcommand")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 251why, one step back251Merge branch '7.x'David Lordbefore adoption7239794
line 252why, one step back252 result = runner.invoke(cli, ["sync"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 253why, one step back253 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 254why, one step back254 assert result.output == "invoke subcommand\nin subcommand\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 255–257why, one step back255256 result = runner.invoke(cli)257 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 258why, one step back258 assert result.output == "no subcommand, use default\nin subcommand\n"Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 259–260why, one step back259260Merge pull request #1615 from MLH-Fellowship/issue1422David Lordbefore adoptionfb1ca91
lines 261–276why, one step back261def test_aliased_command_canonical_name(runner):262 class AliasedGroup(click.Group):263 def get_command(self, ctx, cmd_name):264 return push265266 cli = AliasedGroup()267268 @cli.command()269 def push():270 click.echo("push command")271272 result = runner.invoke(cli, ["pu", "--help"])273 assert not result.exception274 assert result.output.startswith("Usage: root push [OPTIONS]")275276Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 277why, one step back277def test_unprocessed_options(runner):Merge branch '7.x'David Lordbefore adoption7239794
lines 278–280why, one step back278 @click.command(context_settings=dict(ignore_unknown_options=True))279 @click.argument("args", nargs=-1, type=click.UNPROCESSED)280 @click.option("--verbose", "-v", count=True)Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 281why, one step back281 def cli(verbose, args):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 282–283why, one step back282 click.echo(f"Verbosity: {verbose}")283 click.echo(f"Args: {'|'.join(args)}")Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 284why, one step back284Merge branch '7.x'David Lordbefore adoption7239794
line 285why, one step back285 result = runner.invoke(cli, ["-foo", "-vvvvx", "--muhaha", "x", "y", "-x"])Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
lines 286–287why, one step back286 assert not result.exception287 assert result.output.splitlines() == [Merge branch '7.x'David Lordbefore adoption7239794
lines 288–289why, one step back288 "Verbosity: 4",289 "Args: -foo|-x|--muhaha|x|y|-x",Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79
line 290why, one step back290 ]Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e
lines 291–292why, one step back291292Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255
lines 293–296why, one step back293def test_deprecated_in_help_messages(runner):294 @click.command(deprecated=True)295 def cmd_with_help():296 """CLI HELP"""Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e
lines 297–298why, one step back297 pass298Merge branch '7.x'David Lordbefore adoption7239794
lines 299–300why, one step back299 result = runner.invoke(cmd_with_help, ["--help"])300 assert "(DEPRECATED)" in result.outputUnderscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e
line 301why, one step back301Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255
lines 302–303why, one step back302 @click.command(deprecated=True)303 def cmd_without_help():Merge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec
lines 304–305why, one step back304 pass305Merge branch '7.x'David Lordbefore adoption7239794
lines 306–307why, one step back306 result = runner.invoke(cmd_without_help, ["--help"])307 assert "(DEPRECATED)" in result.outputMerge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec
lines 308–309why, one step back308309Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255
lines 310–312why, one step back310def test_deprecated_in_invocation(runner):311 @click.command(deprecated=True)312 def deprecated_cmd():Merge branch '7.x'David Lordbefore adoption7239794
line 313why, one step back313 passMerge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec
line 314why, one step back314Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255
line 315why, one step back315 result = runner.invoke(deprecated_cmd)Merge branch '7.x'David Lordbefore adoption7239794
line 316why, one step back316 assert "DeprecationWarning:" in result.output