Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to invoke args as string #664

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Version 7.0
- `secho`'s first argument can now be `None`, like in `echo`.
- Usage errors now hint at the `--help` option.
- ``launch`` now works properly under Cygwin. See #650.
- `CliRunner.invoke` now may receive `args` as a string representing
a Unix shell command. See #664.

Version 6.7
-----------
Expand Down
11 changes: 9 additions & 2 deletions click/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import shutil
import tempfile
import contextlib
import shlex

from ._compat import iteritems, PY2
from ._compat import iteritems, PY2, string_types


# If someone wants to vendor click, we want to ensure the
Expand Down Expand Up @@ -260,7 +261,10 @@ def invoke(self, cli, args=None, input=None, env=None,
The ``color`` parameter was added.

:param cli: the command to invoke
:param args: the arguments to invoke
:param args: the arguments to invoke. It may be given as an iterable
or a string. When given as string it will be interpreted
as a Unix shell command. More details at
:func:`shlex.split`.
:param input: the input data for `sys.stdin`.
:param env: the environment overrides.
:param catch_exceptions: Whether to catch any other exceptions than
Expand All @@ -274,6 +278,9 @@ def invoke(self, cli, args=None, input=None, env=None,
exception = None
exit_code = 0

if isinstance(args, string_types):
args = shlex.split(args)

try:
cli.main(args=args or (),
prog_name=self.get_default_prog_name(cli), **extra)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,23 @@ def cli_env():
assert result.output == 'ENV=some_value\n'

assert os.environ == env_orig


@pytest.mark.parametrize('args, expected_output', [
(None, 'bar\n'),
([], 'bar\n'),
('', 'bar\n'),
(['--foo', 'one two'], 'one two\n'),
('--foo "one two"', 'one two\n'),
])
def test_args(args, expected_output):

@click.command()
@click.option('--foo', default='bar')
def cli_args(foo):
click.echo(foo)

runner = CliRunner()
result = runner.invoke(cli_args, args=args)
assert result.exit_code == 0
assert result.output == expected_output