Skip to content

Commit

Permalink
Add support to invoke args as string
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Menegazzo committed Sep 28, 2016
1 parent 702acf7 commit 80127d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 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,7 @@ 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. May be an iterable or string.
: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 +275,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

0 comments on commit 80127d9

Please sign in to comment.