|
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 |
@@ -361,3 +363,78 @@ def test_is_windows11_false_win8_1(patched_platform, patched_release): |
361 | 363 | assert is_windows11() is False |
362 | 364 | patched_platform.assert_called() |
363 | 365 | patched_release.assert_called() |
| 366 | + |
| 367 | + |
| 368 | +@pytest.mark.unit |
| 369 | +@patch('platform.system', return_value='Linux') |
| 370 | +def test_active_ports_on_supported_devices_empty(mock_platform): |
| 371 | + """Test active_ports_on_supported_devices()""" |
| 372 | + sds = set() |
| 373 | + assert active_ports_on_supported_devices(sds) == set() |
| 374 | + mock_platform.assert_called() |
| 375 | + |
| 376 | + |
| 377 | +@pytest.mark.unit |
| 378 | +@patch('subprocess.getstatusoutput') |
| 379 | +@patch('platform.system', return_value='Linux') |
| 380 | +def test_active_ports_on_supported_devices_linux(mock_platform, mock_sp): |
| 381 | + """Test active_ports_on_supported_devices()""" |
| 382 | + mock_sp.return_value = (None, 'crw-rw-rw- 1 root wheel 0x9000000 Feb 8 22:22 /dev/ttyUSBfake') |
| 383 | + fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1', baseport_on_linux='ttyUSB') |
| 384 | + fake_supported_devices = [fake_device] |
| 385 | + assert active_ports_on_supported_devices(fake_supported_devices) == {'/dev/ttyUSBfake'} |
| 386 | + mock_platform.assert_called() |
| 387 | + mock_sp.assert_called() |
| 388 | + |
| 389 | + |
| 390 | +@pytest.mark.unit |
| 391 | +@patch('subprocess.getstatusoutput') |
| 392 | +@patch('platform.system', return_value='Darwin') |
| 393 | +def test_active_ports_on_supported_devices_mac(mock_platform, mock_sp): |
| 394 | + """Test active_ports_on_supported_devices()""" |
| 395 | + mock_sp.return_value = (None, 'crw-rw-rw- 1 root wheel 0x9000000 Feb 8 22:22 /dev/cu.usbserial-foo') |
| 396 | + fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1', baseport_on_linux='cu.usbserial-') |
| 397 | + fake_supported_devices = [fake_device] |
| 398 | + assert active_ports_on_supported_devices(fake_supported_devices) == {'/dev/cu.usbserial-foo'} |
| 399 | + mock_platform.assert_called() |
| 400 | + mock_sp.assert_called() |
| 401 | + |
| 402 | + |
| 403 | +@pytest.mark.unit |
| 404 | +@patch('meshtastic.util.detect_windows_port', return_value={'COM2'}) |
| 405 | +@patch('platform.system', return_value='Windows') |
| 406 | +def test_active_ports_on_supported_devices_win(mock_platform, mock_dwp): |
| 407 | + """Test active_ports_on_supported_devices()""" |
| 408 | + fake_device = SupportedDevice(name='a', for_firmware='heltec-v2.1') |
| 409 | + fake_supported_devices = [fake_device] |
| 410 | + assert active_ports_on_supported_devices(fake_supported_devices) == {'COM2'} |
| 411 | + mock_platform.assert_called() |
| 412 | + mock_dwp.assert_called() |
| 413 | + |
| 414 | + |
| 415 | +@pytest.mark.unit |
| 416 | +@patch('subprocess.getstatusoutput') |
| 417 | +@patch('platform.system', return_value='Darwin') |
| 418 | +def test_active_ports_on_supported_devices_mac_no_duplicates_check(mock_platform, mock_sp): |
| 419 | + """Test active_ports_on_supported_devices()""" |
| 420 | + mock_sp.return_value = (None, ('crw-rw-rw- 1 root wheel 0x9000005 Mar 8 10:05 /dev/cu.usbmodem53230051441\n' |
| 421 | + 'crw-rw-rw- 1 root wheel 0x9000003 Mar 8 10:06 /dev/cu.wchusbserial53230051441')) |
| 422 | + fake_device = SupportedDevice(name='a', for_firmware='tbeam', baseport_on_mac='cu.usbmodem') |
| 423 | + fake_supported_devices = [fake_device] |
| 424 | + assert active_ports_on_supported_devices(fake_supported_devices, False) == {'/dev/cu.usbmodem53230051441', '/dev/cu.wchusbserial53230051441'} |
| 425 | + mock_platform.assert_called() |
| 426 | + mock_sp.assert_called() |
| 427 | + |
| 428 | + |
| 429 | +@pytest.mark.unit |
| 430 | +@patch('subprocess.getstatusoutput') |
| 431 | +@patch('platform.system', return_value='Darwin') |
| 432 | +def test_active_ports_on_supported_devices_mac_duplicates_check(mock_platform, mock_sp): |
| 433 | + """Test active_ports_on_supported_devices()""" |
| 434 | + mock_sp.return_value = (None, ('crw-rw-rw- 1 root wheel 0x9000005 Mar 8 10:05 /dev/cu.usbmodem53230051441\n' |
| 435 | + 'crw-rw-rw- 1 root wheel 0x9000003 Mar 8 10:06 /dev/cu.wchusbserial53230051441')) |
| 436 | + fake_device = SupportedDevice(name='a', for_firmware='tbeam', baseport_on_mac='cu.usbmodem') |
| 437 | + fake_supported_devices = [fake_device] |
| 438 | + assert active_ports_on_supported_devices(fake_supported_devices, True) == {'/dev/cu.wchusbserial53230051441'} |
| 439 | + mock_platform.assert_called() |
| 440 | + mock_sp.assert_called() |
0 commit comments