1# Copyright (c) 2023 Nordic Semiconductor ASA
2#
3# SPDX-License-Identifier: Apache-2.0
4
5from __future__ import annotations
6
7import os
8from pathlib import Path
9from typing import Generator
10
11import pytest
12
13from twister_harness.device.binary_adapter import NativeSimulatorAdapter
14from twister_harness.twister_harness_config import DeviceConfig
15
16
17@pytest.fixture
18def resources() -> Path:
19    """Return path to `resources` folder"""
20    return Path(__file__).parent.joinpath('resources')
21
22
23@pytest.fixture
24def zephyr_base() -> str:
25    zephyr_base_path = os.getenv('ZEPHYR_BASE')
26    if zephyr_base_path is None:
27        pytest.fail('Environmental variable ZEPHYR_BASE has to be set.')
28    else:
29        return zephyr_base_path
30
31
32@pytest.fixture
33def twister_harness(zephyr_base) -> str:
34    """Retrun path to pytest-twister-harness src directory"""
35    pytest_twister_harness_path = str(Path(zephyr_base) / 'scripts' / 'pylib' / 'pytest-twister-harness' / 'src')
36    return pytest_twister_harness_path
37
38
39@pytest.fixture
40def shell_simulator_path(resources: Path) -> str:
41    return str(resources / 'shell_simulator.py')
42
43
44@pytest.fixture
45def shell_simulator_adapter(
46    tmp_path: Path, shell_simulator_path: str
47) -> Generator[NativeSimulatorAdapter, None, None]:
48    build_dir = tmp_path / 'build_dir'
49    os.mkdir(build_dir)
50    device = NativeSimulatorAdapter(DeviceConfig(build_dir=build_dir, type='native', base_timeout=5.0))
51    try:
52        device.command = ['python3', shell_simulator_path]
53        device.launch()
54        yield device
55    finally:
56        device.write(b'quit\n')
57        device.close()
58