Skip to content

Commit fe093ac

Browse files
committed
fix errors and better linux/windows handling in tests
1 parent dd238dc commit fe093ac

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

meshtastic/serial_interface.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import sys
66
import time
7+
from io import TextIOWrapper
78

89
from typing import List, Optional
910

@@ -44,7 +45,10 @@ def __init__(self, devPath: Optional[str]=None, debugOut=None, noProto: bool=Fal
4445

4546
logger.debug(f"Connecting to {self.devPath}")
4647

47-
self._set_hupcl_with_termios()
48+
if sys.platform != "win32":
49+
with open(self.devPath, encoding="utf8") as f:
50+
self._set_hupcl_with_termios(f)
51+
time.sleep(0.1)
4852

4953
self.stream = serial.Serial(
5054
self.devPath, 115200, exclusive=True, timeout=0.5, write_timeout=0
@@ -56,21 +60,17 @@ def __init__(self, devPath: Optional[str]=None, debugOut=None, noProto: bool=Fal
5660
self, debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes
5761
)
5862

59-
def _set_hupcl_with_termios(self):
63+
def _set_hupcl_with_termios(self, f: TextIOWrapper):
6064
"""first we need to set the HUPCL so the device will not reboot based on RTS and/or DTR
6165
see https://github.com/pyserial/pyserial/issues/124
6266
"""
6367
if sys.platform == "win32":
6468
return
6569

66-
with open(self.devPath, encoding="utf8") as f:
67-
import termios # pylint: disable=C0415,E0401
68-
attrs = termios.tcgetattr(f)
69-
attrs[2] = attrs[2] & ~termios.HUPCL
70-
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
71-
f.close()
72-
73-
time.sleep(0.1)
70+
import termios # pylint: disable=C0415,E0401
71+
attrs = termios.tcgetattr(f)
72+
attrs[2] = attrs[2] & ~termios.HUPCL
73+
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
7474

7575
def __repr__(self):
7676
rep = f"SerialInterface(devPath={self.devPath!r}"

meshtastic/slog/slog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def root_dir() -> str:
3232
app_author = "meshtastic"
3333
app_dir = platformdirs.user_data_dir(app_name, app_author)
3434
dir_name = Path(app_dir, "slogs")
35-
dir_name.mkdir(exist_ok=True)
35+
dir_name.mkdir(exist_ok=True, parents=True)
3636
return str(dir_name)
3737

3838

meshtastic/tests/test_main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,10 +979,11 @@ def test_main_set_valid(mocked_findports, mocked_serial, mocked_open, mocked_hup
979979

980980
@pytest.mark.unit
981981
@pytest.mark.usefixtures("reset_mt_config")
982+
@patch("meshtastic.serial_interface.SerialInterface._set_hupcl_with_termios")
982983
@patch("builtins.open", new_callable=mock_open, read_data="data")
983984
@patch("serial.Serial")
984985
@patch("meshtastic.util.findPorts", return_value=["/dev/ttyUSBfake"])
985-
def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, capsys):
986+
def test_main_set_valid_wifi_psk(mocked_findports, mocked_serial, mocked_open, mocked_hupcl, capsys):
986987
"""Test --set with valid field"""
987988
sys.argv = ["", "--set", "network.wifi_psk", "123456789"]
988989
mt_config.args = sys.argv

meshtastic/tests/test_serial_interface.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
"""Meshtastic unit tests for serial_interface.py"""
2-
import platform
32
# pylint: disable=R0917
43

54
import re
5+
import sys
66
from unittest.mock import mock_open, patch
77

88
import pytest
99

1010
from ..serial_interface import SerialInterface
1111
from ..protobuf import config_pb2
1212

13-
1413
@pytest.mark.unit
1514
@patch("time.sleep")
1615
@patch("meshtastic.serial_interface.SerialInterface._set_hupcl_with_termios")
@@ -28,11 +27,11 @@ def test_SerialInterface_single_port(
2827
iface.close()
2928
mocked_findPorts.assert_called()
3029
mocked_serial.assert_called()
31-
mock_hupcl.assert_called()
3230

33-
# doesn't get called in SerialInterface._set_hupcl_with_termios on windows
34-
if platform.system() != "Windows":
31+
# doesn't get called in SerialInterface on windows
32+
if sys.platform != "win32":
3533
mocked_open.assert_called()
34+
mock_hupcl.assert_called()
3635

3736
mock_sleep.assert_called()
3837
out, err = capsys.readouterr()

0 commit comments

Comments
 (0)