5
5
import typing as t
6
6
from pathlib import Path
7
7
8
+ import pexpect
8
9
import toml
10
+ from packaging .version import Version
9
11
from pytest_embedded import __version__
10
12
from pytest_embedded .log import DuplicateStdoutPopen
11
13
14
+ from pytest_embedded_wokwi import WOKWI_CLI_MINIMUM_VERSION
15
+
12
16
from .idf import IDFFirmwareResolver
13
17
14
18
if t .TYPE_CHECKING :
@@ -32,7 +36,6 @@ class WokwiCLI(DuplicateStdoutPopen):
32
36
"""
33
37
34
38
SOURCE = 'Wokwi'
35
-
36
39
WOKWI_CLI_PATH = 'wokwi-cli'
37
40
38
41
def __init__ (
@@ -41,6 +44,7 @@ def __init__(
41
44
wokwi_cli_path : t .Optional [str ] = None ,
42
45
wokwi_timeout : t .Optional [int ] = None ,
43
46
wokwi_scenario : t .Optional [str ] = None ,
47
+ wokwi_diagram : t .Optional [str ] = None ,
44
48
app : t .Optional ['IdfApp' ] = None ,
45
49
** kwargs ,
46
50
):
@@ -51,15 +55,37 @@ def __init__(
51
55
self .app = app
52
56
self .firmware_resolver = firmware_resolver
53
57
58
+ # first need to check if wokwi-cli exists in PATH
59
+ if shutil .which ('wokwi-cli' ) is None :
60
+ raise RuntimeError ('Please install wokwi-cli, by running: curl -L https://wokwi.com/ci/install.sh | sh' )
61
+
62
+ child = pexpect .spawn ('wokwi-cli --help' )
63
+ try :
64
+ child .expect (r'Wokwi CLI v(\d+\.\d+\.\d+)' , timeout = 1 )
65
+ wokwi_cli_version = child .match .group (1 ).decode ('utf-8' )
66
+ except pexpect .TIMEOUT :
67
+ logging .warning ('Failed to get wokwi-cli version, assume version requirements satisfied' )
68
+ else :
69
+ if Version (wokwi_cli_version ) < Version (WOKWI_CLI_MINIMUM_VERSION ):
70
+ raise ValueError (
71
+ f'Wokwi CLI version { wokwi_cli_version } is not supported. '
72
+ f'Minimum version required: { WOKWI_CLI_MINIMUM_VERSION } . '
73
+ f'To update Wokwi CLI run: curl -L https://wokwi.com/ci/install.sh | sh'
74
+ )
75
+
54
76
self .create_wokwi_toml ()
55
- self .create_diagram_json ()
77
+
78
+ if wokwi_diagram is None :
79
+ self .create_diagram_json ()
56
80
57
81
wokwi_cli = wokwi_cli_path or self .wokwi_cli_executable
58
82
cmd = [wokwi_cli , '--interactive' , app .app_path ]
59
83
if (wokwi_timeout is not None ) and (wokwi_timeout > 0 ):
60
84
cmd .extend (['--timeout' , str (wokwi_timeout )])
61
85
if (wokwi_scenario is not None ) and os .path .exists (wokwi_scenario ):
62
86
cmd .extend (['--scenario' , wokwi_scenario ])
87
+ if (wokwi_diagram is not None ) and os .path .exists (wokwi_diagram ):
88
+ cmd .extend (['--diagram-file' , wokwi_diagram ])
63
89
64
90
super ().__init__ (
65
91
cmd = cmd ,
@@ -107,22 +133,6 @@ def create_diagram_json(self):
107
133
app = self .app
108
134
target_board = target_to_board [app .target ]
109
135
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
-
126
136
# Check for common diagram.json file
127
137
diagram_json_path = os .path .join (app .app_path , 'diagram.json' )
128
138
if os .path .exists (diagram_json_path ):
0 commit comments