From cb8dafbd31cb8b0389561c9c74579d578404f12b Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Mon, 10 Jan 2022 16:40:47 -0800 Subject: [PATCH] add more coverage --- meshtastic/serial_interface.py | 29 ++++++++++------------- meshtastic/tests/test_serial_interface.py | 8 +++++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/meshtastic/serial_interface.py b/meshtastic/serial_interface.py index 43e0f83..b0e3a40 100644 --- a/meshtastic/serial_interface.py +++ b/meshtastic/serial_interface.py @@ -40,28 +40,25 @@ class SerialInterface(StreamInterface): # first we need to set the HUPCL so the device will not reboot based on RTS and/or DTR # see https://github.com/pyserial/pyserial/issues/124 - if not self.noProto: - if platform.system() != 'Windows': - with open(devPath, encoding='utf8') as f: - attrs = termios.tcgetattr(f) - attrs[2] = attrs[2] & ~termios.HUPCL - termios.tcsetattr(f, termios.TCSAFLUSH, attrs) - f.close() - time.sleep(0.1) + if platform.system() != 'Windows': + with open(devPath, encoding='utf8') as f: + attrs = termios.tcgetattr(f) + attrs[2] = attrs[2] & ~termios.HUPCL + termios.tcsetattr(f, termios.TCSAFLUSH, attrs) + f.close() + time.sleep(0.1) self.stream = serial.Serial(devPath, 921600, exclusive=True, timeout=0.5, write_timeout=0) - if not self.noProto: - self.stream.flush() - time.sleep(0.1) + self.stream.flush() + time.sleep(0.1) StreamInterface.__init__(self, debugOut=debugOut, noProto=noProto, connectNow=connectNow) def close(self): """Close a connection to the device""" - if not self.noProto: - self.stream.flush() - time.sleep(0.1) - self.stream.flush() - time.sleep(0.1) + self.stream.flush() + time.sleep(0.1) + self.stream.flush() + time.sleep(0.1) logging.debug("Closing Serial stream") StreamInterface.close(self) diff --git a/meshtastic/tests/test_serial_interface.py b/meshtastic/tests/test_serial_interface.py index e89b46e..7e91b21 100644 --- a/meshtastic/tests/test_serial_interface.py +++ b/meshtastic/tests/test_serial_interface.py @@ -3,15 +3,19 @@ import re -from unittest.mock import patch +from unittest.mock import patch, mock_open import pytest from ..serial_interface import SerialInterface @pytest.mark.unit +@patch("time.sleep") +@patch("termios.tcsetattr") +@patch("termios.tcgetattr") +@patch("builtins.open", new_callable=mock_open, read_data="data") @patch('serial.Serial') @patch('meshtastic.util.findPorts', return_value=['/dev/ttyUSBfake']) -def test_SerialInterface_single_port(mocked_findPorts, mocked_serial, capsys): +def test_SerialInterface_single_port(mocked_findPorts, mocked_serial, mocked_open, mock_get, mock_set, mock_sleep, capsys): """Test that we can instantiate a SerialInterface with a single port""" iface = SerialInterface(noProto=True) iface.showInfo()