|
| 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() |
0 commit comments