From ede1b5f08b444674584dc9b7011b1f9586fd571d Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Mon, 7 Mar 2022 21:21:41 -0800 Subject: [PATCH] fix when ports are not sorted --- examples/show_ports.py | 6 ++++++ meshtastic/tests/test_util.py | 19 +++++++++++++++++++ meshtastic/util.py | 1 + 3 files changed, 26 insertions(+) create mode 100644 examples/show_ports.py diff --git a/examples/show_ports.py b/examples/show_ports.py new file mode 100644 index 0000000..30c9cd9 --- /dev/null +++ b/examples/show_ports.py @@ -0,0 +1,6 @@ +"""Simple program to show serial ports. +""" + +from meshtastic.util import findPorts + +print(findPorts()) diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index 548efee..b04fd92 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -264,6 +264,22 @@ def test_findPorts_when_duplicate_found_and_duplicate_option_used(patch_comports patch_comports.assert_called() +@pytest.mark.unitslow +@patch('serial.tools.list_ports.comports') +def test_findPorts_when_duplicate_found_and_duplicate_option_used_ports_reversed(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 = [fake2, fake1] + 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): @@ -315,8 +331,11 @@ def test_eliminate_duplicate_port(): assert eliminate_duplicate_port(['/dev/fake', '/dev/fake1']) == ['/dev/fake', '/dev/fake1'] assert eliminate_duplicate_port(['/dev/fake', '/dev/fake1', '/dev/fake2']) == ['/dev/fake', '/dev/fake1', '/dev/fake2'] assert eliminate_duplicate_port(['/dev/cu.usbserial-1430', '/dev/cu.wchusbserial1430']) == ['/dev/cu.wchusbserial1430'] + assert eliminate_duplicate_port(['/dev/cu.wchusbserial1430', '/dev/cu.usbserial-1430']) == ['/dev/cu.wchusbserial1430'] assert eliminate_duplicate_port(['/dev/cu.SLAB_USBtoUART', '/dev/cu.usbserial-0001']) == ['/dev/cu.usbserial-0001'] + assert eliminate_duplicate_port(['/dev/cu.usbserial-0001', '/dev/cu.SLAB_USBtoUART']) == ['/dev/cu.usbserial-0001'] assert eliminate_duplicate_port(['/dev/cu.usbmodem11301', '/dev/cu.wchusbserial11301']) == ['/dev/cu.wchusbserial11301'] + assert eliminate_duplicate_port(['/dev/cu.wchusbserial11301', '/dev/cu.usbmodem11301']) == ['/dev/cu.wchusbserial11301'] @patch('platform.version', return_value='10.0.22000.194') @patch('platform.release', return_value='10') diff --git a/meshtastic/util.py b/meshtastic/util.py index 1931621..79cf36f 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -401,6 +401,7 @@ def eliminate_duplicate_port(ports): if len(ports) != 2: new_ports = ports else: + ports.sort() if 'usbserial' in ports[0] and 'wchusbserial' in ports[1]: first = ports[0].replace("usbserial-", "") second = ports[1].replace("wchusbserial", "")