add detection of duplicate ports to findPorts; fix smoke1 test

This commit is contained in:
Mike Kinney
2022-02-18 11:13:48 -08:00
parent ce8b75d96d
commit e53a5023f1
5 changed files with 44 additions and 15 deletions

View File

@@ -24,21 +24,17 @@ class SerialInterface(StreamInterface):
""" """
self.noProto = noProto self.noProto = noProto
self.devPath = None self.devPath = devPath
if devPath is None: if self.devPath is None:
ports = meshtastic.util.findPorts() ports = meshtastic.util.findPorts(True)
logging.debug(f"ports:{ports}") logging.debug(f"ports:{ports}")
if len(ports) == 0: if len(ports) == 0:
meshtastic.util.our_exit("Warning: No Meshtastic devices detected.") meshtastic.util.our_exit("Warning: No Meshtastic devices detected.")
elif len(ports) > 1: elif len(ports) > 1:
tmp_ports = meshtastic.util.eliminate_duplicate_port(ports) message = "Warning: Multiple serial ports were detected so one serial port must be specified with the '--port'.\n"
if len(tmp_ports) != 1: message += f" Ports detected:{ports}"
message = "Warning: Multiple serial ports were detected so one serial port must be specified with the '--port'.\n" meshtastic.util.our_exit(message)
message += f" Ports detected:{ports}"
meshtastic.util.our_exit(message)
else:
self.devPath = tmp_ports[0]
else: else:
self.devPath = ports[0] self.devPath = ports[0]

View File

@@ -149,7 +149,7 @@ def testAll(numTests=5):
This is called from the cli with the "--test" option. This is called from the cli with the "--test" option.
""" """
ports = meshtastic.util.findPorts() ports = meshtastic.util.findPorts(True)
if len(ports) < 2: if len(ports) < 2:
meshtastic.util.our_exit("Warning: Must have at least two devices connected to USB.") meshtastic.util.our_exit("Warning: Must have at least two devices connected to USB.")

View File

@@ -160,7 +160,7 @@ def test_smoke1_send_hello():
def test_smoke1_port(): def test_smoke1_port():
"""Test --port""" """Test --port"""
# first, get the ports # first, get the ports
ports = findPorts() ports = findPorts(True)
# hopefully there is just one # hopefully there is just one
assert len(ports) == 1 assert len(ports) == 1
port = ports[0] port = ports[0]

View File

@@ -248,6 +248,38 @@ def test_findPorts_when_none_found(patch_comports):
patch_comports.assert_called() patch_comports.assert_called()
@pytest.mark.unitslow
@patch('serial.tools.list_ports.comports')
def test_findPorts_when_duplicate_found_and_duplicate_option_used(patch_comports):
"""Test findPorts()"""
class TempPort:
""" temp class for port"""
def __init__(self, device=None, vid=None):
self.device = device
self.vid = vid
fake1 = TempPort('/dev/cu.usbserial-1430', vid='fake1')
fake2 = TempPort('/dev/cu.wchusbserial1430', vid='fake2')
patch_comports.return_value = [fake1, fake2]
assert findPorts(eliminate_duplicates=True) == ['/dev/cu.wchusbserial1430']
patch_comports.assert_called()
@pytest.mark.unitslow
@patch('serial.tools.list_ports.comports')
def test_findPorts_when_duplicate_found_and_duplicate_option_not_used(patch_comports):
"""Test findPorts()"""
class TempPort:
""" temp class for port"""
def __init__(self, device=None, vid=None):
self.device = device
self.vid = vid
fake1 = TempPort('/dev/cu.usbserial-1430', vid='fake1')
fake2 = TempPort('/dev/cu.wchusbserial1430', vid='fake2')
patch_comports.return_value = [fake1, fake2]
assert findPorts() == ['/dev/cu.usbserial-1430', '/dev/cu.wchusbserial1430']
patch_comports.assert_called()
@pytest.mark.unitslow @pytest.mark.unitslow
def test_convert_mac_addr(): def test_convert_mac_addr():
"""Test convert_mac_addr()""" """Test convert_mac_addr()"""

View File

@@ -113,8 +113,9 @@ def catchAndIgnore(reason, closure):
logging.error(f"Exception thrown in {reason}: {ex}") logging.error(f"Exception thrown in {reason}: {ex}")
def findPorts(): def findPorts(eliminate_duplicates=False):
"""Find all ports that might have meshtastic devices """Find all ports that might have meshtastic devices
eliminate_duplicates will run the eliminate_duplicate_port() on the collection
Returns: Returns:
list -- a list of device paths list -- a list of device paths
@@ -123,6 +124,8 @@ def findPorts():
filter(lambda port: port.vid is not None and port.vid not in blacklistVids, filter(lambda port: port.vid is not None and port.vid not in blacklistVids,
serial.tools.list_ports.comports()))) serial.tools.list_ports.comports())))
l.sort() l.sort()
if eliminate_duplicates:
l = eliminate_duplicate_port(l)
return l return l
@@ -421,7 +424,6 @@ def is_windows11():
if platform.system() == "Windows": if platform.system() == "Windows":
if float(platform.release()) >= 10.0: if float(platform.release()) >= 10.0:
patch = platform.version().split('.')[2] patch = platform.version().split('.')[2]
print(f'patch:{patch}')
# in case they add some number suffix later, just get first 5 chars of patch # in case they add some number suffix later, just get first 5 chars of patch
patch = patch[:5] patch = patch[:5]
try: try:
@@ -429,5 +431,4 @@ def is_windows11():
is_win11 = True is_win11 = True
except Exception as e: except Exception as e:
print(f'problem detecting win11 e:{e}') print(f'problem detecting win11 e:{e}')
print(f'is_win11:{is_win11}')
return is_win11 return is_win11