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 25, 2024
1 parent a6b84b9 commit b1702d4
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 11 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
19 changes: 19 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,22 @@ 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)
7 changes: 6 additions & 1 deletion pytest-embedded/pytest_embedded/dut_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ def _fixture_classes_and_options_fn(
'msg_queue': msg_queue,
'app': None,
'meta': _meta,
'firmware_resolver': None,
})
elif fixture == 'dut':
classes[fixture] = Dut
Expand Down Expand Up @@ -332,8 +333,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
18 changes: 9 additions & 9 deletions tests/fixtures/hello_world_arduino/build/build.options.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"additionalFiles": "",
"builtInLibrariesFolders": "/private/var/folders/t6/v8mgvn9950q9j_wkb3kp1mv40000gn/T/AppTranslocation/5BC795C6-A892-4411-BEA2-AFD13C97CB9D/d/Arduino.app/Contents/Java/libraries",
"builtInToolsFolders": "",
"compiler.optimization_flags": "",
"customBuildProperties": "build.warn_data_percentage=75",
"builtInLibrariesFolders": "",
"builtInToolsFolders": "/Applications/Arduino.app/Contents/Java/tools-builder",
"compiler.optimization_flags": "-Os",
"customBuildProperties": "",
"fqbn": "espressif:esp32:esp32:PSRAM=enabled,PartitionScheme=huge_app",
"hardwareFolders": "/Users/ouss4/Library/Arduino15/packages,/Users/ouss4/Documents/Arduino/hardware",
"otherLibrariesFolders": "/Users/ouss4/Documents/Arduino/libraries",
"runtime.ide.version": "10607",
"sketchLocation": "/Users/ouss4/esp/pytest-embedded/examples/arduino/hello_world_arduino/hello_world_arduino.ino"
}
"hardwareFolders": "/Users/prochy/Documents/Arduino/hardware",
"otherLibrariesFolders": "/Users/prochy/Documents/Arduino/libraries",
"runtime.ide.version": "10810",
"sketchLocation": "/Users/prochy/Documents/Arduino/hardware/espressif/esp32/tests/hello_world/hello_world.ino"
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/fixtures/hello_world_arduino/esp32.diagram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": 1,
"author": "[email protected]",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-devkit-c-v4",
"id": "esp"
}
],
"connections": [
[
"esp:TX",
"$serialMonitor:RX",
""
],
[
"esp:RX",
"$serialMonitor:TX",
""
]
]
}

0 comments on commit b1702d4

Please sign in to comment.