From 50944176d2a42e866bfcaeb07ae3608bd88a7cea Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Mon, 25 Sep 2023 23:15:46 +0300 Subject: [PATCH 01/10] feat(wokwi): initial version --- pytest-embedded-wokwi/LICENSE | 21 ++++ pytest-embedded-wokwi/README.md | 1 + pytest-embedded-wokwi/pyproject.toml | 105 ++++++++++++++++++ .../pytest_embedded_wokwi/__init__.py | 11 ++ .../pytest_embedded_wokwi/dut.py | 25 +++++ .../pytest_embedded_wokwi/wokwi_cli.py | 100 +++++++++++++++++ pytest-embedded/pytest_embedded/plugin.py | 65 ++++++++++- pytest-embedded/pytest_embedded/utils.py | 4 +- 8 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 pytest-embedded-wokwi/LICENSE create mode 100644 pytest-embedded-wokwi/README.md create mode 100644 pytest-embedded-wokwi/pyproject.toml create mode 100644 pytest-embedded-wokwi/pytest_embedded_wokwi/__init__.py create mode 100644 pytest-embedded-wokwi/pytest_embedded_wokwi/dut.py create mode 100644 pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py diff --git a/pytest-embedded-wokwi/LICENSE b/pytest-embedded-wokwi/LICENSE new file mode 100644 index 00000000..d73ab57c --- /dev/null +++ b/pytest-embedded-wokwi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Espressif Systems (Shanghai) Co. Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/pytest-embedded-wokwi/README.md b/pytest-embedded-wokwi/README.md new file mode 100644 index 00000000..8f3dbc88 --- /dev/null +++ b/pytest-embedded-wokwi/README.md @@ -0,0 +1 @@ +### pytest-embedded-wokwi diff --git a/pytest-embedded-wokwi/pyproject.toml b/pytest-embedded-wokwi/pyproject.toml new file mode 100644 index 00000000..73e99bfe --- /dev/null +++ b/pytest-embedded-wokwi/pyproject.toml @@ -0,0 +1,105 @@ +[build-system] +requires = ["flit_core >=3.2,<4"] +build-backend = "flit_core.buildapi" + +[project] +name = "pytest-embedded-wokwi" +authors = [ + {name = "Fu Hanxi", email = "fuhanxi@espressif.com"}, +] +readme = "README.md" +license = {file = "LICENSE"} +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Framework :: Pytest", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python", + "Topic :: Software Development :: Testing", +] +dynamic = ["version", "description"] +requires-python = ">=3.7" + +dependencies = [ + "pytest-embedded~=1.3.5", +] + +[project.optional-dependencies] +idf = [ + "pytest-embedded-idf~=1.3.5", +] + +[project.urls] +homepage = "https://github.com/espressif/pytest-embedded" +repository = "https://github.com/espressif/pytest-embedded" +documentation = "https://docs.espressif.com/projects/pytest-embedded/en/latest/" +changelog = "https://github.com/espressif/pytest-embedded/blob/main/CHANGELOG.md" + +[tool.isort] +profile = 'black' + +[tool.black] +line-length = 120 +target-version = ['py37'] +force-exclude = '/tests/' +skip-string-normalization = true + +[tool.ruff] +select = [ + 'F', # Pyflakes + 'E', # pycodestyle + 'W', # pycodestyle +# 'C90', # mccabe +# 'I', # isort +# 'N', # pep8-naming +# 'D', # pydocstyle +# 'UP', # pyupgrade +# 'YTT', # flake8-2020 +# 'ANN', # flake8-annotations +# 'S', # flake8-bandit +# 'BLE', # flake8-blind-except +# 'FBT', # flake8-boolean-trap +# 'B', # flake8-bugbear +# 'A', # flake8-builtins +# 'COM', # flake8-commas +# 'C4', # flake8-comprehensions +# 'DTZ', # flake8-datetimez +# 'T10', # flake8-debugger +# 'DJ', # flake8-django +# 'EM', # flake8-errmsg +# 'EXE', # flake8-executable +# 'ISC', # flake8-implicit-str-concat +# 'ICN', # flake8-import-conventions +# 'G', # flake8-logging-format +# 'INP', # flake8-no-pep420 +# 'PIE', # flake8-pie +# 'T20', # flake8-print +# 'PYI', # flake8-pyi +# 'PT', # flake8-pytest-style +# 'Q', # flake8-quotes +# 'RSE', # flake8-raise +# 'RET', # flake8-return +# 'SLF', # flake8-self +# 'SIM', # flake8-simplify +# 'TID', # flake8-tidy-imports +# 'TCH', # flake8-type-checking +# 'ARG', # flake8-unused-arguments +# 'PTH', # flake8-use-pathlib +# 'ERA', # eradicate +# 'PD', # pandas-vet +# 'PGH', # pygrep-hooks +# 'PL', # Pylint +# 'TRY', # tryceratops +# 'NPY', # NumPy-specific rules +# 'RUF', # Ruff-specific rules +] +line-length = 120 +target-version = "py37" diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/__init__.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/__init__.py new file mode 100644 index 00000000..48e67962 --- /dev/null +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/__init__.py @@ -0,0 +1,11 @@ +"""Make pytest-embedded plugin work with the Wokwi CLI.""" + +from .dut import WokwiDut # noqa +from .wokwi_cli import WokwiCLI # noqa + +__all__ = [ + 'WokwiCLI', + 'WokwiDut', +] + +__version__ = '1.3.5' diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/dut.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/dut.py new file mode 100644 index 00000000..132eec7b --- /dev/null +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/dut.py @@ -0,0 +1,25 @@ +from typing import AnyStr + +from pytest_embedded.dut import Dut + +from .wokwi_cli import WokwiCLI + + +class WokwiDut(Dut): + """ + Wokwi DUT class + """ + + def __init__( + self, + wokwi: WokwiCLI, + **kwargs, + ) -> None: + self.wokwi = wokwi + + super().__init__(**kwargs) + + self._hard_reset_func = self.wokwi._hard_reset + + def write(self, s: AnyStr) -> None: + self.wokwi.write(s) diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py new file mode 100644 index 00000000..83192bff --- /dev/null +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py @@ -0,0 +1,100 @@ +import json +import os +import typing as t + +from pytest_embedded import __version__ +from pytest_embedded.log import DuplicateStdoutPopen + +if t.TYPE_CHECKING: + from pytest_embedded_idf.app import IdfApp + + +target_to_board = { + 'esp32': 'board-esp32-devkit-c-v4', + 'esp32c3': 'board-esp32-c3-devkitm-1', + 'esp32c6': 'board-esp32-c6-devkitc-1', + 'esp32h2': 'board-esp32-h2-devkitm-1', + 'esp32s2': 'board-esp32-s2-devkitm-1', + 'esp32s3': 'board-esp32-s3-devkitc-1', +} + + +class WokwiCLI(DuplicateStdoutPopen): + """ + WokwiCLI class + """ + + SOURCE = 'Wokwi' + + WOKWI_CLI_PATH = 'wokwi-cli' + + def __init__( + self, + wokwi_cli_path: t.Optional[str] = None, + app: t.Optional['IdfApp'] = None, + **kwargs, + ): + """ + Args: + wokwi_cli_path: Wokwi CLI arguments + """ + self.app = app + + bin_list = [] + app_binary = None + for file in app.flash_files: + if not os.path.exists(file.file_path): + raise ValueError(f'Firmware binary file doesn\'t exist: {file.file_path}') + file_rel_path = os.path.relpath(app.elf_file, app.app_path) + bin_list.append(f"'{file.offset:#x} {file_rel_path}',") + # The following is a workaround until wokwi-cli supports multiple binaries + if file.offset == 0x10000: + app_binary = file_rel_path + + bin_list_toml = '\n '.join(bin_list) + with open(os.path.join(app.app_path, 'wokwi.toml'), 'wt') as f: + f.write( + f""" +[wokwi] +version = 1 +generatedBy = 'pytest-embedded-wokwi {__version__}' +firmware = '{app_binary}' +elf = '{os.path.relpath(app.elf_file, app.app_path)}' + +# We don't support multiple binaries yet, so this will be ignored for now: +flash = [ + {bin_list_toml} +] +""" + ) + + # TODO: Check if diagram already exist and update it? + diagram = { + 'version': 1, + 'author': 'Uri Shaked', + 'editor': 'wokwi', + 'parts': [{'type': target_to_board[app.target], 'id': 'esp'}], + 'connections': [ + ['esp:TX', '$serialMonitor:RX', '', []], + ['esp:RX', '$serialMonitor:TX', '', []], + ], + } + with open(os.path.join(app.app_path, 'diagram.json'), 'wt') as f: + f.write(json.dumps(diagram)) + + wokwi_cli = wokwi_cli_path or self.wokwi_cli_executable + + super().__init__( + cmd=[wokwi_cli, app.app_path], + **kwargs, + ) + + @property + def wokwi_cli_executable(self): + return self.WOKWI_CLI_PATH + + def _hard_reset(self): + """ + This is a fake hard_reset. Keep this API to keep the consistency. + """ + raise NotImplementedError diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index 5fba0f5a..3f371d9c 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -52,6 +52,8 @@ from pytest_embedded_qemu import Qemu from pytest_embedded_serial import Serial + from pytest_embedded_wokwi import WokwiCLI + _T = t.TypeVar('_T') @@ -118,6 +120,7 @@ def pytest_addoption(parser): '- jtag: openocd and gdb\n' '- qemu: use qemu simulator instead of the real target\n' '- arduino: auto-detect more app info with arduino specific rules, auto flash-in\n' + '- wokwi: use wokwi simulator instead of the real target\n' 'All the related CLI options are under the groups named by "embedded-"', ) base_group.addoption('--app-path', help='App path') @@ -242,6 +245,12 @@ def pytest_addoption(parser): help='Flash Encryption (pre-encrypted workflow) key path. (Default: None)', ) + wokwi_group = parser.getgroup('embedded-wokwi') + wokwi_group.addoption( + '--wokwi-cli-path', + help='Path to the wokwi-cli program (Default: "wokwi-cli")', + ) + ########### # helpers # @@ -949,6 +958,16 @@ def keyfile(request: FixtureRequest) -> t.Optional[str]: return _request_param_or_config_option_or_default(request, 'keyfile', None) +######### +# Wokwi # +######### +@pytest.fixture +@multi_dut_argument +def wokwi_cli_path(request: FixtureRequest) -> t.Optional[str]: + """Enable parametrization for the same cli option""" + return _request_param_or_config_option_or_default(request, 'wokwi_cli_path', None) + + #################### # Private Fixtures # #################### @@ -1009,6 +1028,7 @@ def _fixture_classes_and_options( qemu_prog_path, qemu_cli_args, qemu_extra_args, + wokwi_cli_path, skip_regenerate_image, encrypt, keyfile, @@ -1175,6 +1195,17 @@ def _fixture_classes_and_options( 'app': None, 'meta': _meta, } + elif fixture == 'wokwi': + if 'wokwi' in _services: + from pytest_embedded_wokwi import WokwiCLI + + classes[fixture] = WokwiCLI + kwargs[fixture] = { + 'wokwi_cli_path': wokwi_cli_path, + 'msg_queue': msg_queue, + 'app': None, + 'meta': _meta, + } elif fixture == 'dut': classes[fixture] = Dut kwargs[fixture] = { @@ -1185,6 +1216,20 @@ def _fixture_classes_and_options( 'test_case_name': test_case_name, 'meta': _meta, } + if 'wokwi' in _services: + from pytest_embedded_wokwi import WokwiDut + + classes[fixture] = WokwiDut + kwargs[fixture].update( + { + 'wokwi': None, + } + ) + + if 'idf' in _services: + from pytest_embedded_idf.unity_tester import IdfUnityDutMixin + + mixins[fixture].append(IdfUnityDutMixin) if 'qemu' in _services: from pytest_embedded_qemu import QemuDut @@ -1317,6 +1362,22 @@ def qemu(_fixture_classes_and_options: ClassCliOptions, app) -> t.Optional['Qemu return cls(**_drop_none_kwargs(kwargs)) +@pytest.fixture +@multi_dut_generator_fixture +def wokwi(_fixture_classes_and_options: ClassCliOptions, app) -> t.Optional['WokwiCLI']: + """A wokwi subprocess that could read/redirect/write""" + if 'wokwi' not in _fixture_classes_and_options.classes: + return None + + cls = _fixture_classes_and_options.classes['wokwi'] + kwargs = _fixture_classes_and_options.kwargs['wokwi'] + + if 'app' in kwargs and kwargs['app'] is None: + kwargs['app'] = app + + return cls(**_drop_none_kwargs(kwargs)) + + @pytest.fixture @multi_dut_generator_fixture def dut( @@ -1326,6 +1387,7 @@ def dut( app: App, serial: t.Optional[t.Union['Serial', 'LinuxSerial']], qemu: t.Optional['Qemu'], + wokwi: t.Optional['WokwiCLI'], ) -> t.Union[Dut, t.List[Dut]]: """ A device under test (DUT) object that could gather output from various sources and redirect them to the pexpect @@ -1356,7 +1418,8 @@ def dut( kwargs[k] = gdb elif k == 'qemu': kwargs[k] = qemu - + elif k == 'wokwi': + kwargs[k] = wokwi return cls(**_drop_none_kwargs(kwargs), mixins=mixins) diff --git a/pytest-embedded/pytest_embedded/utils.py b/pytest-embedded/pytest_embedded/utils.py index 60c4f747..39a8c21c 100644 --- a/pytest-embedded/pytest_embedded/utils.py +++ b/pytest-embedded/pytest_embedded/utils.py @@ -21,6 +21,7 @@ 'jtag': f'{BASE_LIB_NAME}-jtag', 'qemu': f'{BASE_LIB_NAME}-qemu', 'arduino': f'{BASE_LIB_NAME}-arduino', + 'wokwi': f'{BASE_LIB_NAME}-wokwi', } FIXTURES_SERVICES = { @@ -29,7 +30,8 @@ 'openocd': ['jtag'], 'gdb': ['jtag'], 'qemu': ['qemu'], - 'dut': ['base', 'serial', 'jtag', 'qemu', 'idf'], + 'wokwi': ['wokwi'], + 'dut': ['base', 'serial', 'jtag', 'qemu', 'idf', 'wokwi'], } _T = t.TypeVar('_T') From b00e66616c423d7da7da8df1b6173ff70aae6d80 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Mon, 25 Sep 2023 23:29:25 +0300 Subject: [PATCH 02/10] fix(wokwi): make isort happy --- pytest-embedded/pytest_embedded/plugin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index 3f371d9c..1863fc55 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -51,7 +51,6 @@ from pytest_embedded_jtag import Gdb, OpenOcd from pytest_embedded_qemu import Qemu from pytest_embedded_serial import Serial - from pytest_embedded_wokwi import WokwiCLI From bead256c0be9b8b26d0b25a879f9f0bf6dfab6d7 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Wed, 27 Sep 2023 10:49:46 +0300 Subject: [PATCH 03/10] feat(wokwi): use flasher_args.json wokwi-cli 0.7.0 and later support specifying "flasher_args.json" as the firmware to simulate. This simplifies the pytest driver code, and allows us to support non standard configurations (e.g. custom bootloader or non standard partition layout). --- .../pytest_embedded_wokwi/wokwi_cli.py | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py index 83192bff..630fca65 100644 --- a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py @@ -1,6 +1,7 @@ import json import os import typing as t +from pathlib import Path from pytest_embedded import __version__ from pytest_embedded.log import DuplicateStdoutPopen @@ -39,32 +40,16 @@ def __init__( wokwi_cli_path: Wokwi CLI arguments """ self.app = app + flasher_args = Path(app.binary_path, 'flasher_args.json') - bin_list = [] - app_binary = None - for file in app.flash_files: - if not os.path.exists(file.file_path): - raise ValueError(f'Firmware binary file doesn\'t exist: {file.file_path}') - file_rel_path = os.path.relpath(app.elf_file, app.app_path) - bin_list.append(f"'{file.offset:#x} {file_rel_path}',") - # The following is a workaround until wokwi-cli supports multiple binaries - if file.offset == 0x10000: - app_binary = file_rel_path - - bin_list_toml = '\n '.join(bin_list) with open(os.path.join(app.app_path, 'wokwi.toml'), 'wt') as f: f.write( f""" [wokwi] version = 1 generatedBy = 'pytest-embedded-wokwi {__version__}' -firmware = '{app_binary}' -elf = '{os.path.relpath(app.elf_file, app.app_path)}' - -# We don't support multiple binaries yet, so this will be ignored for now: -flash = [ - {bin_list_toml} -] +firmware = '{Path(flasher_args).relative_to(app.app_path).as_posix()}' +elf = '{Path(app.elf_file).relative_to(app.app_path).as_posix()}' """ ) From 3674eb2331acbcf7cb63eceaf91cb523244c7980 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Thu, 28 Sep 2023 00:26:23 +0300 Subject: [PATCH 04/10] docs(wokwi): README with installation instructions --- pytest-embedded-wokwi/README.md | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pytest-embedded-wokwi/README.md b/pytest-embedded-wokwi/README.md index 8f3dbc88..9456e7da 100644 --- a/pytest-embedded-wokwi/README.md +++ b/pytest-embedded-wokwi/README.md @@ -1 +1,37 @@ ### pytest-embedded-wokwi + +pytest-embedded service for running tests on [Wokwi](https://wokwi.com/ci) instead of the real target. + +Wokwi supports most ESP32 targets, including: esp32, esp32s2, esp32s3, esp32c3, esp32c6, and esp32h2. In addition, it supports a [wide range of peripherals](https://docs.wokwi.com/getting-started/supported-hardware), including sensors, displays, motors, and debugging tools. + +Running the tests with Wokwi requires an internet connection. Your firmware is uploaded to the Wokwi server for the duration of the simulation, but it is not saved on the server. On-premises Wokwi installations are available for enterprise customers. + +## Wokwi CLI installation + +The Wokwi plugin uses the [Wokwi CLI](https://github.com/wokwi/wokwi-cli) to interact with the wokwi simulation server. You can download the precompiled CLI binaries from the [releases page](https://github.com/wokwi/wokwi-cli/releases). Alternatively, on Linux or Mac OS, you can install the CLI using the following command: + +```bash +curl -L https://wokwi.com/ci/install.sh | sh +``` + +And on Windows: + +```powershell +iwr https://wokwi.com/ci/install.ps1 -useb | iex +``` + +## API Tokens + +Before using this plugin, you need to create a free Wokwi account and [generate an API key](https://wokwi.com/dashboard/ci). You can then set the `WOKWI_CLI_TOKEN` environment variable to the API key. + +Linux / Mac OS / WSL: + +```bash +export WOKWI_CLI_TOKEN="your-api-key" +``` + +Windows PowerShell: + +```powershell +$env:WOKWI_CLI_TOKEN="your-api-key" +``` From b368f0fa38d82cd20c5f70bdc5cd1204499d2b71 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Mon, 2 Oct 2023 22:49:21 +0300 Subject: [PATCH 05/10] fix(wokwi): don't overwrite wokwi.toml and diagram.json --- pytest-embedded-wokwi/pyproject.toml | 1 + .../pytest_embedded_wokwi/wokwi_cli.py | 90 +++++++++++++------ 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/pytest-embedded-wokwi/pyproject.toml b/pytest-embedded-wokwi/pyproject.toml index 73e99bfe..359a7a8a 100644 --- a/pytest-embedded-wokwi/pyproject.toml +++ b/pytest-embedded-wokwi/pyproject.toml @@ -30,6 +30,7 @@ requires-python = ">=3.7" dependencies = [ "pytest-embedded~=1.3.5", + "toml~=0.10.2", ] [project.optional-dependencies] diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py index 630fca65..434c92e3 100644 --- a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py @@ -1,8 +1,10 @@ import json +import logging import os import typing as t from pathlib import Path +import toml from pytest_embedded import __version__ from pytest_embedded.log import DuplicateStdoutPopen @@ -40,32 +42,9 @@ def __init__( wokwi_cli_path: Wokwi CLI arguments """ self.app = app - flasher_args = Path(app.binary_path, 'flasher_args.json') - with open(os.path.join(app.app_path, 'wokwi.toml'), 'wt') as f: - f.write( - f""" -[wokwi] -version = 1 -generatedBy = 'pytest-embedded-wokwi {__version__}' -firmware = '{Path(flasher_args).relative_to(app.app_path).as_posix()}' -elf = '{Path(app.elf_file).relative_to(app.app_path).as_posix()}' -""" - ) - - # TODO: Check if diagram already exist and update it? - diagram = { - 'version': 1, - 'author': 'Uri Shaked', - 'editor': 'wokwi', - 'parts': [{'type': target_to_board[app.target], 'id': 'esp'}], - 'connections': [ - ['esp:TX', '$serialMonitor:RX', '', []], - ['esp:RX', '$serialMonitor:TX', '', []], - ], - } - with open(os.path.join(app.app_path, 'diagram.json'), 'wt') as f: - f.write(json.dumps(diagram)) + self.create_wokwi_toml() + self.create_diagram_json() wokwi_cli = wokwi_cli_path or self.wokwi_cli_executable @@ -78,6 +57,67 @@ def __init__( def wokwi_cli_executable(self): return self.WOKWI_CLI_PATH + def create_wokwi_toml(self): + app = self.app + flasher_args = Path(app.binary_path, 'flasher_args.json') + wokwi_toml_path = os.path.join(app.app_path, 'wokwi.toml') + firmware_path = Path(flasher_args).relative_to(app.app_path).as_posix() + elf_path = Path(app.elf_file).relative_to(app.app_path).as_posix() + + if os.path.exists(wokwi_toml_path): + with open(wokwi_toml_path, 'rt') as f: + toml_data = toml.load(f) + + if 'wokwi' not in toml_data: + toml_data['wokwi'] = {'version': 1} + + wokwi_table = toml_data['wokwi'] + if wokwi_table.get('firmware') == firmware_path and wokwi_table.get('elf') == elf_path: + # No need to update + return + + wokwi_table.update({'firmware': firmware_path, 'elf': elf_path}) + else: + toml_data = { + 'wokwi': { + 'version': 1, + 'generatedBy': f'pytest-embedded-wokwi {__version__}', + 'firmware': firmware_path, + 'elf': elf_path, + } + } + + with open(wokwi_toml_path, 'wt') as f: + toml.dump(toml_data, f) + + def create_diagram_json(self): + app = self.app + diagram_json_path = os.path.join(app.app_path, 'diagram.json') + target_board = target_to_board[app.target] + + if os.path.exists(diagram_json_path): + with open(diagram_json_path, 'rt') as f: + json_data = json.load(f) + if not any(part['type'] == target_board for part in json_data['parts']): + logging.warning( + f'diagram.json exists, no part with type "{target_board}" found. ' + + 'You may need to update the diagram.json file manually to match the target board.' + ) + return + + diagram = { + 'version': 1, + 'author': 'Uri Shaked', + 'editor': 'wokwi', + 'parts': [{'type': target_board, 'id': 'esp'}], + 'connections': [ + ['esp:TX', '$serialMonitor:RX', ''], + ['esp:RX', '$serialMonitor:TX', ''], + ], + } + with open(diagram_json_path, 'wt') as f: + f.write(json.dumps(diagram, indent=2)) + def _hard_reset(self): """ This is a fake hard_reset. Keep this API to keep the consistency. From cf2069aa332ec48891e200760ba0c69022f00c70 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Fri, 20 Oct 2023 12:25:52 +0300 Subject: [PATCH 06/10] fix(wokwi): ensure idf service is present --- .../pytest_embedded_wokwi/idf.py | 14 ++++++++++++ .../pytest_embedded_wokwi/wokwi_cli.py | 6 ++++- pytest-embedded/pytest_embedded/plugin.py | 22 +++++++++++++------ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 pytest-embedded-wokwi/pytest_embedded_wokwi/idf.py diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/idf.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/idf.py new file mode 100644 index 00000000..c14be821 --- /dev/null +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/idf.py @@ -0,0 +1,14 @@ +import typing as t +from pathlib import Path + +if t.TYPE_CHECKING: + from pytest_embedded_idf.app import IdfApp + + +class IDFFirmwareResolver: + """ + IDFFirmwareResolver class + """ + + def resolve_firmware(self, app: 'IdfApp'): + return Path(app.binary_path, 'flasher_args.json') diff --git a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py index 434c92e3..df11291a 100644 --- a/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py +++ b/pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py @@ -8,6 +8,8 @@ from pytest_embedded import __version__ from pytest_embedded.log import DuplicateStdoutPopen +from .idf import IDFFirmwareResolver + if t.TYPE_CHECKING: from pytest_embedded_idf.app import IdfApp @@ -33,6 +35,7 @@ class WokwiCLI(DuplicateStdoutPopen): def __init__( self, + firmware_resolver: IDFFirmwareResolver, wokwi_cli_path: t.Optional[str] = None, app: t.Optional['IdfApp'] = None, **kwargs, @@ -42,6 +45,7 @@ def __init__( wokwi_cli_path: Wokwi CLI arguments """ self.app = app + self.firmware_resolver = firmware_resolver self.create_wokwi_toml() self.create_diagram_json() @@ -59,7 +63,7 @@ def wokwi_cli_executable(self): def create_wokwi_toml(self): app = self.app - flasher_args = Path(app.binary_path, 'flasher_args.json') + flasher_args = self.firmware_resolver.resolve_firmware(app) wokwi_toml_path = os.path.join(app.app_path, 'wokwi.toml') firmware_path = Path(flasher_args).relative_to(app.app_path).as_posix() elf_path = Path(app.elf_file).relative_to(app.app_path).as_posix() diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index 1863fc55..e9dae243 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -51,6 +51,7 @@ from pytest_embedded_jtag import Gdb, OpenOcd from pytest_embedded_qemu import Qemu from pytest_embedded_serial import Serial + from pytest_embedded_wokwi import WokwiCLI @@ -1199,12 +1200,14 @@ def _fixture_classes_and_options( from pytest_embedded_wokwi import WokwiCLI classes[fixture] = WokwiCLI - kwargs[fixture] = { - 'wokwi_cli_path': wokwi_cli_path, - 'msg_queue': msg_queue, - 'app': None, - 'meta': _meta, - } + kwargs[fixture].update( + { + 'wokwi_cli_path': wokwi_cli_path, + 'msg_queue': msg_queue, + 'app': None, + 'meta': _meta, + } + ) elif fixture == 'dut': classes[fixture] = Dut kwargs[fixture] = { @@ -1228,7 +1231,13 @@ def _fixture_classes_and_options( if 'idf' in _services: from pytest_embedded_idf.unity_tester import IdfUnityDutMixin + from pytest_embedded_wokwi.idf import IDFFirmwareResolver + + kwargs['wokwi'].update({'firmware_resolver': IDFFirmwareResolver()}) + mixins[fixture].append(IdfUnityDutMixin) + else: + raise SystemExit('wokwi service should be used together with idf service') if 'qemu' in _services: from pytest_embedded_qemu import QemuDut @@ -1373,7 +1382,6 @@ def wokwi(_fixture_classes_and_options: ClassCliOptions, app) -> t.Optional['Wok if 'app' in kwargs and kwargs['app'] is None: kwargs['app'] = app - return cls(**_drop_none_kwargs(kwargs)) From 9d5be1ebaa6b1551c96898a52ca773c8107c981b Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Fri, 20 Oct 2023 12:31:27 +0300 Subject: [PATCH 07/10] fix(wokwi): make isort happy --- pytest-embedded/pytest_embedded/plugin.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index e9dae243..bd45a0a6 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -51,7 +51,6 @@ from pytest_embedded_jtag import Gdb, OpenOcd from pytest_embedded_qemu import Qemu from pytest_embedded_serial import Serial - from pytest_embedded_wokwi import WokwiCLI @@ -1230,7 +1229,6 @@ def _fixture_classes_and_options( if 'idf' in _services: from pytest_embedded_idf.unity_tester import IdfUnityDutMixin - from pytest_embedded_wokwi.idf import IDFFirmwareResolver kwargs['wokwi'].update({'firmware_resolver': IDFFirmwareResolver()}) From c19632f5048dd226b90a1093a0d5688b205d4825 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Tue, 24 Oct 2023 11:06:26 +0300 Subject: [PATCH 08/10] docs(wokwi): add Uri to authors --- pytest-embedded-wokwi/pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pytest-embedded-wokwi/pyproject.toml b/pytest-embedded-wokwi/pyproject.toml index 359a7a8a..4f684056 100644 --- a/pytest-embedded-wokwi/pyproject.toml +++ b/pytest-embedded-wokwi/pyproject.toml @@ -6,6 +6,7 @@ build-backend = "flit_core.buildapi" name = "pytest-embedded-wokwi" authors = [ {name = "Fu Hanxi", email = "fuhanxi@espressif.com"}, + {name = "Uri Shaked", email = "uri@wokwi.com"}, ] readme = "README.md" license = {file = "LICENSE"} From 64a1d26fbc35c0d5d2ed120cf3d76e97c7da12ba Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Tue, 24 Oct 2023 11:17:08 +0300 Subject: [PATCH 09/10] test(wokwi): add esp32 test case --- pytest-embedded-wokwi/tests/test_wokwi.py | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 pytest-embedded-wokwi/tests/test_wokwi.py diff --git a/pytest-embedded-wokwi/tests/test_wokwi.py b/pytest-embedded-wokwi/tests/test_wokwi.py new file mode 100644 index 00000000..9f7ec470 --- /dev/null +++ b/pytest-embedded-wokwi/tests/test_wokwi.py @@ -0,0 +1,38 @@ +import os +import shutil + +import pytest + +wokwi_cli_required = pytest.mark.skipif( + shutil.which('wokwi-cli') is None, + reason='Please make sure that `wokwi-cli` is in your PATH env var. ' + + 'To install: https://docs.wokwi.com/wokwi-ci/getting-started#cli-installation' +) + +wokwi_token_required = pytest.mark.skipif( + os.getenv('WOKWI_CLI_TOKEN') is None, + reason='Please make sure that `WOKWI_CLI_TOKEN` env var is set. Get a token here: https://wokwi.com/dashboard/ci' +) + + +@wokwi_cli_required +@wokwi_token_required +def test_pexpect_by_wokwi_esp32(testdir): + testdir.makepyfile(""" + import pexpect + import pytest + + def test_pexpect_by_wokwi(dut): + dut.expect('Hello world!') + dut.expect('Restarting') + with pytest.raises(pexpect.TIMEOUT): + dut.expect('foo bar not found', timeout=1) + """) + + result = testdir.runpytest( + '-s', + '--embedded-services', 'idf,wokwi', + '--app-path', os.path.join(testdir.tmpdir, 'hello_world_esp32'), + ) + + result.assert_outcomes(passed=1) From e90059f66c7c81fa748e4e266c058597914e64cf Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Tue, 24 Oct 2023 11:43:30 +0300 Subject: [PATCH 10/10] chore: add pytest-embedded-wokwi to foreach.sh --- foreach.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/foreach.sh b/foreach.sh index 2ddcad19..7f1a72d8 100755 --- a/foreach.sh +++ b/foreach.sh @@ -10,6 +10,7 @@ DEFAULT_PACKAGES=" \ pytest-embedded-jtag \ pytest-embedded-qemu \ pytest-embedded-arduino \ + pytest-embedded-wokwi \ " action=${1:-"install"}