Add support to invoke args as string
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Fabio Menegazzo · committed by Fabio Menegazzo · parent 702acf7 · Browse files · 3 files·+31 added−2 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+31 added−2 removed
- tests:+20 added−0 removed
- other:+11 added−2 removed
Provenance
- Commit
0759c87- Tree
55a03da- Author
Fabio Menegazzo <fabio.menegazzo@axado.com.br>- Committer
Fabio Menegazzo <fabio.menegazzo@axado.com.br>- Parent
702acf7
3 files changed+31 added−2 removed
modified:click/testing.py+9 added-2 removed
1 unmodified line2 import sys3 import shutil4 import tempfile5 import contextlib6+import shlex7 7-from ._compat import iteritems, PY28+from ._compat import iteritems, PY2, string_types9 10 11 # If someone wants to vendor click, we want to ensure the12 # correct package is discovered. Ideally we could use a247 unmodified lines260 .. versionadded:: 4.0261 The ``color`` parameter was added.262 263 :param cli: the command to invoke263- :param args: the arguments to invoke264+ :param args: the arguments to invoke. It may be given as an iterable265+ or a string. When given as string it will be interpreted266+ as a Unix shell command. More details at267+ :func:`shlex.split`.268 :param input: the input data for `sys.stdin`.269 :param env: the environment overrides.270 :param catch_exceptions: Whether to catch any other exceptions than271 ``SystemExit``.5 unmodified lines277 with self.isolation(input=input, env=env, color=color) as out:278 exception = None279 exit_code = 0280 281+ if isinstance(args, string_types):282+ args = shlex.split(args)283+284 try:285 cli.main(args=args or (),286 prog_name=self.get_default_prog_name(cli), **extra)287 except SystemExit as e:42 unmodified linesmodified:tests/test_testing.py+20 added
200 unmodified lines201 assert result.exit_code == 0202 assert result.output == 'ENV=some_value\n'203 204 assert os.environ == env_orig205+206+207+@pytest.mark.parametrize('args, expected_output', [208+ (None, 'bar\n'),209+ ([], 'bar\n'),210+ ('', 'bar\n'),211+ (['--foo', 'one two'], 'one two\n'),212+ ('--foo "one two"', 'one two\n'),213+])214+def test_args(args, expected_output):215+216+ @click.command()217+ @click.option('--foo', default='bar')218+ def cli_args(foo):219+ click.echo(foo)220+221+ runner = CliRunner()222+ result = runner.invoke(cli_args, args=args)223+ assert result.exit_code == 0224+ assert result.output == expected_outputmodified:CHANGES+2 added
12 unmodified lines13 - Added Float Range in Types.14 - `secho`'s first argument can now be `None`, like in `echo`.15 - Usage errors now hint at the `--help` option.16 - ``launch`` now works properly under Cygwin. See #650.17+- `CliRunner.invoke` now may receive `args` as a string representing18+ a Unix shell command. See #664.19 20 Version 6.721 -----------22 341 unmodified lines