Skip to content
Myr

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 file

Take 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 back
1import re

Merge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec

line 2why, one step back
2

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 3–12why, one step back
3import click
456def 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 back
13 @click.argument("arg", type=click.INT)

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 14–18why, one step back
14 def other_cmd(arg):
15 click.echo(arg)1617 result = runner.invoke(cli, [])18 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 19why, one step back
19 assert result.output == "42\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 20–25why, one step back
20
2122def test_other_command_forward(runner):23 cli = click.Group()2425 @cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 26why, one step back
26 @click.option("--count", default=1)

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 27why, one step back
27 def test(count):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 28why, one step back
28 click.echo(f"Count: {count:d}")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 29–30why, one step back
29
30 @cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 31why, one step back
31 @click.option("--count", default=1)

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 32–36why, one step back
32 @click.pass_context
33 def dist(ctx, count):34 ctx.forward(test)35 ctx.invoke(test, count=42)36

Merge branch '7.x'David Lordbefore adoption7239794

line 37why, one step back
37 result = runner.invoke(cli, ["dist"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 38why, one step back
38 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 39why, one step back
39 assert result.output == "Count: 1\nCount: 42\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 40–59why, one step back
40
4142def 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."""59

Merge branch '7.x'David Lordbefore adoption7239794

lines 60–71why, one step back
60 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 back
72
73

Merge branch '7.x'David Lordbefore adoptiona438322

lines 74–80why, one step back
74def 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 == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 81why, one step back
81 assert "Show this message and exit." in result.output

Merge branch '7.x'David Lordbefore adoptiona438322

lines 82–83why, one step back
82
83

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 84–89why, one step back
84def 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 back
90 @click.option("--name", default="normal")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 91–93why, one step back
91 def foo(name):
92 click.echo(name)93

Merge branch '7.x'David Lordbefore adoption7239794

line 94why, one step back
94 result = runner.invoke(cli, ["foo"], default_map={"foo": {"name": "changed"}})

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 95–96why, one step back
95
96 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 97why, one step back
97 assert result.output == "changed\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 98–101why, one step back
98
99100def test_group_with_args(runner):101 @click.group()

Merge branch '7.x'David Lordbefore adoption7239794

line 102why, one step back
102 @click.argument("obj")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 103why, one step back
103 def cli(obj):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 104why, one step back
104 click.echo(f"obj={obj}")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 105–107why, one step back
105
106 @cli.command()107 def move():

Merge branch '7.x'David Lordbefore adoption7239794

line 108why, one step back
108 click.echo("move")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 109–111why, one step back
109
110 result = runner.invoke(cli, [])111 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 112why, one step back
112 assert "Show this message and exit." in result.output

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 113why, one step back
113

Merge branch '7.x'David Lordbefore adoption7239794

line 114why, one step back
114 result = runner.invoke(cli, ["obj1"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 115why, one step back
115 assert result.exit_code == 2

Merge branch '7.x'David Lordbefore adoption7239794

line 116why, one step back
116 assert "Error: Missing command." in result.output

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 117why, one step back
117

Merge branch '7.x'David Lordbefore adoption7239794

line 118why, one step back
118 result = runner.invoke(cli, ["obj1", "--help"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 119why, one step back
119 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 120why, one step back
120 assert "Show this message and exit." in result.output

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 121why, one step back
121

Merge branch '7.x'David Lordbefore adoption7239794

line 122why, one step back
122 result = runner.invoke(cli, ["obj1", "move"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 123why, one step back
123 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 124why, one step back
124 assert result.output == "obj=obj1\nmove\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 125–156why, one step back
125
126127def 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)156

Merge branch '7.x'David Lordbefore adoption7239794

lines 157–168why, one step back
157 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 back
169
170 def test_callback(args, filename, verbose):

Merge branch '7.x'David Lordbefore adoption7239794

line 171why, one step back
171 click.echo(" ".join(args))

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 172–174why, one step back
172 click.echo(filename)
173 click.echo(verbose)174

Merge branch '7.x'David Lordbefore adoption7239794

lines 175–179why, one step back
175 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 back
180 assert not result.exception
181 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 182–184why, one step back
182 "whatever.txt whateverelse.txt",
183 "test.txt",184 "False",

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 185–186why, one step back
185 ]
186

Merge branch '7.x'David Lordbefore adoption7239794

line 187why, one step back
187 result = runner.invoke(cli, ["test", "--help"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 188–189why, one step back
188 assert not result.exception
189 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 190–195why, one step back
190 "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 back
196 ]
197198199def test_object_propagation(runner):200 for chain in False, True:

Merge branch '7.x'David Lordbefore adoption7239794

line 201why, one step back
201

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 202why, one step back
202 @click.group(chain=chain)

Merge branch '7.x'David Lordbefore adoption7239794

line 203why, one step back
203 @click.option("--debug/--no-debug", default=False)

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 204–207why, one step back
204 @click.pass_context
205 def cli(ctx, debug):206 if ctx.obj is None:207 ctx.obj = {}

Merge branch '7.x'David Lordbefore adoption7239794

line 208why, one step back
208 ctx.obj["DEBUG"] = debug

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 209–212why, one step back
209
210 @cli.command()211 @click.pass_context212 def sync(ctx):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 213why, one step back
213 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 back
214

Merge branch '7.x'David Lordbefore adoption7239794

line 215why, one step back
215 result = runner.invoke(cli, ["sync"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 216why, one step back
216 assert result.exception is None

Merge branch '7.x'David Lordbefore adoption7239794

line 217why, one step back
217 assert result.output == "Debug is off\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 218–226why, one step back
218
219220def 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 back
227 @click.option("--foo", type=click.INT, default=42)

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 228–229why, one step back
228 @click.pass_context
229 def other_cmd(ctx, foo):

Merge branch '7.x'David Lordbefore adoption7239794

line 230why, one step back
230 assert ctx.info_name == "other-cmd"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 231–234why, one step back
231 click.echo(foo)
232233 result = runner.invoke(cli, [])234 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 235why, one step back
235 assert result.output == "42\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 236–242why, one step back
236
237238def 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 back
243 click.echo("no subcommand, use default")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 244–245why, one step back
244 ctx.invoke(sync)
245 else:

Merge branch '7.x'David Lordbefore adoption7239794

line 246why, one step back
246 click.echo("invoke subcommand")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 247–249why, one step back
247
248 @cli.command()249 def sync():

Merge branch '7.x'David Lordbefore adoption7239794

line 250why, one step back
250 click.echo("in subcommand")

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 251why, one step back
251

Merge branch '7.x'David Lordbefore adoption7239794

line 252why, one step back
252 result = runner.invoke(cli, ["sync"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 253why, one step back
253 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 254why, one step back
254 assert result.output == "invoke subcommand\nin subcommand\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 255–257why, one step back
255
256 result = runner.invoke(cli)257 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 258why, one step back
258 assert result.output == "no subcommand, use default\nin subcommand\n"

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 259–260why, one step back
259
260

Merge pull request #1615 from MLH-Fellowship/issue1422David Lordbefore adoptionfb1ca91

lines 261–276why, one step back
261def 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]")275276

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 277why, one step back
277def test_unprocessed_options(runner):

Merge branch '7.x'David Lordbefore adoption7239794

lines 278–280why, one step back
278 @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 back
281 def cli(verbose, args):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

lines 282–283why, one step back
282 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 back
284

Merge branch '7.x'David Lordbefore adoption7239794

line 285why, one step back
285 result = runner.invoke(cli, ["-foo", "-vvvvx", "--muhaha", "x", "y", "-x"])

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

lines 286–287why, one step back
286 assert not result.exception
287 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 288–289why, one step back
288 "Verbosity: 4",
289 "Args: -foo|-x|--muhaha|x|y|-x",

Merge pull request #842 from axu2/masterMarkus Unterwaditzerbefore adoption752ff79

line 290why, one step back
290 ]

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

lines 291–292why, one step back
291
292

Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255

lines 293–296why, one step back
293def 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 back
297 pass
298

Merge branch '7.x'David Lordbefore adoption7239794

lines 299–300why, one step back
299 result = runner.invoke(cmd_with_help, ["--help"])
300 assert "(DEPRECATED)" in result.output

Underscores are now replaced by dashes in commandsArmin Ronacherbefore adoption5d1df0e

line 301why, one step back
301

Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255

lines 302–303why, one step back
302 @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 back
304 pass
305

Merge branch '7.x'David Lordbefore adoption7239794

lines 306–307why, one step back
306 result = runner.invoke(cmd_without_help, ["--help"])
307 assert "(DEPRECATED)" in result.output

Merge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec

lines 308–309why, one step back
308
309

Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255

lines 310–312why, one step back
310def 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 back
313 pass

Merge pull request #874 from rgreinho/ISSUE-873/variable-precedenceZachery Birbefore adoption9c456ec

line 314why, one step back
314

Merge pull request #1005 from pallets/deprecated-flagLokiHokiebefore adoption1dee255

line 315why, one step back
315 result = runner.invoke(deprecated_cmd)

Merge branch '7.x'David Lordbefore adoption7239794

line 316why, one step back
316 assert "DeprecationWarning:" in result.output