From 6aa8a8d460657bbba51d2d57fdc951651723ae92 Mon Sep 17 00:00:00 2001 From: Benedikt Moneke <67148916+bmoneke@users.noreply.github.com> Date: Tue, 10 May 2022 22:55:38 +0200 Subject: [PATCH] Ensure a correctly encoded input for manual input. (#352) * Ensure a correctly encoded input for manual input. * Test added. Co-authored-by: Benedikt Moneke --- .../abstract_instruments/comm/loopback_communicator.py | 2 +- tests/test_comm/test_loopback.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/instruments/abstract_instruments/comm/loopback_communicator.py b/src/instruments/abstract_instruments/comm/loopback_communicator.py index 5687dca9..f117eebb 100644 --- a/src/instruments/abstract_instruments/comm/loopback_communicator.py +++ b/src/instruments/abstract_instruments/comm/loopback_communicator.py @@ -121,7 +121,7 @@ def read_raw(self, size=-1): else: raise ValueError("Must read a positive value of characters.") else: - input_var = input("Desired Response: ") + input_var = input("Desired Response: ").encode("utf-8") return input_var def write_raw(self, msg): diff --git a/tests/test_comm/test_loopback.py b/tests/test_comm/test_loopback.py index 0aa9ab46..5b4fde08 100644 --- a/tests/test_comm/test_loopback.py +++ b/tests/test_comm/test_loopback.py @@ -116,10 +116,16 @@ def test_loopbackcomm_read_raw_size_invalid(): comm.read_raw(size=-2) +@mock.patch("builtins.input") +def test_loopbackcomm_read_raw_stdin(mock_input): + mock_input.return_value = "Returned string." + comm = LoopbackCommunicator() + assert comm.read_raw() == b"Returned string." + + def test_loopbackcomm_write_raw(): mock_stdout = mock.MagicMock() comm = LoopbackCommunicator(stdout=mock_stdout) - comm.write_raw(b"mock") mock_stdout.write.assert_called_with(b"mock")