diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-04-25 20:47:50 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-04-25 20:47:50 -0400 |
commit | 14b5033dc82fc055f2852e9cc0fa5a1b071186ed (patch) | |
tree | 3605a1a414f75794cc2ffcae0f12cb3c8e43f7f1 /tests/test_utils.py | |
parent | 48d3f4e85883f14aa8ac19b33409440e2dff5c09 (diff) | |
download | cmd2-git-14b5033dc82fc055f2852e9cc0fa5a1b071186ed.tar.gz |
Added unit test
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r-- | tests/test_utils.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index 4574e579..c0b16990 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -126,23 +126,12 @@ def stdout_sim(): stdsim = cu.StdSim(sys.stdout, echo=True) return stdsim -@pytest.fixture -def stringio_sim(): - import io - stdsim = cu.StdSim(io.StringIO(), echo=True) - return stdsim - def test_stdsim_write_str(stdout_sim): my_str = 'Hello World' stdout_sim.write(my_str) assert stdout_sim.getvalue() == my_str -def test_stdsim_write_str_inner_no_buffer(stringio_sim): - my_str = 'Hello World' - stringio_sim.write(my_str) - assert stringio_sim.getvalue() == my_str - def test_stdsim_write_bytes(stdout_sim): b_str = b'Hello World' with pytest.raises(TypeError): @@ -218,6 +207,26 @@ def test_stdsim_pause_storage(stdout_sim): stdout_sim.buffer.write(b_str) assert stdout_sim.getbytes() == b'' +def test_stdsim_line_buffering(base_app): + # This exercises the case of writing binary data that contains new lines/carriage returns to a StdSim + # when line buffering is on. The output should immediately be flushed to the underlying stream. + import os + import tempfile + file = tempfile.NamedTemporaryFile(mode='wt') + file.line_buffering = True + + stdsim = cu.StdSim(file, echo=True) + saved_size = os.path.getsize(file.name) + + bytes_to_write = b'hello\n' + stdsim.buffer.write(bytes_to_write) + assert os.path.getsize(file.name) == saved_size + len(bytes_to_write) + saved_size = os.path.getsize(file.name) + + bytes_to_write = b'hello\r' + stdsim.buffer.write(bytes_to_write) + assert os.path.getsize(file.name) == saved_size + len(bytes_to_write) + @pytest.fixture def pr_none(): |