Skip to content

Commit 351140a

Browse files
committed
feat: add --port-by-serial option to filter ports by serial number
1 parent f7e2991 commit 351140a

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

pytest-embedded-serial-esp/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ requires-python = ">=3.7"
3232

3333
dependencies = [
3434
"pytest-embedded-serial~=1.16.1",
35-
"esptool~=4.5",
35+
"esptool~=4.9.0",
3636
]
3737

3838
[project.urls]

pytest-embedded-serial-esp/pytest_embedded_serial_esp/serial.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(
5555
target: Optional[str] = None,
5656
beta_target: Optional[str] = None,
5757
port: Optional[str] = None,
58+
port_by_serial: Optional[str] = None,
5859
port_mac: Optional[str] = None,
5960
baud: int = Serial.DEFAULT_BAUDRATE,
6061
esptool_baud: int = ESPTOOL_DEFAULT_BAUDRATE,
@@ -66,11 +67,14 @@ def __init__(
6667
**kwargs,
6768
) -> None:
6869
self._meta = meta
70+
filters = {}
71+
if port_by_serial:
72+
filters['serials'] = [s.strip() for s in port_by_serial.split(',') if s.strip()]
6973

7074
esptool_target = beta_target or target or 'auto'
7175
if port is None or port.endswith('*'):
7276
port_filter = port.strip('*') if port else ''
73-
available_ports = [_p for _p in esptool.get_port_list() if port_filter in _p]
77+
available_ports = [_p for _p in esptool.get_port_list(**filters) if port_filter in _p]
7478
ports = list(set(available_ports) - set(self.occupied_ports.keys()) - set(ports_to_occupy))
7579

7680
# sort to make /dev/ttyS* ports before /dev/ttyUSB* ports

pytest-embedded/pytest_embedded/dut_factory.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def _fixture_classes_and_options_fn(
120120
app_path,
121121
build_dir,
122122
port,
123+
port_by_serial,
123124
port_location,
124125
port_mac,
125126
target,
@@ -210,6 +211,7 @@ def _fixture_classes_and_options_fn(
210211
'target': target,
211212
'beta_target': beta_target,
212213
'port': os.getenv('ESPPORT') or port,
214+
'port_by_serial': port_by_serial,
213215
'port_location': port_location,
214216
'port_mac': port_mac,
215217
'baud': int(baud or EspSerial.DEFAULT_BAUDRATE),
@@ -632,6 +634,7 @@ def create(
632634
app_path: str = '',
633635
build_dir: str = 'build',
634636
port: t.Optional[str] = None,
637+
port_by_serial: t.Optional[str] = None,
635638
port_location: t.Optional[str] = None,
636639
port_mac: t.Optional[str] = None,
637640
target: t.Optional[str] = None,
@@ -680,6 +683,7 @@ def create(
680683
app_path: Path to the application.
681684
build_dir: Directory for build output (default is 'build').
682685
port: Port configuration.
686+
port_by_serial: Filters the port based on the specified serial number.
683687
port_location: Port location.
684688
port_mac: Port MAC address.
685689
target: Target configuration.
@@ -744,6 +748,7 @@ def create(
744748
'app_path': app_path,
745749
'build_dir': build_dir,
746750
'port': port,
751+
'port_by_serial': port_by_serial,
747752
'port_location': port_location,
748753
'port_mac': port_mac,
749754
'target': target,

pytest-embedded/pytest_embedded/plugin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def pytest_addoption(parser):
203203
'--port-mac',
204204
help='MAC address of the board. (Default: None)',
205205
)
206+
esp_group.addoption(
207+
'--port-by-serial', help='Comma-separated list of serial numbers to filter ports by. (Default: None)'
208+
)
206209
esp_group.addoption(
207210
'--esp-flash-force',
208211
action='store_true',
@@ -855,6 +858,13 @@ def port_mac(request: FixtureRequest) -> t.Optional[str]:
855858
return _request_param_or_config_option_or_default(request, 'port_mac', None)
856859

857860

861+
@pytest.fixture
862+
@multi_dut_argument
863+
def port_by_serial(request: FixtureRequest) -> t.Optional[str]:
864+
"""Enable parametrization for the same cli option"""
865+
return _request_param_or_config_option_or_default(request, 'port_by_serial', None)
866+
867+
858868
#######
859869
# idf #
860870
#######
@@ -1044,6 +1054,7 @@ def parametrize_fixtures(
10441054
app_path,
10451055
build_dir,
10461056
port,
1057+
port_by_serial,
10471058
port_location,
10481059
port_mac,
10491060
target,

0 commit comments

Comments
 (0)