Skip to content

Commit 867676c

Browse files
fabiobaltierikartben
authored andcommitted
twister: pass flash_command to the harness
Add the necessary logic to pass the custom flash_command to the pytest harness. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent a31f784 commit 867676c

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

scripts/pylib/pytest-twister-harness/src/twister_harness/device/hardware_adapter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,23 @@ def __init__(self, device_config: DeviceConfig) -> None:
3838
self.device_log_path: Path = device_config.build_dir / 'device.log'
3939
self._log_files.append(self.device_log_path)
4040

41+
def _generate_flash_command(self) -> None:
42+
command = [self.device_config.flash_command[0]]
43+
command.extend(['--build-dir', str(self.device_config.build_dir)])
44+
45+
if self.device_config.id:
46+
command.extend(['--board-id', self.device_config.id])
47+
48+
command.extend(self.device_config.flash_command[1:])
49+
50+
self.command = command
51+
4152
def generate_command(self) -> None:
4253
"""Return command to flash."""
54+
if self.device_config.flash_command:
55+
self._generate_flash_command()
56+
return
57+
4358
command = [
4459
self.west,
4560
'flash',

scripts/pylib/pytest-twister-harness/src/twister_harness/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def pytest_addoption(parser: pytest.Parser):
102102
'E.g. --west-flash-extra-args="--board-id=foobar,--erase" '
103103
'will translate to "west flash -- --board-id=foobar --erase".'
104104
)
105+
twister_harness_group.addoption(
106+
'--flash-command',
107+
help='Use a custom flash command for flashing.'
108+
)
105109
twister_harness_group.addoption(
106110
'--pre-script',
107111
metavar='PATH',

scripts/pylib/pytest-twister-harness/src/twister_harness/twister_harness_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
import csv
78
import logging
89
from dataclasses import dataclass, field
910
from pathlib import Path
@@ -30,6 +31,7 @@ class DeviceConfig:
3031
serial_pty: str = ''
3132
flash_before: bool = False
3233
west_flash_extra_args: list[str] = field(default_factory=list, repr=False)
34+
flash_command: str = ''
3335
name: str = ''
3436
pre_script: Path | None = None
3537
post_script: Path | None = None
@@ -60,6 +62,9 @@ def create(cls, config: pytest.Config) -> TwisterHarnessConfig:
6062
west_flash_extra_args: list[str] = []
6163
if config.option.west_flash_extra_args:
6264
west_flash_extra_args = [w.strip() for w in config.option.west_flash_extra_args.split(',')]
65+
flash_command: list[str] = []
66+
if config.option.flash_command:
67+
flash_command = [w.strip() for w in next(csv.reader([config.option.flash_command]))]
6368
runner_params: list[str] = []
6469
if config.option.runner_params:
6570
runner_params = [w.strip() for w in config.option.runner_params]
@@ -78,6 +83,7 @@ def create(cls, config: pytest.Config) -> TwisterHarnessConfig:
7883
serial_pty=config.option.device_serial_pty,
7984
flash_before=bool(config.option.flash_before),
8085
west_flash_extra_args=west_flash_extra_args,
86+
flash_command=flash_command,
8187
pre_script=_cast_to_path(config.option.pre_script),
8288
post_script=_cast_to_path(config.option.post_script),
8389
post_flash_script=_cast_to_path(config.option.post_flash_script),

scripts/pylib/pytest-twister-harness/tests/device/hardware_adapter_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def fixture_adapter(tmp_path) -> HardwareAdapter:
2525
platform='platform',
2626
id='p_id',
2727
base_timeout=5.0,
28+
flash_command='',
2829
)
2930
return HardwareAdapter(device_config)
3031

@@ -181,6 +182,14 @@ def test_if_get_command_returns_proper_string_with_west_flash_extra_args(
181182
]
182183

183184

185+
def test_if_get_command_flash_command(device: HardwareAdapter) -> None:
186+
device.device_config.build_dir = Path('build')
187+
device.device_config.flash_command = ['flash_command', '--with-arg']
188+
device.generate_command()
189+
assert isinstance(device.command, list)
190+
assert device.command == ['flash_command', '--build-dir', 'build', '--board-id', 'p_id', '--with-arg']
191+
192+
184193
def test_if_hardware_adapter_raises_exception_empty_command(device: HardwareAdapter) -> None:
185194
device.command = []
186195
exception_msg = 'Flash command is empty, please verify if it was generated properly.'

scripts/pylib/twister/twisterlib/harness.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ def _generate_parameters_for_hardware(self, handler: Handler):
479479
if options.west_flash and options.west_flash != []:
480480
command.append(f'--west-flash-extra-args={options.west_flash}')
481481

482+
if options.flash_command:
483+
command.append(f'--flash-command={options.flash_command}')
484+
482485
if board_id := hardware.probe_id or hardware.id:
483486
command.append(f'--device-id={board_id}')
484487

scripts/tests/twister/test_harness.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ def test_pytest__generate_parameters_for_hardware(tmp_path, pty_value, hardware_
564564

565565
options = handler.options
566566
options.west_flash = "args"
567+
options.flash_command = "flash_command"
567568

568569
hardware.probe_id = "123"
569570
hardware.product = "product"
@@ -597,6 +598,7 @@ def test_pytest__generate_parameters_for_hardware(tmp_path, pty_value, hardware_
597598
assert "--runner-params=--runner-param1" in command
598599
assert "--runner-params=runner-param2" in command
599600
assert "--west-flash-extra-args=args" in command
601+
assert "--flash-command=flash_command" in command
600602
assert "--device-id=123" in command
601603
assert "--device-product=product" in command
602604
assert "--pre-script=pre_script" in command

0 commit comments

Comments
 (0)