Skip to content

Commit 96e42ac

Browse files
committed
add duplicate check
1 parent 9214b2f commit 96e42ac

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

meshtastic/supported_device.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import subprocess
77
import re
88

9+
import meshtastic.util
10+
911
# Goal is to detect which device and port to use from the supported devices
1012
# without installing any libraries that are not currently in the python meshtastic library
1113

@@ -109,7 +111,7 @@ def get_devices_with_vendor_id(vid):
109111
sd.add(d)
110112
return sd
111113

112-
def active_ports_on_supported_devices(sds):
114+
def active_ports_on_supported_devices(sds, eliminate_duplicates=False):
113115
"""Return a set of active ports based on the supplied supported devices"""
114116
ports = set()
115117
baseports = set()
@@ -170,6 +172,10 @@ def active_ports_on_supported_devices(sds):
170172
# add all ports
171173
for com_port in com_ports:
172174
ports.add(com_port)
175+
if eliminate_duplicates:
176+
ports = meshtastic.util.eliminate_duplicate_port(list(ports))
177+
ports.sort()
178+
ports = set(ports)
173179
return ports
174180

175181

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""Meshtastic unit tests for supported_device.py"""
2+
3+
4+
from unittest.mock import patch
5+
import pytest
6+
7+
from meshtastic.supported_device import active_ports_on_supported_devices, SupportedDevice
8+
9+
10+
@pytest.mark.unit
11+
@patch('platform.system', return_value='Linux')
12+
def test_active_ports_on_supported_devices_empty(mock_platform):
13+
"""Test active_ports_on_supported_devices()"""
14+
sds = set()
15+
assert active_ports_on_supported_devices(sds) == set()
16+
mock_platform.assert_called()
17+
18+
19+
@pytest.mark.unit
20+
@patch('subprocess.getstatusoutput')
21+
@patch('platform.system', return_value='Linux')
22+
def test_active_ports_on_supported_devices_linux(mock_platform, mock_sp):
23+
"""Test active_ports_on_supported_devices()"""
24+
mock_sp.return_value = (None, 'crw-rw-rw- 1 root wheel 0x9000000 Feb 8 22:22 /dev/ttyUSBfake')
25+
fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1', baseport_on_linux='ttyUSB')
26+
fake_supported_devices = [fake_device]
27+
assert active_ports_on_supported_devices(fake_supported_devices) == {'/dev/ttyUSBfake'}
28+
mock_platform.assert_called()
29+
mock_sp.assert_called()
30+
31+
32+
@pytest.mark.unit
33+
@patch('subprocess.getstatusoutput')
34+
@patch('platform.system', return_value='Darwin')
35+
def test_active_ports_on_supported_devices_mac(mock_platform, mock_sp):
36+
"""Test active_ports_on_supported_devices()"""
37+
mock_sp.return_value = (None, 'crw-rw-rw- 1 root wheel 0x9000000 Feb 8 22:22 /dev/cu.usbserial-foo')
38+
fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1', baseport_on_linux='cu.usbserial-')
39+
fake_supported_devices = [fake_device]
40+
assert active_ports_on_supported_devices(fake_supported_devices) == {'/dev/cu.usbserial-foo'}
41+
mock_platform.assert_called()
42+
mock_sp.assert_called()
43+
44+
45+
@pytest.mark.unit
46+
@patch('meshtastic.supported_device.detect_windows_port', return_value={'COM2'})
47+
@patch('platform.system', return_value='Windows')
48+
def test_active_ports_on_supported_devices_win(mock_platform, mock_dwp):
49+
"""Test active_ports_on_supported_devices()"""
50+
fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1')
51+
fake_supported_devices = [fake_device]
52+
assert active_ports_on_supported_devices(fake_supported_devices) == {'COM2'}
53+
mock_platform.assert_called()
54+
mock_dwp.assert_called()
55+
56+
57+
@pytest.mark.unit
58+
@patch('subprocess.getstatusoutput')
59+
@patch('platform.system', return_value='Darwin')
60+
def test_active_ports_on_supported_devices_mac_no_duplicates_check(mock_platform, mock_sp):
61+
"""Test active_ports_on_supported_devices()"""
62+
mock_sp.return_value = (None, ('crw-rw-rw- 1 root wheel 0x9000005 Mar 8 10:05 /dev/cu.usbmodem53230051441\n'
63+
'crw-rw-rw- 1 root wheel 0x9000003 Mar 8 10:06 /dev/cu.wchusbserial53230051441'))
64+
fake_device = SupportedDevice(name='a', for_firmware='tbeam', baseport_on_mac='cu.usbmodem')
65+
fake_supported_devices = [fake_device]
66+
assert active_ports_on_supported_devices(fake_supported_devices, False) == {'/dev/cu.usbmodem53230051441', '/dev/cu.wchusbserial53230051441'}
67+
mock_platform.assert_called()
68+
mock_sp.assert_called()
69+
70+
71+
@pytest.mark.unit
72+
@patch('subprocess.getstatusoutput')
73+
@patch('platform.system', return_value='Darwin')
74+
def test_active_ports_on_supported_devices_mac_duplicates_check(mock_platform, mock_sp):
75+
"""Test active_ports_on_supported_devices()"""
76+
mock_sp.return_value = (None, ('crw-rw-rw- 1 root wheel 0x9000005 Mar 8 10:05 /dev/cu.usbmodem53230051441\n'
77+
'crw-rw-rw- 1 root wheel 0x9000003 Mar 8 10:06 /dev/cu.wchusbserial53230051441'))
78+
fake_device = SupportedDevice(name='a', for_firmware='tbeam', baseport_on_mac='cu.usbmodem')
79+
fake_supported_devices = [fake_device]
80+
assert active_ports_on_supported_devices(fake_supported_devices, True) == {'/dev/cu.wchusbserial53230051441'}
81+
mock_platform.assert_called()
82+
mock_sp.assert_called()

meshtastic/tests/test_util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ def test_eliminate_duplicate_port():
317317
assert eliminate_duplicate_port(['/dev/cu.usbserial-1430', '/dev/cu.wchusbserial1430']) == ['/dev/cu.wchusbserial1430']
318318
assert eliminate_duplicate_port(['/dev/cu.SLAB_USBtoUART', '/dev/cu.usbserial-0001']) == ['/dev/cu.usbserial-0001']
319319
assert eliminate_duplicate_port(['/dev/cu.usbmodem11301', '/dev/cu.wchusbserial11301']) == ['/dev/cu.wchusbserial11301']
320+
assert eliminate_duplicate_port(['/dev/cu.usbmodem53230051441', '/dev/cu.wchusbserial53230051441']) == ['/dev/cu.wchusbserial53230051441']
321+
assert eliminate_duplicate_port(['/dev/cu.wchusbserial53230051441', '/dev/cu.usbmodem53230051441']) == ['/dev/cu.wchusbserial53230051441']
322+
320323

321324
@patch('platform.version', return_value='10.0.22000.194')
322325
@patch('platform.release', return_value='10')

meshtastic/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ def eliminate_duplicate_port(ports):
401401
if len(ports) != 2:
402402
new_ports = ports
403403
else:
404+
ports.sort()
404405
if 'usbserial' in ports[0] and 'wchusbserial' in ports[1]:
405406
first = ports[0].replace("usbserial-", "")
406407
second = ports[1].replace("wchusbserial", "")

0 commit comments

Comments
 (0)