Skip to content

Commit

Permalink
feat(wokwi): Support arduino
Browse files Browse the repository at this point in the history
  • Loading branch information
P-R-O-C-H-Y committed Apr 24, 2024
1 parent 58575f7 commit daec8cf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions pytest-embedded-arduino/pytest_embedded_arduino/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(
self.fqbn = self._get_fqbn(self.binary_path)
self.target = self.fqbn.split(':')[2]
self.flash_files = self._get_bin_files(self.binary_path, self.sketch, self.target)
self.elf_file = os.path.realpath(os.path.join(self.binary_path, self.sketch + '.ino.elf'))

def _get_fqbn(self, build_path) -> str:
options_file = os.path.realpath(os.path.join(build_path, 'build.options.json'))
Expand Down
15 changes: 15 additions & 0 deletions pytest-embedded-wokwi/pytest_embedded_wokwi/arduino.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import typing as t
from pathlib import Path

if t.TYPE_CHECKING:
from pytest_embedded_arduino.app import ArduinoApp


class ArduinoFirmwareResolver:
"""
ArduinoFirmwareResolver class
"""

def resolve_firmware(self, app: 'ArduinoApp'):
# get path of ino.bin file
return Path(app.binary_path, app.sketch + '.ino.bin')
10 changes: 9 additions & 1 deletion pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
import shutil
import typing as t
from pathlib import Path

Expand Down Expand Up @@ -101,9 +102,16 @@ def create_wokwi_toml(self):

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]

# Check for specific target.diagram.json file first
diagram_json_path = os.path.join(app.app_path, (app.target + '.diagram.json'))
if os.path.exists(diagram_json_path):
shutil.copyfile(diagram_json_path, os.path.join(app.app_path, 'diagram.json'))
return

# Check for common diagram.json file
diagram_json_path = os.path.join(app.app_path, 'diagram.json')
if os.path.exists(diagram_json_path):
with open(diagram_json_path) as f:
json_data = json.load(f)
Expand Down
20 changes: 20 additions & 0 deletions pytest-embedded-wokwi/tests/test_wokwi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ def test_pexpect_by_wokwi(dut):
)

result.assert_outcomes(passed=1)


def test_pexpect_by_wokwi_esp32_arduino(testdir):
testdir.makepyfile("""
import pexpect
import pytest
def test_pexpect_by_wokwi(dut):
dut.expect('Hello Arduino!')
with pytest.raises(pexpect.TIMEOUT):
dut.expect('foo bar not found', timeout=1)
""")

result = testdir.runpytest(
'-s',
'--embedded-services', 'arduino,wokwi',
'--app-path', os.path.join(testdir.tmpdir, 'hello_world_arduino'),
)

result.assert_outcomes(passed=1)
27 changes: 26 additions & 1 deletion pytest-embedded/pytest_embedded/dut_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,27 @@ def _fixture_classes_and_options_fn(
if 'wokwi' in _services:
from pytest_embedded_wokwi import WokwiCLI

if 'idf' in _services:
from pytest_embedded_wokwi.idf import IDFFirmwareResolver

resolver = IDFFirmwareResolver
app = IdfApp
elif 'arduino' in _services:
from pytest_embedded_wokwi.arduino import ArduinoFirmwareResolver

resolver = ArduinoFirmwareResolver
app = ArduinoApp

classes[fixture] = WokwiCLI
kwargs[fixture].update({
'wokwi_cli_path': wokwi_cli_path,
'wokwi_timeout': wokwi_timeout,
'msg_queue': msg_queue,
'app': app,
'meta': _meta,
'firmware_resolver': resolver,
})

classes[fixture] = WokwiCLI
kwargs[fixture].update({
'wokwi_cli_path': wokwi_cli_path,
Expand Down Expand Up @@ -326,8 +347,12 @@ def _fixture_classes_and_options_fn(
from pytest_embedded_wokwi.idf import IDFFirmwareResolver

kwargs['wokwi'].update({'firmware_resolver': IDFFirmwareResolver()})
elif 'arduino' in _services:
from pytest_embedded_wokwi.arduino import ArduinoFirmwareResolver

kwargs['wokwi'].update({'firmware_resolver': ArduinoFirmwareResolver()})
else:
raise SystemExit('wokwi service should be used together with idf service')
raise SystemExit('wokwi service should be used together with idf or arduino service')
elif 'qemu' in _services:
from pytest_embedded_qemu import QemuDut

Expand Down

0 comments on commit daec8cf

Please sign in to comment.