Skip to content

Commit cb8dafb

Browse files
committed
add more coverage
1 parent 52db617 commit cb8dafb

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

meshtastic/serial_interface.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,25 @@ def __init__(self, devPath=None, debugOut=None, noProto=False, connectNow=True):
4040

4141
# first we need to set the HUPCL so the device will not reboot based on RTS and/or DTR
4242
# see https://github.com/pyserial/pyserial/issues/124
43-
if not self.noProto:
44-
if platform.system() != 'Windows':
45-
with open(devPath, encoding='utf8') as f:
46-
attrs = termios.tcgetattr(f)
47-
attrs[2] = attrs[2] & ~termios.HUPCL
48-
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
49-
f.close()
50-
time.sleep(0.1)
43+
if platform.system() != 'Windows':
44+
with open(devPath, encoding='utf8') as f:
45+
attrs = termios.tcgetattr(f)
46+
attrs[2] = attrs[2] & ~termios.HUPCL
47+
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
48+
f.close()
49+
time.sleep(0.1)
5150

5251
self.stream = serial.Serial(devPath, 921600, exclusive=True, timeout=0.5, write_timeout=0)
53-
if not self.noProto:
54-
self.stream.flush()
55-
time.sleep(0.1)
52+
self.stream.flush()
53+
time.sleep(0.1)
5654

5755
StreamInterface.__init__(self, debugOut=debugOut, noProto=noProto, connectNow=connectNow)
5856

5957
def close(self):
6058
"""Close a connection to the device"""
61-
if not self.noProto:
62-
self.stream.flush()
63-
time.sleep(0.1)
64-
self.stream.flush()
65-
time.sleep(0.1)
59+
self.stream.flush()
60+
time.sleep(0.1)
61+
self.stream.flush()
62+
time.sleep(0.1)
6663
logging.debug("Closing Serial stream")
6764
StreamInterface.close(self)

meshtastic/tests/test_serial_interface.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import re
44

55

6-
from unittest.mock import patch
6+
from unittest.mock import patch, mock_open
77
import pytest
88

99
from ..serial_interface import SerialInterface
1010

1111
@pytest.mark.unit
12+
@patch("time.sleep")
13+
@patch("termios.tcsetattr")
14+
@patch("termios.tcgetattr")
15+
@patch("builtins.open", new_callable=mock_open, read_data="data")
1216
@patch('serial.Serial')
1317
@patch('meshtastic.util.findPorts', return_value=['/dev/ttyUSBfake'])
14-
def test_SerialInterface_single_port(mocked_findPorts, mocked_serial, capsys):
18+
def test_SerialInterface_single_port(mocked_findPorts, mocked_serial, mocked_open, mock_get, mock_set, mock_sleep, capsys):
1519
"""Test that we can instantiate a SerialInterface with a single port"""
1620
iface = SerialInterface(noProto=True)
1721
iface.showInfo()

0 commit comments

Comments
 (0)