Skip to content

Commit f52f08f

Browse files
gchwierhenrikbrixandersen
authored andcommitted
twister: pytest: Sysbuild support in pytest-twister-harness
Read default domain from domains.yaml file and update paths to proper build directory. It fixes native and qemu pytest scenarios, when application is build with sysbuild. Signed-off-by: Grzegorz Chwierut <[email protected]>
1 parent 37e82fc commit f52f08f

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,18 @@ class NativeSimulatorAdapter(BinaryAdapterBase):
118118

119119
def generate_command(self) -> None:
120120
"""Set command to run."""
121-
self.command = [str(self.device_config.build_dir / 'zephyr' / 'zephyr.exe')]
121+
self.command = [str(self.device_config.app_build_dir / 'zephyr' / 'zephyr.exe')]
122122

123123

124124
class UnitSimulatorAdapter(BinaryAdapterBase):
125125
"""Simulator adapter to run unit tests"""
126126

127127
def generate_command(self) -> None:
128128
"""Set command to run."""
129-
self.command = [str(self.device_config.build_dir / 'testbinary')]
129+
self.command = [str(self.device_config.app_build_dir / 'testbinary')]
130130

131131

132132
class CustomSimulatorAdapter(BinaryAdapterBase):
133133
def generate_command(self) -> None:
134134
"""Set command to run."""
135-
self.command = [self.west, 'build', '-d', str(self.device_config.build_dir), '-t', 'run']
135+
self.command = [self.west, 'build', '-d', str(self.device_config.app_build_dir), '-t', 'run']

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, device_config: DeviceConfig) -> None:
2323

2424
def generate_command(self) -> None:
2525
"""Set command to run."""
26-
self.command = [self.west, 'build', '-d', str(self.device_config.build_dir), '-t', 'run']
26+
self.command = [self.west, 'build', '-d', str(self.device_config.app_build_dir), '-t', 'run']
2727
if 'stdin' in self.process_kwargs:
2828
self.process_kwargs.pop('stdin')
2929

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
from __future__ import annotations
5+
6+
import os
7+
import sys
8+
import logging
9+
10+
from pathlib import Path
11+
12+
ZEPHYR_BASE = os.environ['ZEPHYR_BASE']
13+
sys.path.insert(0, os.path.join(ZEPHYR_BASE, 'scripts', 'pylib', 'build_helpers'))
14+
15+
from domains import Domains
16+
17+
logger = logging.getLogger(__name__)
18+
logging.getLogger('pykwalify').setLevel(logging.ERROR)
19+
20+
21+
def get_default_domain_name(domains_file: Path | str) -> int:
22+
"""
23+
Get the default domain name from the domains.yaml file
24+
"""
25+
domains = Domains.from_file(domains_file)
26+
logger.debug("Loaded sysbuild domain data from %s" % domains_file)
27+
return domains.get_default_domain().name

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
from dataclasses import dataclass, field
99
from pathlib import Path
10+
from twister_harness.helpers.domains_helper import get_default_domain_name
1011

1112
import pytest
1213

@@ -32,6 +33,14 @@ class DeviceConfig:
3233
pre_script: Path | None = None
3334
post_script: Path | None = None
3435
post_flash_script: Path | None = None
36+
app_build_dir: Path | None = None
37+
38+
def __post_init__(self):
39+
domains = self.build_dir / 'domains.yaml'
40+
if domains.exists():
41+
self.app_build_dir = self.build_dir / get_default_domain_name(domains)
42+
else:
43+
self.app_build_dir = self.build_dir
3544

3645

3746
@dataclass

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def fixture_device_adapter(tmp_path) -> Generator[QemuAdapter, None, None]:
2727

2828
@patch('shutil.which', return_value='west')
2929
def test_if_generate_command_creates_proper_command(patched_which, device: QemuAdapter):
30-
device.device_config.build_dir = Path('build_dir')
30+
device.device_config.app_build_dir = Path('build_dir')
3131
device.generate_command()
3232
assert device.command == ['west', 'build', '-d', 'build_dir', '-t', 'run']
3333

0 commit comments

Comments
 (0)