non-chained group invoked without command invokes result callback
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by IamCathal · committed by David Lord · parent 2815455 · Browse files · 3 files·+28 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−12 removed
- tests:+19 added−0 removed
- other:+9 added−12 removed
Provenance
- Commit
32ddbb1- Tree
c002785- Author
IamCathal <cathalsocallaghan@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
2815455
3 files changed+28 added−12 removed
modified:src/click/core.py+7 added-12 removed
1168 unmodified lines1169 Command.format_options(self, ctx, formatter)1170 self.format_commands(ctx, formatter)1171 1172 def resultcallback(self, replace=False):1173- """Adds a result callback to the chain command. By default if a1173+ """Adds a result callback to the command. By default if a1174 result callback is already registered this will chain them but1175 this can be disabled with the `replace` parameter. The result1176 callback is invoked with the return value of the subcommand1177 (or the list of return values from all subcommands if chaining10 unmodified lines1188 @cli.resultcallback()1189 def process_result(result, input):1190 return result + input1191 1192- .. versionadded:: 3.01193-1192 :param replace: if set to `True` an already existing result1193 callback will be removed.1194+1195+ .. versionadded:: 3.01196 """1197 1198 def decorator(f):1199 old_callback = self.result_callback57 unmodified lines1257 value = ctx.invoke(self.result_callback, value, **ctx.params)1258 return value1259 1260 if not ctx.protected_args:1261- # If we are invoked without command the chain flag controls1262- # how this happens. If we are not in chain mode, the return1263- # value here is the return value of the command.1264- # If however we are in chain mode, the return value is the1265- # return value of the result processor invoked with an empty1266- # list (which means that no subcommand actually was executed).1261 if self.invoke_without_command:1268- if not self.chain:1269- return Command.invoke(self, ctx)1262+ # No subcommand was invoked, so the result callback is1263+ # invoked with None for regular groups, or an empty list1264+ # for chained groups.1265 with ctx:1266 Command.invoke(self, ctx)1272- return _process_result([])1267+ return _process_result([] if self.chain else None)1268 ctx.fail("Missing command.")1269 1270 # Fetch args back out1271 args = ctx.protected_args + ctx.args803 unmodified linesmodified:tests/test_chain.py+19 added
86 unmodified lines87 assert not result.exception88 assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]89 90 91+@pytest.mark.parametrize(("chain", "expect"), [(False, "None"), (True, "[]")])92+def test_no_command_result_callback(runner, chain, expect):93+ """When a group has ``invoke_without_command=True``, the result94+ callback is always invoked. A regular group invokes it with95+ ``None``, a chained group with ``[]``.96+ """97+98+ @click.group(invoke_without_command=True, chain=chain)99+ def cli():100+ pass101+102+ @cli.resultcallback()103+ def process_result(result):104+ click.echo(str(result), nl=False)105+106+ result = runner.invoke(cli, [])107+ assert result.output == expect108+109+110 def test_chaining_with_arguments(runner):111 @click.group(chain=True)112 def cli():113 pass135 unmodified linesmodified:CHANGES.rst+2 added
27 unmodified lines28 matched name. This makes behavior such as help text and29 ``Context.invoked_subcommand`` consistent when using patterns like30 ``AliasedGroup``. :issue:`1422`31 - The ``BOOL`` type accepts the values "on" and "off". :issue:`1629`32+- A ``Group`` with ``invoke_without_command=True`` will always invoke33+ its result callback. :issue:`1178`34 35 36 Version 7.1.237 -------------635 unmodified lines