From a56a92b166170314d9aaa1dfbd7b05fb07e92f0d Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Thu, 23 Dec 2021 09:30:23 -0800 Subject: [PATCH] needed to do some actual testing to see error in code --- meshtastic/__main__.py | 2 +- meshtastic/tests/test_main.py | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 83911baf..29e6bb04 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -244,7 +244,7 @@ def getNode(): channelIndex = 0 if args.ch_index is not None: channelIndex = int(args.ch_index) - ch = interface.getChannelByChannelIndex(channelIndex) + ch = getNode().getChannelByChannelIndex(channelIndex) if ch and ch.role != channel_pb2.Channel.Role.DISABLED: print(f"Sending text message {args.sendtext} to {args.destOrAll} on channelIndex:{channelIndex}") interface.sendText(args.sendtext, args.destOrAll, wantAck=True, channelIndex=channelIndex) diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index c83cbe55..e1921022 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -449,7 +449,26 @@ def test_main_sendtext_with_invalid_channel(capsys, reset_globals): iface = MagicMock(autospec=SerialInterface) iface.getChannelByChannelIndex.return_value = None with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: - #mo.getChannelByChannelIndex.return_value = None + iface.getNode.return_value.getChannelByChannelIndex.return_value = None + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert re.search(r'is not a valid channel', out, re.MULTILINE) + assert err == '' + mo.assert_called() + + +@pytest.mark.unit +def test_main_sendtext_with_invalid_channel_nine(capsys, reset_globals): + """Test --sendtext""" + sys.argv = ['', '--sendtext', 'hello', '--ch-index', '9'] + Globals.getInstance().set_args(sys.argv) + + iface = MagicMock(autospec=SerialInterface) + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + iface.getNode.return_value.getChannelByChannelIndex.return_value = None with pytest.raises(SystemExit) as pytest_wrapped_e: main() assert pytest_wrapped_e.type == SystemExit