|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch |
| 3 | +import esptool |
| 4 | + |
| 5 | +# Espressif VID constant (same as in esptool/__init__.py) |
| 6 | +ESPRESSIF_VID = 0x303A |
| 7 | + |
| 8 | + |
| 9 | +class MockPort: |
| 10 | + """Mock serial port object that mimics pyserial's ListPortInfo""" |
| 11 | + |
| 12 | + def __init__(self, device, vid=None, pid=None, name=None, serial_number=None): |
| 13 | + self.device = device |
| 14 | + self.vid = vid |
| 15 | + self.pid = pid |
| 16 | + self.name = name |
| 17 | + self.serial_number = serial_number |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.host_test |
| 21 | +class TestPortSorting: |
| 22 | + """Test the port sorting algorithm in get_port_list function""" |
| 23 | + |
| 24 | + def test_linux_port_sorting(self): |
| 25 | + """Test port sorting on Linux platform""" |
| 26 | + mock_ports = [ |
| 27 | + MockPort("/dev/ttyS0", vid=0x1234), |
| 28 | + MockPort("/dev/ttyS1", vid=0x1234), |
| 29 | + MockPort("/dev/ttyUSB0", vid=0x1234), |
| 30 | + MockPort("/dev/ttyUSB1", vid=0x1234), |
| 31 | + MockPort("/dev/ttyACM1", vid=ESPRESSIF_VID), |
| 32 | + MockPort("/dev/ttyACM0", vid=0x1234), |
| 33 | + ] |
| 34 | + |
| 35 | + with ( |
| 36 | + patch("sys.platform", "linux"), |
| 37 | + patch("esptool.list_ports") as mock_list_ports, |
| 38 | + ): |
| 39 | + mock_list_ports.comports.return_value = mock_ports |
| 40 | + |
| 41 | + result = esptool.get_port_list() |
| 42 | + |
| 43 | + # Expected sorting order (alphabetically within each group): |
| 44 | + # 1. Other devices (priority 1) |
| 45 | + # 2. ttyUSB*/ttyACM* devices (priority 2) |
| 46 | + # 3. Espressif VID devices (priority 3) - highest priority, appear LAST |
| 47 | + expected = [ |
| 48 | + "/dev/ttyS0", # Other |
| 49 | + "/dev/ttyS1", # Other |
| 50 | + "/dev/ttyACM0", # USB/ACM |
| 51 | + "/dev/ttyUSB0", # USB/ACM |
| 52 | + "/dev/ttyUSB1", # USB/ACM |
| 53 | + "/dev/ttyACM1", # Espressif VID |
| 54 | + ] |
| 55 | + |
| 56 | + assert result == expected |
| 57 | + |
| 58 | + def test_macos_port_sorting(self): |
| 59 | + """Test port sorting on macOS platform""" |
| 60 | + mock_ports = [ |
| 61 | + MockPort("/dev/cu.wlan-debug", vid=0x1234), # Excluded by macOS filter |
| 62 | + MockPort( |
| 63 | + "/dev/cu.Bluetooth-Incoming-Port", vid=0x1234 |
| 64 | + ), # Excluded by macOS filter |
| 65 | + MockPort("/dev/cu.debug-console", vid=0x1234), # Excluded by macOS filter |
| 66 | + MockPort("/dev/cu.usbserial2", vid=0x1234), |
| 67 | + MockPort("/dev/cu.usbmodem1", vid=0x1234), |
| 68 | + MockPort("/dev/cu.usbmodem2", vid=ESPRESSIF_VID), |
| 69 | + MockPort("/dev/cu.usbserial1", vid=0x1234), |
| 70 | + ] |
| 71 | + |
| 72 | + with ( |
| 73 | + patch("sys.platform", "darwin"), |
| 74 | + patch("esptool.list_ports") as mock_list_ports, |
| 75 | + ): |
| 76 | + mock_list_ports.comports.return_value = mock_ports |
| 77 | + |
| 78 | + result = esptool.get_port_list() |
| 79 | + |
| 80 | + # Expected sorting order (alphabetically within each group): |
| 81 | + # 1. Other devices (priority 1) |
| 82 | + # 2. usbserial*/usbmodem* devices (priority 2) |
| 83 | + # 3. Espressif VID devices (priority 3) - highest priority, appear LAST |
| 84 | + # Note: wlan-debug, Bluetooth-Incoming-Port, debug-console are excluded |
| 85 | + expected = [ |
| 86 | + "/dev/cu.usbmodem1", # usbmodem |
| 87 | + "/dev/cu.usbserial1", # usbserial |
| 88 | + "/dev/cu.usbserial2", # usbserial |
| 89 | + "/dev/cu.usbmodem2", # Espressif VID |
| 90 | + ] |
| 91 | + |
| 92 | + assert result == expected |
| 93 | + |
| 94 | + def test_windows_port_sorting(self): |
| 95 | + """Test port sorting on Windows platform""" |
| 96 | + mock_ports = [ |
| 97 | + MockPort("COM3", vid=0x1234), |
| 98 | + MockPort("COM1", vid=0x1234), |
| 99 | + MockPort("COM10", vid=0x1234), |
| 100 | + MockPort("COM5", vid=ESPRESSIF_VID), |
| 101 | + MockPort("COM2", vid=0x1234), |
| 102 | + ] |
| 103 | + |
| 104 | + with ( |
| 105 | + patch("sys.platform", "win32"), |
| 106 | + patch("esptool.list_ports") as mock_list_ports, |
| 107 | + ): |
| 108 | + mock_list_ports.comports.return_value = mock_ports |
| 109 | + |
| 110 | + result = esptool.get_port_list() |
| 111 | + |
| 112 | + # Expected sorting order (alphabetically within each group): |
| 113 | + # 1. All other COM ports (priority 1) |
| 114 | + # 2. Espressif VID devices (priority 2) - highest priority, appear LAST |
| 115 | + expected = [ |
| 116 | + "COM1", # Other |
| 117 | + "COM10", # Other |
| 118 | + "COM2", # Other |
| 119 | + "COM3", # Other |
| 120 | + "COM5", # Espressif VID |
| 121 | + ] |
| 122 | + |
| 123 | + assert result == expected |
| 124 | + |
| 125 | + def test_port_filtering_parameters(self): |
| 126 | + """Test port filtering with various parameters while maintaining sorting""" |
| 127 | + mock_ports = [ |
| 128 | + MockPort( |
| 129 | + "/dev/ttyUSB0", |
| 130 | + vid=0x1234, |
| 131 | + pid=0x5678, |
| 132 | + name="USB Serial", |
| 133 | + serial_number="ABC123", |
| 134 | + ), |
| 135 | + MockPort( |
| 136 | + "/dev/ttyUSB1", |
| 137 | + vid=ESPRESSIF_VID, |
| 138 | + pid=0x1001, |
| 139 | + name="ESP32", |
| 140 | + serial_number="ESP001", |
| 141 | + ), |
| 142 | + MockPort( |
| 143 | + "/dev/ttyUSB2", |
| 144 | + vid=ESPRESSIF_VID, |
| 145 | + pid=0x1002, |
| 146 | + name="ESP32-S3", |
| 147 | + serial_number="ESP002", |
| 148 | + ), |
| 149 | + ] |
| 150 | + |
| 151 | + with ( |
| 152 | + patch("sys.platform", "linux"), |
| 153 | + patch("esptool.list_ports") as mock_list_ports, |
| 154 | + ): |
| 155 | + mock_list_ports.comports.return_value = mock_ports |
| 156 | + |
| 157 | + # Test VID filtering - Espressif devices appear last |
| 158 | + result = esptool.get_port_list(vids=[ESPRESSIF_VID]) |
| 159 | + expected = ["/dev/ttyUSB1", "/dev/ttyUSB2"] |
| 160 | + assert result == expected |
| 161 | + |
| 162 | + # Test PID filtering |
| 163 | + result = esptool.get_port_list(pids=[0x1001]) |
| 164 | + expected = ["/dev/ttyUSB1"] |
| 165 | + assert result == expected |
| 166 | + |
| 167 | + # Test name filtering |
| 168 | + result = esptool.get_port_list(names=["ESP32"]) |
| 169 | + expected = ["/dev/ttyUSB1", "/dev/ttyUSB2"] |
| 170 | + assert result == expected |
| 171 | + |
| 172 | + # Test serial filtering |
| 173 | + result = esptool.get_port_list(serials=["ESP"]) |
| 174 | + expected = ["/dev/ttyUSB1", "/dev/ttyUSB2"] |
| 175 | + assert result == expected |
0 commit comments