diff options
-rwxr-xr-x | cmd2.py | 11 | ||||
-rw-r--r-- | tests/test_cmd2.py | 14 |
2 files changed, 18 insertions, 7 deletions
@@ -776,11 +776,18 @@ class Cmd(cmd.Cmd): # Create a pipe with read and write sides read_fd, write_fd = os.pipe() + # Make sure that self.stdout.write() expects unicode strings in Python 3 and byte strings in Python 2 + write_mode = 'w' + read_mode = 'r' + if six.PY2: + write_mode = 'wb' + read_mode = 'rb' + # Open each side of the pipe and set stdout accordingly # noinspection PyTypeChecker - self.stdout = io.open(write_fd, 'w') + self.stdout = io.open(write_fd, write_mode) # noinspection PyTypeChecker - subproc_stdin = io.open(read_fd, 'r') + subproc_stdin = io.open(read_fd, read_mode) # If you don't set shell=True, subprocess failure will throw an exception try: diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 3a53a9c3..db834a68 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -113,7 +113,10 @@ now: True def test_base_shell(base_app, monkeypatch): m = mock.Mock() - monkeypatch.setattr("subprocess.Popen", m) + subprocess = 'subprocess' + if six.PY2: + subprocess = 'subprocess32' + monkeypatch.setattr("{}.Popen".format(subprocess), m) out = run_cmd(base_app, 'shell echo a') assert out == [] assert m.called @@ -554,7 +557,6 @@ def test_pipe_to_shell(base_app, capsys): # Windows command = 'help | sort' # Get help menu and pipe it's output to the sort shell command - run_cmd(base_app, 'help | sort') # expected = ['', '', '_relative_load edit history py quit save shell show', # '========================================', # 'cmdenvironment help load pyscript run set shortcuts', @@ -578,15 +580,17 @@ def test_pipe_to_shell(base_app, capsys): # 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:") + + expected_error = 'FileNotFoundError' + if six.PY2: + expected_error = 'OSError' + assert err.startswith("EXCEPTION of type '{}' occurred with message:".format(expected_error)) @pytest.mark.skipif(not cmd2.can_clip, |