diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-08-07 23:22:11 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-08-07 23:22:11 -0400 |
commit | 3a3c867c0715e03e30cc30bea7b08604af3aacdc (patch) | |
tree | d5c6a69fd6c826d20803abb10f323087b852ca70 /tests/test_cmd2.py | |
parent | de9cda230d3623f7c5c73e4b1a3827e906160208 (diff) | |
download | cmd2-git-3a3c867c0715e03e30cc30bea7b08604af3aacdc.tar.gz |
Changed how the pipe file descriptors get opened on Python 3 vs 2
Open them in text mode in Python 3 so self.stdout.write() expects normal Python 3 (unicode) strings.
Open them in binary mode in Python 2 so self.stdout.write() expects normal Python 2 (byte) strings.
Also fixed a unit test mocking issue on Python 2 to account for the fact that Python 2.7 requires the subprocess32 module instead of subprocess.
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 14 |
1 files changed, 9 insertions, 5 deletions
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, |