Skip to content

Commit 14b5033

Browse files
committed
Added unit test
1 parent 48d3f4e commit 14b5033

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

tests/test_utils.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,12 @@ def stdout_sim():
126126
stdsim = cu.StdSim(sys.stdout, echo=True)
127127
return stdsim
128128

129-
@pytest.fixture
130-
def stringio_sim():
131-
import io
132-
stdsim = cu.StdSim(io.StringIO(), echo=True)
133-
return stdsim
134-
135129

136130
def test_stdsim_write_str(stdout_sim):
137131
my_str = 'Hello World'
138132
stdout_sim.write(my_str)
139133
assert stdout_sim.getvalue() == my_str
140134

141-
def test_stdsim_write_str_inner_no_buffer(stringio_sim):
142-
my_str = 'Hello World'
143-
stringio_sim.write(my_str)
144-
assert stringio_sim.getvalue() == my_str
145-
146135
def test_stdsim_write_bytes(stdout_sim):
147136
b_str = b'Hello World'
148137
with pytest.raises(TypeError):
@@ -218,6 +207,26 @@ def test_stdsim_pause_storage(stdout_sim):
218207
stdout_sim.buffer.write(b_str)
219208
assert stdout_sim.getbytes() == b''
220209

210+
def test_stdsim_line_buffering(base_app):
211+
# This exercises the case of writing binary data that contains new lines/carriage returns to a StdSim
212+
# when line buffering is on. The output should immediately be flushed to the underlying stream.
213+
import os
214+
import tempfile
215+
file = tempfile.NamedTemporaryFile(mode='wt')
216+
file.line_buffering = True
217+
218+
stdsim = cu.StdSim(file, echo=True)
219+
saved_size = os.path.getsize(file.name)
220+
221+
bytes_to_write = b'hello\n'
222+
stdsim.buffer.write(bytes_to_write)
223+
assert os.path.getsize(file.name) == saved_size + len(bytes_to_write)
224+
saved_size = os.path.getsize(file.name)
225+
226+
bytes_to_write = b'hello\r'
227+
stdsim.buffer.write(bytes_to_write)
228+
assert os.path.getsize(file.name) == saved_size + len(bytes_to_write)
229+
221230

222231
@pytest.fixture
223232
def pr_none():

0 commit comments

Comments
 (0)