summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-20 12:59:39 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-20 12:59:39 -0500
commit4046a6968405dce820168913dbfe3690c446dac9 (patch)
treea10b79f8609c952df2dd5c021c575215c3115b31
parent136de7e22fa04ed41fc37b8a6c900cf507db8f26 (diff)
downloadcmd2-git-4046a6968405dce820168913dbfe3690c446dac9.tar.gz
Fixed unit tests
Updated unit tests due to changes in how help is output for commands decorated with an argparse ArgumentParser.
-rw-r--r--CHANGELOG.md2
-rw-r--r--tests/conftest.py1
-rw-r--r--tests/test_argparse.py18
-rw-r--r--tests/test_cmd2.py34
4 files changed, 36 insertions, 19 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c017ff36..a79ba192 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,8 @@
* **do_*** commands get two arguments, the output of argparse.parse_known_args()
* See the **Argument Processing** section of the documentation for more information on these decorators
* Alternatively, see the **argparse_example.py** and **arg_print.py** examples
+ * Added support for Argpasre sub-commands when using the **with_argument_parser** or **with_argparser_and_unknown_args** decorators
+ * See [subcommands.py](https://github.com/python-cmd2/cmd2/blob/master/examples/subcommands.py) for an example of how to use subcommands
* The **__relative_load** command is now hidden from the help menu by default
* This command is not intended to be called from the command line, only from within scripts
* The **set** command now has an additional **-a/--all** option to also display read-only settings
diff --git a/tests/conftest.py b/tests/conftest.py
index 021af193..030172a1 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -37,7 +37,6 @@ optional arguments:
-o FILE, --output-file FILE
output to file
-s, --script script format; no separation lines
-
"""
# Output from the shortcuts command with default built-in shortcuts
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 21e81603..733e741b 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -138,20 +138,26 @@ def test_argparse_quoted_arguments_posix_multiple(argparse_app):
out = run_cmd(argparse_app, 'tag strong this "should be" loud')
assert out == ['<strong>this should be loud</strong>']
-def test_argparse_help_docstring(argparse_app):
- out = run_cmd(argparse_app, 'help say')
+def test_argparse_help_docstring(argparse_app, capsys):
+ run_cmd(argparse_app, 'help say')
+ out, err = capsys.readouterr()
+ out = out.splitlines()
assert out[0].startswith('usage: say')
assert out[1] == ''
assert out[2] == 'Repeat what you tell me to.'
-def test_argparse_help_description(argparse_app):
- out = run_cmd(argparse_app, 'help tag')
+def test_argparse_help_description(argparse_app, capsys):
+ run_cmd(argparse_app, 'help tag')
+ out, err = capsys.readouterr()
+ out = out.splitlines()
assert out[0].startswith('usage: tag')
assert out[1] == ''
assert out[2] == 'create a html tag'
-def test_argparse_prog(argparse_app):
- out = run_cmd(argparse_app, 'help tag')
+def test_argparse_prog(argparse_app, capsys):
+ run_cmd(argparse_app, 'help tag')
+ out, err = capsys.readouterr()
+ out = out.splitlines()
progname = out[0].split(' ')[1]
assert progname == 'tag'
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 30308dd7..91fc0e61 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -39,21 +39,30 @@ def test_base_help(base_app):
assert out == expected
-def test_base_help_history(base_app):
- out = run_cmd(base_app, 'help history')
- expected = normalize(HELP_HISTORY)
- assert out == expected
+def test_base_help_history(base_app, capsys):
+ run_cmd(base_app, 'help history')
+ out, err = capsys.readouterr()
+ assert out == HELP_HISTORY
+ assert err == ''
def test_base_argparse_help(base_app, capsys):
+ # Verify that "set -h" gives the same output as "help set" and that it starts in a way that makes sense
run_cmd(base_app, 'set -h')
- out, err = capsys.readouterr()
- expected = run_cmd(base_app, 'help set')
- assert normalize(base_app.do_set.__doc__ + str(err)) == expected
+ out1, err1 = capsys.readouterr()
+
+ run_cmd(base_app, 'help set')
+ out2, err2 = capsys.readouterr()
+
+ assert out1 == out2
+ assert err1 == err2
+ out = out1.splitlines()
+ assert out[0].startswith('usage: set')
+ assert out[1] == ''
+ assert out[2].startswith('Sets a settable parameter')
def test_base_invalid_option(base_app, capsys):
run_cmd(base_app, 'set -z')
out, err = capsys.readouterr()
- run_cmd(base_app, 'help set')
expected = ['usage: set [-h] [-a] [-l] [settable [settable ...]]', 'set: error: unrecognized arguments: -z']
assert normalize(str(err)) == expected
@@ -597,16 +606,17 @@ def test_allow_redirection(base_app):
assert not os.path.exists(filename)
-def test_input_redirection(base_app, request):
+def test_input_redirection(base_app, request, capsys):
test_dir = os.path.dirname(request.module.__file__)
filename = os.path.join(test_dir, 'redirect.txt')
# NOTE: File 'redirect.txt" contains 1 word "history"
# Verify that redirecting input ffom a file works
- out = run_cmd(base_app, 'help < {}'.format(filename))
- expected = normalize(HELP_HISTORY)
- assert out == expected
+ run_cmd(base_app, 'help < {}'.format(filename))
+ out, err = capsys.readouterr()
+ assert out == HELP_HISTORY
+ assert err == ''
def test_pipe_to_shell(base_app, capsys):