Skip to content

Commit

Permalink
feat(wokwi): Add support for specifying diagram path
Browse files Browse the repository at this point in the history
This PR adds a support of specifying diagram file for pytest-embedded-wokwi.
To specify diagram.json add --wokwi-diagram to the pytest command
  • Loading branch information
P-R-O-C-H-Y committed May 29, 2024
1 parent 30082f7 commit 7e6eb46
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
24 changes: 6 additions & 18 deletions pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import os
import shutil
import typing as t
from pathlib import Path

Expand Down Expand Up @@ -41,6 +40,7 @@ def __init__(
wokwi_cli_path: t.Optional[str] = None,
wokwi_timeout: t.Optional[int] = None,
wokwi_scenario: t.Optional[str] = None,
wokwi_diagram: t.Optional[str] = None,
app: t.Optional['IdfApp'] = None,
**kwargs,
):
Expand All @@ -52,14 +52,18 @@ def __init__(
self.firmware_resolver = firmware_resolver

self.create_wokwi_toml()
self.create_diagram_json()

if wokwi_diagram is None:
self.create_diagram_json()

wokwi_cli = wokwi_cli_path or self.wokwi_cli_executable
cmd = [wokwi_cli, '--interactive', app.app_path]
if (wokwi_timeout is not None) and (wokwi_timeout > 0):
cmd.extend(['--timeout', str(wokwi_timeout)])
if (wokwi_scenario is not None) and os.path.exists(wokwi_scenario):
cmd.extend(['--scenario', wokwi_scenario])
if (wokwi_diagram is not None) and os.path.exists(wokwi_diagram):
cmd.extend(['--diagram-file', wokwi_diagram])

super().__init__(
cmd=cmd,
Expand Down Expand Up @@ -107,22 +111,6 @@ def create_diagram_json(self):
app = self.app
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):
# If there is also common diagram.json file, backup it first to diagram.json.old
if os.path.exists(os.path.join(app.app_path, 'diagram.json')):
logging.warning(
'using %s instead. backup the original diagram.json to diagram.json.old', diagram_json_path
)
shutil.copyfile(
os.path.join(app.app_path, 'diagram.json'),
os.path.join(app.app_path, 'diagram.json.old'),
)
# Copy target.diagram.json to diagram.json
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):
Expand Down
1 change: 1 addition & 0 deletions pytest-embedded-wokwi/tests/test_wokwi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_pexpect_by_wokwi(dut):
'-s',
'--embedded-services', 'arduino,wokwi',
'--app-path', os.path.join(testdir.tmpdir, 'hello_world_arduino'),
'--wokwi-diagram', os.path.join(testdir.tmpdir, 'hello_world_arduino/esp32.diagram.json'),
)

result.assert_outcomes(passed=1)
5 changes: 5 additions & 0 deletions pytest-embedded/pytest_embedded/dut_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def _fixture_classes_and_options_fn(
wokwi_cli_path,
wokwi_timeout,
wokwi_scenario,
wokwi_diagram,
skip_regenerate_image,
encrypt,
keyfile,
Expand Down Expand Up @@ -302,6 +303,7 @@ def _fixture_classes_and_options_fn(
'wokwi_cli_path': wokwi_cli_path,
'wokwi_timeout': wokwi_timeout,
'wokwi_scenario': wokwi_scenario,
'wokwi_diagram': wokwi_diagram,
'msg_queue': msg_queue,
'app': None,
'meta': _meta,
Expand Down Expand Up @@ -608,6 +610,7 @@ def create(
wokwi_cli_path: t.Optional[str] = None,
wokwi_timeout: t.Optional[int] = 0,
wokwi_scenario: t.Optional[str] = None,
wokwi_diagram: t.Optional[str] = None,
skip_regenerate_image: t.Optional[bool] = None,
encrypt: t.Optional[bool] = None,
keyfile: t.Optional[str] = None,
Expand Down Expand Up @@ -655,6 +658,7 @@ def create(
wokwi_cli_path: Wokwi CLI path.
wokwi_timeout: Wokwi timeout.
wokwi_scenario: Wokwi scenario path.
wokwi_diagram: Wokwi diagram path.
skip_regenerate_image: Skip image regeneration flag.
encrypt: Encryption flag.
keyfile: Keyfile for encryption.
Expand Down Expand Up @@ -718,6 +722,7 @@ def create(
'wokwi_cli_path': wokwi_cli_path,
'wokwi_timeout': wokwi_timeout,
'wokwi_scenario': wokwi_scenario,
'wokwi_diagram': wokwi_diagram,
'skip_regenerate_image': skip_regenerate_image,
'encrypt': encrypt,
'keyfile': keyfile,
Expand Down
12 changes: 12 additions & 0 deletions pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ def pytest_addoption(parser):
'--wokwi-scenario',
help='Path to the wokwi scenario file (Default: None)',
)
wokwi_group.addoption(
'--wokwi-diagram',
help='Path to the wokwi diagram file (Default: None)',
)


###########
Expand Down Expand Up @@ -973,6 +977,13 @@ def wokwi_scenario(request: FixtureRequest) -> t.Optional[str]:
return _request_param_or_config_option_or_default(request, 'wokwi_scenario', None)


@pytest.fixture
@multi_dut_argument
def wokwi_diagram(request: FixtureRequest) -> t.Optional[str]:
"""Enable parametrization for the same cli option"""
return _request_param_or_config_option_or_default(request, 'wokwi_diagram', None)


####################
# Private Fixtures #
####################
Expand Down Expand Up @@ -1031,6 +1042,7 @@ def parametrize_fixtures(
wokwi_cli_path,
wokwi_timeout,
wokwi_scenario,
wokwi_diagram,
skip_regenerate_image,
encrypt,
keyfile,
Expand Down

0 comments on commit 7e6eb46

Please sign in to comment.