|
12 | 12 | remove_keys_from_dict, Timeout, hexstr, |
13 | 13 | ipstr, readnet_u16, findPorts, convert_mac_addr, |
14 | 14 | snake_to_camel, camel_to_snake, eliminate_duplicate_port, |
15 | | - is_windows11) |
| 15 | + is_windows11, active_ports_on_supported_devices) |
| 16 | + |
| 17 | +from meshtastic.supported_device import SupportedDevice |
16 | 18 |
|
17 | 19 |
|
18 | 20 | @pytest.mark.unit |
@@ -336,6 +338,8 @@ def test_eliminate_duplicate_port(): |
336 | 338 | assert eliminate_duplicate_port(['/dev/cu.usbserial-0001', '/dev/cu.SLAB_USBtoUART']) == ['/dev/cu.usbserial-0001'] |
337 | 339 | assert eliminate_duplicate_port(['/dev/cu.usbmodem11301', '/dev/cu.wchusbserial11301']) == ['/dev/cu.wchusbserial11301'] |
338 | 340 | assert eliminate_duplicate_port(['/dev/cu.wchusbserial11301', '/dev/cu.usbmodem11301']) == ['/dev/cu.wchusbserial11301'] |
| 341 | + assert eliminate_duplicate_port(['/dev/cu.usbmodem53230051441', '/dev/cu.wchusbserial53230051441']) == ['/dev/cu.wchusbserial53230051441'] |
| 342 | + assert eliminate_duplicate_port(['/dev/cu.wchusbserial53230051441', '/dev/cu.usbmodem53230051441']) == ['/dev/cu.wchusbserial53230051441'] |
339 | 343 |
|
340 | 344 | @patch('platform.version', return_value='10.0.22000.194') |
341 | 345 | @patch('platform.release', return_value='10') |
@@ -377,3 +381,78 @@ def test_is_windows11_false_win8_1(patched_platform, patched_release): |
377 | 381 | assert is_windows11() is False |
378 | 382 | patched_platform.assert_called() |
379 | 383 | patched_release.assert_called() |
| 384 | + |
| 385 | + |
| 386 | +@pytest.mark.unit |
| 387 | +@patch('platform.system', return_value='Linux') |
| 388 | +def test_active_ports_on_supported_devices_empty(mock_platform): |
| 389 | + """Test active_ports_on_supported_devices()""" |
| 390 | + sds = set() |
| 391 | + assert active_ports_on_supported_devices(sds) == set() |
| 392 | + mock_platform.assert_called() |
| 393 | + |
| 394 | + |
| 395 | +@pytest.mark.unit |
| 396 | +@patch('subprocess.getstatusoutput') |
| 397 | +@patch('platform.system', return_value='Linux') |
| 398 | +def test_active_ports_on_supported_devices_linux(mock_platform, mock_sp): |
| 399 | + """Test active_ports_on_supported_devices()""" |
| 400 | + mock_sp.return_value = (None, 'crw-rw-rw- 1 root wheel 0x9000000 Feb 8 22:22 /dev/ttyUSBfake') |
| 401 | + fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1', baseport_on_linux='ttyUSB') |
| 402 | + fake_supported_devices = [fake_device] |
| 403 | + assert active_ports_on_supported_devices(fake_supported_devices) == {'/dev/ttyUSBfake'} |
| 404 | + mock_platform.assert_called() |
| 405 | + mock_sp.assert_called() |
| 406 | + |
| 407 | + |
| 408 | +@pytest.mark.unit |
| 409 | +@patch('subprocess.getstatusoutput') |
| 410 | +@patch('platform.system', return_value='Darwin') |
| 411 | +def test_active_ports_on_supported_devices_mac(mock_platform, mock_sp): |
| 412 | + """Test active_ports_on_supported_devices()""" |
| 413 | + mock_sp.return_value = (None, 'crw-rw-rw- 1 root wheel 0x9000000 Feb 8 22:22 /dev/cu.usbserial-foo') |
| 414 | + fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1', baseport_on_linux='cu.usbserial-') |
| 415 | + fake_supported_devices = [fake_device] |
| 416 | + assert active_ports_on_supported_devices(fake_supported_devices) == {'/dev/cu.usbserial-foo'} |
| 417 | + mock_platform.assert_called() |
| 418 | + mock_sp.assert_called() |
| 419 | + |
| 420 | + |
| 421 | +@pytest.mark.unit |
| 422 | +@patch('meshtastic.util.detect_windows_port', return_value={'COM2'}) |
| 423 | +@patch('platform.system', return_value='Windows') |
| 424 | +def test_active_ports_on_supported_devices_win(mock_platform, mock_dwp): |
| 425 | + """Test active_ports_on_supported_devices()""" |
| 426 | + fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1') |
| 427 | + fake_supported_devices = [fake_device] |
| 428 | + assert active_ports_on_supported_devices(fake_supported_devices) == {'COM2'} |
| 429 | + mock_platform.assert_called() |
| 430 | + mock_dwp.assert_called() |
| 431 | + |
| 432 | + |
| 433 | +@pytest.mark.unit |
| 434 | +@patch('subprocess.getstatusoutput') |
| 435 | +@patch('platform.system', return_value='Darwin') |
| 436 | +def test_active_ports_on_supported_devices_mac_no_duplicates_check(mock_platform, mock_sp): |
| 437 | + """Test active_ports_on_supported_devices()""" |
| 438 | + mock_sp.return_value = (None, ('crw-rw-rw- 1 root wheel 0x9000005 Mar 8 10:05 /dev/cu.usbmodem53230051441\n' |
| 439 | + 'crw-rw-rw- 1 root wheel 0x9000003 Mar 8 10:06 /dev/cu.wchusbserial53230051441')) |
| 440 | + fake_device = SupportedDevice(name='a', for_firmware='tbeam', baseport_on_mac='cu.usbmodem') |
| 441 | + fake_supported_devices = [fake_device] |
| 442 | + assert active_ports_on_supported_devices(fake_supported_devices, False) == {'/dev/cu.usbmodem53230051441', '/dev/cu.wchusbserial53230051441'} |
| 443 | + mock_platform.assert_called() |
| 444 | + mock_sp.assert_called() |
| 445 | + |
| 446 | + |
| 447 | +@pytest.mark.unit |
| 448 | +@patch('subprocess.getstatusoutput') |
| 449 | +@patch('platform.system', return_value='Darwin') |
| 450 | +def test_active_ports_on_supported_devices_mac_duplicates_check(mock_platform, mock_sp): |
| 451 | + """Test active_ports_on_supported_devices()""" |
| 452 | + mock_sp.return_value = (None, ('crw-rw-rw- 1 root wheel 0x9000005 Mar 8 10:05 /dev/cu.usbmodem53230051441\n' |
| 453 | + 'crw-rw-rw- 1 root wheel 0x9000003 Mar 8 10:06 /dev/cu.wchusbserial53230051441')) |
| 454 | + fake_device = SupportedDevice(name='a', for_firmware='tbeam', baseport_on_mac='cu.usbmodem') |
| 455 | + fake_supported_devices = [fake_device] |
| 456 | + assert active_ports_on_supported_devices(fake_supported_devices, True) == {'/dev/cu.wchusbserial53230051441'} |
| 457 | + mock_platform.assert_called() |
| 458 | + mock_sp.assert_called() |
0 commit comments