From 7b0fed0987f1799edddfabecdf81897e6631250a Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Thu, 9 Dec 2021 00:56:26 -0800 Subject: [PATCH] add test with no serial ports detected and with 2 serial ports detected --- meshtastic/test/test_serial_interface.py | 35 ++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/meshtastic/test/test_serial_interface.py b/meshtastic/test/test_serial_interface.py index df6f12f..10b4dbc 100644 --- a/meshtastic/test/test_serial_interface.py +++ b/meshtastic/test/test_serial_interface.py @@ -1,5 +1,8 @@ """Meshtastic unit tests for serial_interface.py""" +import re + + from unittest.mock import patch import pytest @@ -8,11 +11,39 @@ from ..serial_interface import SerialInterface @pytest.mark.unit @patch('serial.Serial') @patch('meshtastic.util.findPorts', return_value=['/dev/ttyUSBfake']) -def test_SerialInterface(mocked_findPorts, mocked_serial): - """Test that we can instantiate a SerialInterface""" +def test_SerialInterface_single_port(mocked_findPorts, mocked_serial): + """Test that we can instantiate a SerialInterface with a single port""" iface = SerialInterface(noProto=True) iface.showInfo() iface.localNode.showInfo() iface.close() mocked_findPorts.assert_called() mocked_serial.assert_called() + + +@pytest.mark.unit +@patch('meshtastic.util.findPorts', return_value=[]) +def test_SerialInterface_no_ports(mocked_findPorts, capsys): + """Test that we can instantiate a SerialInterface with no ports""" + with pytest.raises(SystemExit) as pytest_wrapped_e: + SerialInterface(noProto=True) + mocked_findPorts.assert_called() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert re.search(r'Warning: No Meshtastic devices detected', out, re.MULTILINE) + assert err == '' + + +@pytest.mark.unit +@patch('meshtastic.util.findPorts', return_value=['/dev/ttyUSBfake1', '/dev/ttyUSBfake2']) +def test_SerialInterface_multiple_ports(mocked_findPorts, capsys): + """Test that we can instantiate a SerialInterface with two ports""" + with pytest.raises(SystemExit) as pytest_wrapped_e: + SerialInterface(noProto=True) + mocked_findPorts.assert_called() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert re.search(r'Warning: Multiple serial ports were detected', out, re.MULTILINE) + assert err == ''