Skip to content

Commit

Permalink
fix: changed logic for flash port
Browse files Browse the repository at this point in the history
  • Loading branch information
horw committed Jul 26, 2024
1 parent a1ce3ac commit c93c77a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def __init__(
self._meta = meta

esptool_target = beta_target or target or 'auto'
if port is None:
available_ports = esptool.get_port_list()
if port is None or port.endswith('*'):
port_filter = port.strip('*') if port else ''
available_ports = [_p for _p in esptool.get_port_list() if port_filter in _p]
ports = list(set(available_ports) - set(self.occupied_ports.keys()))

# sort to make /dev/ttyS* ports before /dev/ttyUSB* ports
Expand Down
8 changes: 6 additions & 2 deletions pytest-embedded-serial/pytest_embedded_serial/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
port_location: Optional[str] = None,
baud: int = DEFAULT_BAUDRATE,
meta: Optional[Meta] = None,
stop_after_init: bool = False,
**kwargs,
):
self._q = msg_queue
Expand Down Expand Up @@ -88,9 +89,12 @@ def __init__(

self._post_init()
self._start()
self._finalize_init()

self.start_redirect_thread()
if not stop_after_init:
self._finalize_init()
self.start_redirect_thread()
else:
self.close()

def start_redirect_thread(self) -> None:
if self._redirect_thread and self._redirect_thread.is_alive():
Expand Down
22 changes: 22 additions & 0 deletions pytest-embedded/pytest_embedded/dut_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import subprocess
import sys
import time
import typing as t
from collections import defaultdict
from pathlib import Path
Expand Down Expand Up @@ -127,6 +128,7 @@ def _fixture_classes_and_options_fn(
target,
beta_target,
baud,
flash_port,
skip_autoflash,
erase_all,
esptool_baud,
Expand Down Expand Up @@ -159,6 +161,7 @@ def _fixture_classes_and_options_fn(
pexpect_proc,
msg_queue,
_meta,
**kwargs,
) -> ClassCliOptions:
classes: t.Dict[str, type] = {}
mixins: t.Dict[str, t.List[type]] = defaultdict(list)
Expand Down Expand Up @@ -211,6 +214,7 @@ def _fixture_classes_and_options_fn(
'baud': int(baud or EspSerial.DEFAULT_BAUDRATE),
'esptool_baud': int(os.getenv('ESPBAUD') or esptool_baud or EspSerial.ESPTOOL_DEFAULT_BAUDRATE),
'esp_flash_force': esp_flash_force,
'flash_port': flash_port,
'skip_autoflash': skip_autoflash,
'erase_all': erase_all,
'meta': _meta,
Expand Down Expand Up @@ -408,6 +412,21 @@ def serial_gn(_fixture_classes_and_options, msg_queue, app) -> t.Optional[t.Unio
kwargs = _fixture_classes_and_options.kwargs['serial']
if 'app' in kwargs and kwargs['app'] is None:
kwargs['app'] = app

if kwargs.get('flash_port'):
operation_port = kwargs.pop('port', None)
if operation_port is None:
raise SystemExit('If the flash port was set up, the port should also be set up.')

flash_port = kwargs.pop('flash_port')
kwargs['stop_after_init'] = True
kwargs['port'] = flash_port
cls(**_drop_none_kwargs(kwargs))
time.sleep(3) # time for device restart
kwargs['stop_after_init'] = False
kwargs['port'] = operation_port
kwargs['skip_autoflash'] = True

return cls(**_drop_none_kwargs(kwargs))


Expand Down Expand Up @@ -586,6 +605,7 @@ def create(
target: t.Optional[str] = None,
beta_target: t.Optional[str] = None,
baud: t.Optional[int] = None,
flash_port: t.Optional[str] = None,
skip_autoflash: t.Optional[bool] = None,
erase_all: t.Optional[bool] = None,
esptool_baud: t.Optional[int] = None,
Expand Down Expand Up @@ -633,6 +653,7 @@ def create(
target: Target configuration.
beta_target: Beta target configuration.
baud: Baud rate.
flash_port: Port used for flashing the app.
skip_autoflash: Skip autoflash flag.
erase_all: Erase all flag.
esptool_baud: ESP tool baud rate.
Expand Down Expand Up @@ -696,6 +717,7 @@ def create(
'target': target,
'beta_target': beta_target,
'baud': baud,
'flash_port': flash_port,
'skip_autoflash': skip_autoflash,
'erase_all': erase_all,
'esptool_baud': esptool_baud,
Expand Down
13 changes: 13 additions & 0 deletions pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ def pytest_addoption(parser):
'y/yes/true for True and n/no/false for False. '
'(Default: False, parametrization not supported, `|` will be escaped to `-`)',
)
esp_group.addoption(
'--flash-port',
help='serial port for flashing. Only set this value when the flashing port is different from the serial port.'
'(Default: None)',
)
esp_group.addoption(
'--skip-autoflash',
help='y/yes/true for True and n/no/false for False. Set to True to disable auto flash. (Default: False)',
Expand Down Expand Up @@ -786,6 +791,13 @@ def beta_target(request: FixtureRequest) -> t.Optional[str]:
return _request_param_or_config_option_or_default(request, 'beta_target', None)


@pytest.fixture
@multi_dut_argument
def flash_port(request: FixtureRequest) -> t.Optional[str]:
"""Enable parametrization for the same cli option"""
return _request_param_or_config_option_or_default(request, 'flash_port', None)


@pytest.fixture
@multi_dut_argument
def skip_autoflash(request: FixtureRequest) -> t.Optional[bool]:
Expand Down Expand Up @@ -1008,6 +1020,7 @@ def parametrize_fixtures(
target,
beta_target,
baud,
flash_port,
skip_autoflash,
erase_all,
esptool_baud,
Expand Down

0 comments on commit c93c77a

Please sign in to comment.