Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4305.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace byte/unicode helpers in test_capture with python level syntax.
47 changes: 15 additions & 32 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,6 @@
)


def tobytes(obj):
if isinstance(obj, text_type):
obj = obj.encode("UTF-8")
assert isinstance(obj, bytes)
return obj


def totext(obj):
if isinstance(obj, bytes):
obj = text_type(obj, "UTF-8")
assert isinstance(obj, text_type)
return obj


def oswritebytes(fd, obj):
os.write(fd, tobytes(obj))


def StdCaptureFD(out=True, err=True, in_=True):
return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture)

Expand Down Expand Up @@ -832,10 +814,11 @@ def test_write_bytes_to_buffer(self):

def test_bytes_io():
f = py.io.BytesIO()
f.write(tobytes("hello"))
pytest.raises(TypeError, "f.write(totext('hello'))")
f.write(b"hello")
with pytest.raises(TypeError):
f.write(u"hello")
s = f.getvalue()
assert s == tobytes("hello")
assert s == b"hello"


def test_dontreadfrominput():
Expand Down Expand Up @@ -948,7 +931,7 @@ class TestFDCapture(object):
def test_simple(self, tmpfile):
fd = tmpfile.fileno()
cap = capture.FDCapture(fd)
data = tobytes("hello")
data = b"hello"
os.write(fd, data)
s = cap.snap()
cap.done()
Expand Down Expand Up @@ -988,18 +971,18 @@ def test_stdin(self, tmpfile):
cap.start()
x = os.read(0, 100).strip()
cap.done()
assert x == tobytes("")
assert x == b""

def test_writeorg(self, tmpfile):
data1, data2 = tobytes("foo"), tobytes("bar")
data1, data2 = b"foo", b"bar"
cap = capture.FDCapture(tmpfile.fileno())
cap.start()
tmpfile.write(data1)
tmpfile.flush()
cap.writeorg(data2)
scap = cap.snap()
cap.done()
assert scap == totext(data1)
assert scap == data1.decode("ascii")
with open(tmpfile.name, "rb") as stmp_file:
stmp = stmp_file.read()
assert stmp == data2
Expand All @@ -1008,17 +991,17 @@ def test_simple_resume_suspend(self, tmpfile):
with saved_fd(1):
cap = capture.FDCapture(1)
cap.start()
data = tobytes("hello")
data = b"hello"
os.write(1, data)
sys.stdout.write("whatever")
s = cap.snap()
assert s == "hellowhatever"
cap.suspend()
os.write(1, tobytes("world"))
os.write(1, b"world")
sys.stdout.write("qlwkej")
assert not cap.snap()
cap.resume()
os.write(1, tobytes("but now"))
os.write(1, b"but now")
sys.stdout.write(" yes\n")
s = cap.snap()
assert s == "but now yes\n"
Expand Down Expand Up @@ -1189,14 +1172,14 @@ def test_x():

def test_intermingling(self):
with self.getcapture() as cap:
oswritebytes(1, "1")
os.write(1, b"1")
sys.stdout.write(str(2))
sys.stdout.flush()
oswritebytes(1, "3")
oswritebytes(2, "a")
os.write(1, b"3")
os.write(2, b"a")
sys.stderr.write("b")
sys.stderr.flush()
oswritebytes(2, "c")
os.write(2, b"c")
out, err = cap.readouterr()
assert out == "123"
assert err == "abc"
Expand Down