Skip to content

Commit 4d3553b

Browse files
committed
pylint/test fixes
1 parent 95bd3ea commit 4d3553b

2 files changed

Lines changed: 86 additions & 7 deletions

File tree

meshtastic/serial_interface.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ def __init__(
5656

5757
def connect(self) -> None:
5858
logger.debug(f"Connecting to {self.devPath}")
59+
dev_path = self.devPath
60+
if dev_path is None:
61+
raise RuntimeError("Serial device path is not set")
5962

6063
if sys.platform != "win32":
61-
with open(self.devPath, encoding="utf8") as f:
64+
with open(dev_path, encoding="utf8") as f:
6265
self._set_hupcl_with_termios(f)
6366
time.sleep(0.1)
6467

6568
self.stream = serial.Serial(
66-
self.devPath, 115200, exclusive=True, timeout=0.5, write_timeout=0
69+
dev_path, 115200, exclusive=True, timeout=0.5, write_timeout=0
6770
)
6871
self.stream.flush() # type: ignore[attr-defined]
6972
time.sleep(0.1)

meshtastic/tests/test_main.py

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import sys
99
import tempfile
10+
from types import SimpleNamespace
1011
from unittest.mock import mock_open, MagicMock, patch
1112

1213
import pytest
@@ -1722,11 +1723,9 @@ def test_main_onReceive_with_sendtext(caplog, capsys):
17221723

17231724
@pytest.mark.unit
17241725
@pytest.mark.usefixtures("reset_mt_config")
1725-
def test_main_onReceive_with_text(caplog, capsys):
1726-
"""Test onReceive with text"""
1727-
args = MagicMock()
1728-
args.sendtext.return_value = "foo"
1729-
mt_config.args = args
1726+
def test_main_onReceive_with_text_replies_on_target_channel(caplog, capsys):
1727+
"""Test onReceive replies when channel matches --ch-index (default 0)."""
1728+
mt_config.args = SimpleNamespace(reply=True, ch_index=0, sendtext=None)
17301729

17311730
# Note: 'TEXT_MESSAGE_APP' value is 1
17321731
# Note: Some of this is faked below.
@@ -1752,6 +1751,83 @@ def test_main_onReceive_with_text(caplog, capsys):
17521751
assert re.search(r"in onReceive", caplog.text, re.MULTILINE)
17531752
out, err = capsys.readouterr()
17541753
assert re.search(r"Sending reply", out, re.MULTILINE)
1754+
iface.sendText.assert_called_once_with(
1755+
"got msg 'faked' with rxSnr: 6.0 and hopLimit: 3", channelIndex=0
1756+
)
1757+
assert err == ""
1758+
1759+
1760+
@pytest.mark.unit
1761+
@pytest.mark.usefixtures("reset_mt_config")
1762+
def test_main_onReceive_with_text_ignores_non_target_channel(caplog, capsys):
1763+
"""Test onReceive does not reply when packet channel differs from --ch-index."""
1764+
mt_config.args = SimpleNamespace(reply=True, ch_index=1, sendtext=None)
1765+
1766+
packet = {
1767+
"to": 4294967295,
1768+
"decoded": {"portnum": 1, "payload": "hello", "text": "faked"},
1769+
"id": 334776977,
1770+
"hop_limit": 3,
1771+
"want_ack": True,
1772+
"rxSnr": 6.0,
1773+
"hopLimit": 3,
1774+
"raw": "faked",
1775+
"fromId": "!28b5465c",
1776+
"toId": "^all",
1777+
"channel": 0,
1778+
}
1779+
1780+
iface = MagicMock(autospec=SerialInterface)
1781+
iface.myInfo.my_node_num = 4294967295
1782+
1783+
with patch("meshtastic.serial_interface.SerialInterface", return_value=iface):
1784+
with caplog.at_level(logging.DEBUG):
1785+
onReceive(packet, iface)
1786+
assert re.search(r"in onReceive", caplog.text, re.MULTILINE)
1787+
out, err = capsys.readouterr()
1788+
assert re.search(
1789+
r"Ignored message on channel 0 \(waiting for channel 1\)", out, re.MULTILINE
1790+
)
1791+
iface.sendText.assert_not_called()
1792+
assert err == ""
1793+
1794+
1795+
@pytest.mark.unit
1796+
@pytest.mark.usefixtures("reset_mt_config")
1797+
def test_main_onReceive_with_text_replies_on_explicit_matching_channel(caplog, capsys):
1798+
"""Test onReceive replies when explicit packet channel matches --ch-index."""
1799+
mt_config.args = SimpleNamespace(reply=True, ch_index=2, sendtext=None)
1800+
1801+
packet = {
1802+
"to": 4294967295,
1803+
"decoded": {"portnum": 1, "payload": "hello", "text": "faked"},
1804+
"id": 334776977,
1805+
"hop_limit": 3,
1806+
"want_ack": True,
1807+
"rxSnr": 6.0,
1808+
"hopLimit": 3,
1809+
"raw": "faked",
1810+
"fromId": "!28b5465c",
1811+
"toId": "^all",
1812+
"channel": 2,
1813+
}
1814+
1815+
iface = MagicMock(autospec=SerialInterface)
1816+
iface.myInfo.my_node_num = 4294967295
1817+
1818+
with patch("meshtastic.serial_interface.SerialInterface", return_value=iface):
1819+
with caplog.at_level(logging.DEBUG):
1820+
onReceive(packet, iface)
1821+
assert re.search(r"in onReceive", caplog.text, re.MULTILINE)
1822+
out, err = capsys.readouterr()
1823+
assert re.search(
1824+
r"Received channel 2\. Sending reply: got msg 'faked' with rxSnr: 6.0 and hopLimit: 3",
1825+
out,
1826+
re.MULTILINE,
1827+
)
1828+
iface.sendText.assert_called_once_with(
1829+
"got msg 'faked' with rxSnr: 6.0 and hopLimit: 3", channelIndex=2
1830+
)
17551831
assert err == ""
17561832

17571833

0 commit comments

Comments
 (0)