Skip to content

Commit 4046a69

Browse files
committed
Fixed unit tests
Updated unit tests due to changes in how help is output for commands decorated with an argparse ArgumentParser.
1 parent 136de7e commit 4046a69

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* **do_*** commands get two arguments, the output of argparse.parse_known_args()
1212
* See the **Argument Processing** section of the documentation for more information on these decorators
1313
* Alternatively, see the **argparse_example.py** and **arg_print.py** examples
14+
* Added support for Argpasre sub-commands when using the **with_argument_parser** or **with_argparser_and_unknown_args** decorators
15+
* See [subcommands.py](https://github.com/python-cmd2/cmd2/blob/master/examples/subcommands.py) for an example of how to use subcommands
1416
* The **__relative_load** command is now hidden from the help menu by default
1517
* This command is not intended to be called from the command line, only from within scripts
1618
* The **set** command now has an additional **-a/--all** option to also display read-only settings

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
-o FILE, --output-file FILE
3838
output to file
3939
-s, --script script format; no separation lines
40-
4140
"""
4241

4342
# Output from the shortcuts command with default built-in shortcuts

tests/test_argparse.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,26 @@ def test_argparse_quoted_arguments_posix_multiple(argparse_app):
138138
out = run_cmd(argparse_app, 'tag strong this "should be" loud')
139139
assert out == ['<strong>this should be loud</strong>']
140140

141-
def test_argparse_help_docstring(argparse_app):
142-
out = run_cmd(argparse_app, 'help say')
141+
def test_argparse_help_docstring(argparse_app, capsys):
142+
run_cmd(argparse_app, 'help say')
143+
out, err = capsys.readouterr()
144+
out = out.splitlines()
143145
assert out[0].startswith('usage: say')
144146
assert out[1] == ''
145147
assert out[2] == 'Repeat what you tell me to.'
146148

147-
def test_argparse_help_description(argparse_app):
148-
out = run_cmd(argparse_app, 'help tag')
149+
def test_argparse_help_description(argparse_app, capsys):
150+
run_cmd(argparse_app, 'help tag')
151+
out, err = capsys.readouterr()
152+
out = out.splitlines()
149153
assert out[0].startswith('usage: tag')
150154
assert out[1] == ''
151155
assert out[2] == 'create a html tag'
152156

153-
def test_argparse_prog(argparse_app):
154-
out = run_cmd(argparse_app, 'help tag')
157+
def test_argparse_prog(argparse_app, capsys):
158+
run_cmd(argparse_app, 'help tag')
159+
out, err = capsys.readouterr()
160+
out = out.splitlines()
155161
progname = out[0].split(' ')[1]
156162
assert progname == 'tag'
157163

tests/test_cmd2.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,30 @@ def test_base_help(base_app):
3939
assert out == expected
4040

4141

42-
def test_base_help_history(base_app):
43-
out = run_cmd(base_app, 'help history')
44-
expected = normalize(HELP_HISTORY)
45-
assert out == expected
42+
def test_base_help_history(base_app, capsys):
43+
run_cmd(base_app, 'help history')
44+
out, err = capsys.readouterr()
45+
assert out == HELP_HISTORY
46+
assert err == ''
4647

4748
def test_base_argparse_help(base_app, capsys):
49+
# Verify that "set -h" gives the same output as "help set" and that it starts in a way that makes sense
4850
run_cmd(base_app, 'set -h')
49-
out, err = capsys.readouterr()
50-
expected = run_cmd(base_app, 'help set')
51-
assert normalize(base_app.do_set.__doc__ + str(err)) == expected
51+
out1, err1 = capsys.readouterr()
52+
53+
run_cmd(base_app, 'help set')
54+
out2, err2 = capsys.readouterr()
55+
56+
assert out1 == out2
57+
assert err1 == err2
58+
out = out1.splitlines()
59+
assert out[0].startswith('usage: set')
60+
assert out[1] == ''
61+
assert out[2].startswith('Sets a settable parameter')
5262

5363
def test_base_invalid_option(base_app, capsys):
5464
run_cmd(base_app, 'set -z')
5565
out, err = capsys.readouterr()
56-
run_cmd(base_app, 'help set')
5766
expected = ['usage: set [-h] [-a] [-l] [settable [settable ...]]', 'set: error: unrecognized arguments: -z']
5867
assert normalize(str(err)) == expected
5968

@@ -597,16 +606,17 @@ def test_allow_redirection(base_app):
597606
assert not os.path.exists(filename)
598607

599608

600-
def test_input_redirection(base_app, request):
609+
def test_input_redirection(base_app, request, capsys):
601610
test_dir = os.path.dirname(request.module.__file__)
602611
filename = os.path.join(test_dir, 'redirect.txt')
603612

604613
# NOTE: File 'redirect.txt" contains 1 word "history"
605614

606615
# Verify that redirecting input ffom a file works
607-
out = run_cmd(base_app, 'help < {}'.format(filename))
608-
expected = normalize(HELP_HISTORY)
609-
assert out == expected
616+
run_cmd(base_app, 'help < {}'.format(filename))
617+
out, err = capsys.readouterr()
618+
assert out == HELP_HISTORY
619+
assert err == ''
610620

611621

612622
def test_pipe_to_shell(base_app, capsys):

0 commit comments

Comments
 (0)