diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-22 00:08:27 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-22 00:08:27 -0500 |
commit | f28c10a50535f753419bd2120ac6cb0bea9f56e2 (patch) | |
tree | 102b200c4a3dff1adad4bf326ea3f6de9cd56baf /tests | |
parent | c9f7c012bda012b4df7a8c5e853bd5d3e6d99b1b (diff) | |
download | cmd2-git-f28c10a50535f753419bd2120ac6cb0bea9f56e2.tar.gz |
help command temporarily redirects sys.stdout and sys.stderr to self.stdout for argparse commands
In order to make "help" behave more consistently for decorated and undecorated commands, argparse output is temporarily redirected to self.stdout. So doing "help history" is similar to "help load".
However, when using the "-h" with argparse commands without using the "help" command, the output from argparse isn't redirected to self.stdout. Fixing this would be rather difficult and would essentially involve creating a pyparsing rule to detect it at the parser level.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_argparse.py | 40 | ||||
-rw-r--r-- | tests/test_cmd2.py | 30 | ||||
-rw-r--r-- | tests/test_completion.py | 6 |
3 files changed, 29 insertions, 47 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py index ecaa1049..d3646046 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -139,26 +139,20 @@ 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, capsys): - run_cmd(argparse_app, 'help say') - out, err = capsys.readouterr() - out = out.splitlines() +def test_argparse_help_docstring(argparse_app): + out = run_cmd(argparse_app, 'help say') 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, capsys): - run_cmd(argparse_app, 'help tag') - out, err = capsys.readouterr() - out = out.splitlines() +def test_argparse_help_description(argparse_app): + out = run_cmd(argparse_app, 'help tag') assert out[0].startswith('usage: tag') assert out[1] == '' assert out[2] == 'create a html tag' -def test_argparse_prog(argparse_app, capsys): - run_cmd(argparse_app, 'help tag') - out, err = capsys.readouterr() - out = out.splitlines() +def test_argparse_prog(argparse_app): + out = run_cmd(argparse_app, 'help tag') progname = out[0].split(' ')[1] assert progname == 'tag' @@ -237,26 +231,20 @@ def test_subcommand_invalid(subcommand_app, capsys): assert err[0].startswith('usage: base') assert err[1].startswith("base: error: invalid choice: 'baz'") -def test_subcommand_base_help(subcommand_app, capsys): - run_cmd(subcommand_app, 'help base') - out, err = capsys.readouterr() - out = out.splitlines() +def test_subcommand_base_help(subcommand_app): + out = run_cmd(subcommand_app, 'help base') assert out[0].startswith('usage: base') assert out[1] == '' assert out[2] == 'Base command help' -def test_subcommand_help(subcommand_app, capsys): - run_cmd(subcommand_app, 'help base foo') - out, err = capsys.readouterr() - out = out.splitlines() +def test_subcommand_help(subcommand_app): + out = run_cmd(subcommand_app, 'help base foo') assert out[0].startswith('usage: base foo') assert out[1] == '' assert out[2] == 'positional arguments:' -def test_subcommand_invalid_help(subcommand_app, capsys): - run_cmd(subcommand_app, 'help base baz') - out, err = capsys.readouterr() - err = err.splitlines() - assert err[0].startswith('usage: base') - assert err[1].startswith("base: error: invalid choice: 'baz'") +def test_subcommand_invalid_help(subcommand_app): + out = run_cmd(subcommand_app, 'help base baz') + assert out[0].startswith('usage: base') + assert out[1].startswith("base: error: invalid choice: 'baz'") diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 9fb9f2d9..186def65 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -39,26 +39,22 @@ def test_base_help(base_app): 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_help_history(base_app): + out = run_cmd(base_app, 'help history') + assert out == normalize(HELP_HISTORY) 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') - out1, err1 = capsys.readouterr() + out, err = capsys.readouterr() + out1 = out.splitlines() - run_cmd(base_app, 'help set') - out2, err2 = capsys.readouterr() + out2 = run_cmd(base_app, 'help set') 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') + assert out1[0].startswith('usage: set') + assert out1[1] == '' + assert out1[2].startswith('Sets a settable parameter') def test_base_invalid_option(base_app, capsys): run_cmd(base_app, 'set -z') @@ -606,17 +602,15 @@ def test_allow_redirection(base_app): assert not os.path.exists(filename) -def test_input_redirection(base_app, request, capsys): +def test_input_redirection(base_app, request): 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 - run_cmd(base_app, 'help < {}'.format(filename)) - out, err = capsys.readouterr() - assert out == HELP_HISTORY - assert err == '' + out = run_cmd(base_app, 'help < {}'.format(filename)) + assert out == normalize(HELP_HISTORY) def test_pipe_to_shell(base_app, capsys): diff --git a/tests/test_completion.py b/tests/test_completion.py index 05774df9..70f77d0a 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -80,9 +80,9 @@ def test_complete_command_invalid_state(cmd2_app): with mock.patch.object(readline, 'get_line_buffer', get_line): with mock.patch.object(readline, 'get_begidx', get_begidx): with mock.patch.object(readline, 'get_endidx', get_endidx): - with pytest.raises(AttributeError): - # Run the readline tab-completion function with readline mocks in place and cause an exception - completion = cmd2_app.complete(text, state) + # Run the readline tab-completion function with readline mocks in place get None + completion = cmd2_app.complete(text, state) + assert completion is None def test_complete_empty_arg(cmd2_app): text = '' |