Skip to content

Commit 7e6eb46

Browse files
committed
feat(wokwi): Add support for specifying diagram path
This PR adds a support of specifying diagram file for pytest-embedded-wokwi. To specify diagram.json add --wokwi-diagram to the pytest command
1 parent 30082f7 commit 7e6eb46

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

pytest-embedded-wokwi/pytest_embedded_wokwi/wokwi_cli.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import logging
33
import os
4-
import shutil
54
import typing as t
65
from pathlib import Path
76

@@ -41,6 +40,7 @@ def __init__(
4140
wokwi_cli_path: t.Optional[str] = None,
4241
wokwi_timeout: t.Optional[int] = None,
4342
wokwi_scenario: t.Optional[str] = None,
43+
wokwi_diagram: t.Optional[str] = None,
4444
app: t.Optional['IdfApp'] = None,
4545
**kwargs,
4646
):
@@ -52,14 +52,18 @@ def __init__(
5252
self.firmware_resolver = firmware_resolver
5353

5454
self.create_wokwi_toml()
55-
self.create_diagram_json()
55+
56+
if wokwi_diagram is None:
57+
self.create_diagram_json()
5658

5759
wokwi_cli = wokwi_cli_path or self.wokwi_cli_executable
5860
cmd = [wokwi_cli, '--interactive', app.app_path]
5961
if (wokwi_timeout is not None) and (wokwi_timeout > 0):
6062
cmd.extend(['--timeout', str(wokwi_timeout)])
6163
if (wokwi_scenario is not None) and os.path.exists(wokwi_scenario):
6264
cmd.extend(['--scenario', wokwi_scenario])
65+
if (wokwi_diagram is not None) and os.path.exists(wokwi_diagram):
66+
cmd.extend(['--diagram-file', wokwi_diagram])
6367

6468
super().__init__(
6569
cmd=cmd,
@@ -107,22 +111,6 @@ def create_diagram_json(self):
107111
app = self.app
108112
target_board = target_to_board[app.target]
109113

110-
# Check for specific target.diagram.json file first
111-
diagram_json_path = os.path.join(app.app_path, (app.target + '.diagram.json'))
112-
if os.path.exists(diagram_json_path):
113-
# If there is also common diagram.json file, backup it first to diagram.json.old
114-
if os.path.exists(os.path.join(app.app_path, 'diagram.json')):
115-
logging.warning(
116-
'using %s instead. backup the original diagram.json to diagram.json.old', diagram_json_path
117-
)
118-
shutil.copyfile(
119-
os.path.join(app.app_path, 'diagram.json'),
120-
os.path.join(app.app_path, 'diagram.json.old'),
121-
)
122-
# Copy target.diagram.json to diagram.json
123-
shutil.copyfile(diagram_json_path, os.path.join(app.app_path, 'diagram.json'))
124-
return
125-
126114
# Check for common diagram.json file
127115
diagram_json_path = os.path.join(app.app_path, 'diagram.json')
128116
if os.path.exists(diagram_json_path):

pytest-embedded-wokwi/tests/test_wokwi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_pexpect_by_wokwi(dut):
5454
'-s',
5555
'--embedded-services', 'arduino,wokwi',
5656
'--app-path', os.path.join(testdir.tmpdir, 'hello_world_arduino'),
57+
'--wokwi-diagram', os.path.join(testdir.tmpdir, 'hello_world_arduino/esp32.diagram.json'),
5758
)
5859

5960
result.assert_outcomes(passed=1)

pytest-embedded/pytest_embedded/dut_factory.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def _fixture_classes_and_options_fn(
149149
wokwi_cli_path,
150150
wokwi_timeout,
151151
wokwi_scenario,
152+
wokwi_diagram,
152153
skip_regenerate_image,
153154
encrypt,
154155
keyfile,
@@ -302,6 +303,7 @@ def _fixture_classes_and_options_fn(
302303
'wokwi_cli_path': wokwi_cli_path,
303304
'wokwi_timeout': wokwi_timeout,
304305
'wokwi_scenario': wokwi_scenario,
306+
'wokwi_diagram': wokwi_diagram,
305307
'msg_queue': msg_queue,
306308
'app': None,
307309
'meta': _meta,
@@ -608,6 +610,7 @@ def create(
608610
wokwi_cli_path: t.Optional[str] = None,
609611
wokwi_timeout: t.Optional[int] = 0,
610612
wokwi_scenario: t.Optional[str] = None,
613+
wokwi_diagram: t.Optional[str] = None,
611614
skip_regenerate_image: t.Optional[bool] = None,
612615
encrypt: t.Optional[bool] = None,
613616
keyfile: t.Optional[str] = None,
@@ -655,6 +658,7 @@ def create(
655658
wokwi_cli_path: Wokwi CLI path.
656659
wokwi_timeout: Wokwi timeout.
657660
wokwi_scenario: Wokwi scenario path.
661+
wokwi_diagram: Wokwi diagram path.
658662
skip_regenerate_image: Skip image regeneration flag.
659663
encrypt: Encryption flag.
660664
keyfile: Keyfile for encryption.
@@ -718,6 +722,7 @@ def create(
718722
'wokwi_cli_path': wokwi_cli_path,
719723
'wokwi_timeout': wokwi_timeout,
720724
'wokwi_scenario': wokwi_scenario,
725+
'wokwi_diagram': wokwi_diagram,
721726
'skip_regenerate_image': skip_regenerate_image,
722727
'encrypt': encrypt,
723728
'keyfile': keyfile,

pytest-embedded/pytest_embedded/plugin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ def pytest_addoption(parser):
283283
'--wokwi-scenario',
284284
help='Path to the wokwi scenario file (Default: None)',
285285
)
286+
wokwi_group.addoption(
287+
'--wokwi-diagram',
288+
help='Path to the wokwi diagram file (Default: None)',
289+
)
286290

287291

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

975979

980+
@pytest.fixture
981+
@multi_dut_argument
982+
def wokwi_diagram(request: FixtureRequest) -> t.Optional[str]:
983+
"""Enable parametrization for the same cli option"""
984+
return _request_param_or_config_option_or_default(request, 'wokwi_diagram', None)
985+
986+
976987
####################
977988
# Private Fixtures #
978989
####################
@@ -1031,6 +1042,7 @@ def parametrize_fixtures(
10311042
wokwi_cli_path,
10321043
wokwi_timeout,
10331044
wokwi_scenario,
1045+
wokwi_diagram,
10341046
skip_regenerate_image,
10351047
encrypt,
10361048
keyfile,

0 commit comments

Comments
 (0)