diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-08-07 22:49:56 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-08-07 22:49:56 -0400 |
commit | de9cda230d3623f7c5c73e4b1a3827e906160208 (patch) | |
tree | e5b8ad6fee5e327fca3690c5977939287bfe56ff /tests/test_cmd2.py | |
parent | 11f14edd3ab4fb852a13574071fe616bbb41c720 (diff) | |
download | cmd2-git-de9cda230d3623f7c5c73e4b1a3827e906160208.tar.gz |
First attempt at improving how command output gets piped to a shell command
Now a real pipe is created to a subprocess. This has many advantages and should "just work" like intended with all commands.
One downside is to work properly on Python 2.7, it requires the subprocess32 module which is the subprocess module from Python 3.2 backported to Python 2.7. Another downside, is that unit testing the feature is now more difficult.
This still needs to be tested for compatibility across all OSes and supported versions of Python.
The user needs to be careful if designing multi-threaded cmd2 applications that do command processing in other threads and those threads can make calls to self.stdout.write to put in a try/except to catch Broken Pipe errors which can occur for long running commands if the user closes the shell subprocess before the command is finished.
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index f41b7ed1..3a53a9c3 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -543,33 +543,50 @@ def test_input_redirection(base_app, request): # NOTE: File 'redirect.txt" contains 1 word "history" - # Verify that redirecting input from a file works + # Verify that redirecting input ffom a file works out = run_cmd(base_app, 'help < {}'.format(filename)) expected = normalize(HELP_HISTORY) assert out == expected -def test_pipe_to_shell(base_app): +def test_pipe_to_shell(base_app, capsys): if sys.platform == "win32": # Windows + command = 'help | sort' # Get help menu and pipe it's output to the sort shell command - out = run_cmd(base_app, 'help | sort') - expected = ['', '', '_relative_load edit history py quit save shell show', - '========================================', - 'cmdenvironment help load pyscript run set shortcuts', - 'Documented commands (type help <topic>):'] - assert out == expected + run_cmd(base_app, 'help | sort') + # expected = ['', '', '_relative_load edit history py quit save shell show', + # '========================================', + # 'cmdenvironment help load pyscript run set shortcuts', + # 'Documented commands (type help <topic>):'] + # assert out == expected else: # Mac and Linux # Get help on help and pipe it's output to the input of the word count shell command - out = run_cmd(base_app, 'help help | wc') - - # Mac and Linux wc behave the same when piped from shell, but differently when piped stdin from file directly - if sys.platform == 'darwin': - expected = normalize("1 11 70") - else: - expected = normalize("1 11 70") - assert out[0].strip() == expected[0].strip() + command = 'help help | wc' + # # Mac and Linux wc behave the same when piped from shell, but differently when piped stdin from file directly + # if sys.platform == 'darwin': + # expected = "1 11 70" + # else: + # expected = "1 11 70" + # assert out.strip() == expected.strip() + + run_cmd(base_app, command) + out, err = capsys.readouterr() + + # Unfortunately with the improved way of piping output to a subprocess, there isn't any good way of getting + # access to the output produced by that subprocess within a unit test, but we can verify that no error occured + assert not err + + + +def test_pipe_to_shell_error(base_app, capsys): + # Try to pipe command output to a shell command that doesn't exist in order to produce an error + run_cmd(base_app, 'help | foobarbaz.this_does_not_exist') + out, err = capsys.readouterr() + + assert not out + assert err.startswith("EXCEPTION of type 'FileNotFoundError' occurred with message:") @pytest.mark.skipif(not cmd2.can_clip, |