diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-10-09 20:04:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-09 20:04:50 -0400 |
commit | f38e100fd77f4a136a4883d23b2f4f8b3cd934b7 (patch) | |
tree | c289c216807646567953191d35ebdc5c07198c24 /tests | |
parent | 467be57e647112f536becc8625ffa080cb67a0ce (diff) | |
parent | 84f290bfdd82eb1c2eaf26b5936f7088b4911f2c (diff) | |
download | cmd2-git-f38e100fd77f4a136a4883d23b2f4f8b3cd934b7.tar.gz |
Merge pull request #571 from python-cmd2/argparse_remainder
Fixes related to handling of argparse.REMAINDER
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_acargparse.py | 17 | ||||
-rw-r--r-- | tests/test_autocompletion.py | 2 | ||||
-rw-r--r-- | tests/test_completion.py | 24 | ||||
-rw-r--r-- | tests/test_pyscript.py | 19 | ||||
-rw-r--r-- | tests/test_utils.py | 12 |
5 files changed, 71 insertions, 3 deletions
diff --git a/tests/test_acargparse.py b/tests/test_acargparse.py index be3e8b97..617afd4f 100644 --- a/tests/test_acargparse.py +++ b/tests/test_acargparse.py @@ -5,7 +5,7 @@ Copyright 2018 Eric Lin <anselor@gmail.com> Released under MIT license, see LICENSE file """ import pytest -from cmd2.argparse_completer import ACArgumentParser +from cmd2.argparse_completer import ACArgumentParser, token_resembles_flag def test_acarg_narg_empty_tuple(): @@ -51,3 +51,18 @@ def test_acarg_narg_tuple_zero_base(): def test_acarg_narg_tuple_zero_to_one(): parser = ACArgumentParser(prog='test') parser.add_argument('tuple', nargs=(0, 1)) + + +def test_token_resembles_flag(): + parser = ACArgumentParser() + + # Not valid flags + assert not token_resembles_flag('', parser) + assert not token_resembles_flag('non-flag', parser) + assert not token_resembles_flag('-', parser) + assert not token_resembles_flag('--has space', parser) + assert not token_resembles_flag('-2', parser) + + # Valid flags + assert token_resembles_flag('-flag', parser) + assert token_resembles_flag('--flag', parser) diff --git a/tests/test_autocompletion.py b/tests/test_autocompletion.py index c6c1d1f6..3473ab38 100644 --- a/tests/test_autocompletion.py +++ b/tests/test_autocompletion.py @@ -36,7 +36,7 @@ optional arguments: MEDIA_MOVIES_ADD_HELP = '''Usage: media movies add -d DIRECTOR{1..2} [-h] - title {G, PG, PG-13, R, NC-17} [actor [...]] + title {G, PG, PG-13, R, NC-17} ... positional arguments: title Movie Title diff --git a/tests/test_completion.py b/tests/test_completion.py index d722e534..ed36eb01 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -716,6 +716,30 @@ def test_add_opening_quote_delimited_space_in_prefix(cmd2_app): os.path.commonprefix(cmd2_app.completion_matches) == expected_common_prefix and \ cmd2_app.display_matches == expected_display +def test_argparse_remainder_completion(cmd2_app): + # First test a positional with nargs=argparse.REMAINDER + text = '--h' + line = 'help command subcommand {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + # --h should not complete into --help because we are in the argparse.REMAINDER sections + assert complete_tester(text, line, begidx, endidx, cmd2_app) is None + + # Now test a flag with nargs=argparse.REMAINDER + parser = argparse.ArgumentParser() + parser.add_argument('-f', nargs=argparse.REMAINDER) + + # Overwrite eof's parser for this test + cmd2.Cmd.do_eof.argparser = parser + + text = '--h' + line = 'eof -f {}'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + # --h should not complete into --help because we are in the argparse.REMAINDER sections + assert complete_tester(text, line, begidx, endidx, cmd2_app) is None @pytest.fixture def sc_app(): diff --git a/tests/test_pyscript.py b/tests/test_pyscript.py index 6b72e940..bcb72a3b 100644 --- a/tests/test_pyscript.py +++ b/tests/test_pyscript.py @@ -9,7 +9,7 @@ import os import pytest from cmd2.cmd2 import Cmd, with_argparser from cmd2 import argparse_completer -from .conftest import run_cmd +from .conftest import run_cmd, normalize from cmd2.utils import namedtuple_with_defaults, StdSim @@ -234,3 +234,20 @@ def test_pyscript_custom_name(ps_echo, request): out = run_cmd(ps_echo, 'pyscript {}'.format(python_script)) assert out assert message == out[0] + + +def test_pyscript_argparse_checks(ps_app, capsys): + # Test command that has nargs.REMAINDER and make sure all tokens are accepted + run_cmd(ps_app, 'py app.alias.create("my_alias", "alias_command", "command_arg1", "command_arg2")') + out = run_cmd(ps_app, 'alias list my_alias') + assert out == normalize('alias create my_alias alias_command command_arg1 command_arg2') + + # Specify flag outside of keyword argument + run_cmd(ps_app, 'py app.help("-h")') + _, err = capsys.readouterr() + assert '-h appears to be a flag' in err + + # Specify list with flag outside of keyword argument + run_cmd(ps_app, 'py app.help(["--help"])') + _, err = capsys.readouterr() + assert '--help appears to be a flag' in err diff --git a/tests/test_utils.py b/tests/test_utils.py index 43a05a9a..807bc0fd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -119,11 +119,23 @@ def stdout_sim(): stdsim = cu.StdSim(sys.stdout) return stdsim +@pytest.fixture +def stringio_sim(): + import io + stdsim = cu.StdSim(io.StringIO(), echo=True) + return stdsim + + def test_stdsim_write_str(stdout_sim): my_str = 'Hello World' stdout_sim.write(my_str) assert stdout_sim.getvalue() == my_str +def test_stdsim_write_str_inner_no_buffer(stringio_sim): + my_str = 'Hello World' + stringio_sim.write(my_str) + assert stringio_sim.getvalue() == my_str + def test_stdsim_write_bytes(stdout_sim): b_str = b'Hello World' with pytest.raises(TypeError): |