-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest_cli.py
368 lines (290 loc) · 11.1 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Callable
import pytest
from mypy_extensions import VarArg
from typer.testing import CliRunner, Result
from pyscript import LATEST_PYSCRIPT_VERSION, __version__, config
from pyscript.cli import app
if TYPE_CHECKING:
from _pytest.monkeypatch import MonkeyPatch
CLIInvoker = Callable[[VarArg(str)], Result]
@pytest.fixture()
def app_details_args():
return [
"--app-description",
"tester-app",
"--author-name",
"tester",
"--author-email",
"tester@me.com",
]
@pytest.fixture()
def invoke_cli(tmp_path: Path, monkeypatch: MonkeyPatch) -> CLIInvoker:
"""Returns a function, which can be used to call the CLI from within a temporary directory."""
runner = CliRunner()
monkeypatch.chdir(tmp_path)
def f(*args: str) -> Result:
return runner.invoke(app, args)
return f
def test_version() -> None:
runner = CliRunner()
result = runner.invoke(app, "--version")
assert result.exit_code == 0
assert f"PyScript CLI version: {__version__}" in result.stdout
def test_create_command(
invoke_cli: CLIInvoker, tmp_path: Path, app_details_args: list[str], auto_enter
) -> None:
result = invoke_cli("create", "myapp")
assert result.exit_code == 0
expected_path = tmp_path / "myapp"
assert expected_path.exists()
expected_main_py_path = expected_path / "main.py"
assert expected_main_py_path.exists()
expected_config_path = expected_path / config["project_config_filename"]
assert expected_config_path.exists()
with expected_config_path.open() as fp:
config_text = fp.read()
assert 'name = "myapp' in config_text
# Assert that description, author name and email are empty
assert 'description = ""' in config_text
assert 'author_name = ""' in config_text
assert 'author_email = ""' in config_text
def test_create_command_bad_app_type(
invoke_cli: CLIInvoker, tmp_path: Path, app_details_args: list[str], auto_enter
) -> None:
result = invoke_cli("create", "myapp", "--project-type", "bad_type")
assert (
str(result.exception)
== "Unknown project type: bad_type. Valid values are: 'app'"
)
def test_create_command_no_app_name(
invoke_cli: CLIInvoker, tmp_path: Path, app_details_args: list[str], auto_enter
) -> None:
result = invoke_cli("create")
assert result.exit_code == 0
expected_path = tmp_path / "my-pyscript-app"
assert expected_path.exists()
expected_main_py_path = expected_path / "main.py"
assert expected_main_py_path.exists()
expected_config_path = expected_path / config["project_config_filename"]
assert expected_config_path.exists()
with expected_config_path.open() as fp:
config_text = fp.read()
assert 'name = "my-pyscript-app' in config_text
# Assert that description, author name and email are empty
assert 'description = ""' in config_text
assert 'author_name = ""' in config_text
assert 'author_email = ""' in config_text
def test_create_command_with_single_py_file(
invoke_cli: CLIInvoker, tmp_path: Path, app_details_args: list[str], auto_enter
):
"""
Test that when create is called with a single python file as input,
the project is created correctly
"""
input_file = tmp_path / "hello.py"
with input_file.open("w") as fp:
fp.write('print("Yo!")')
result = invoke_cli("create", "hello.py", *app_details_args)
assert result.exit_code == 0
expected_path = tmp_path / "hello"
assert expected_path.exists()
expected_main_py_path = expected_path / "main.py"
assert expected_main_py_path.exists()
with expected_main_py_path.open() as fp:
py_text = fp.read()
assert 'print("Yo!")' in py_text
expected_config_path = expected_path / config["project_config_filename"]
assert expected_config_path.exists()
with expected_config_path.open() as fp:
config_text = fp.read()
assert 'name = "hello' in config_text
# Assert that description, author name and email are empty
assert 'description = "tester-app"' in config_text
assert 'author_name = "tester"' in config_text
@pytest.mark.parametrize("flag", ["-c", "--command"])
def test_wrap_command(
invoke_cli: CLIInvoker, tmp_path: Path, flag: str, app_details_args: list[str]
) -> None:
command = 'print("Hello World!")'
result = invoke_cli(
"create", "--wrap", flag, command, "-o", "output.html", *app_details_args
)
assert result.exit_code == 0
expected_html_path = tmp_path / "output" / "output.html"
assert expected_html_path.exists()
expected_main_py_path = tmp_path / "output" / "main.py"
with expected_main_py_path.open() as fp:
py_text = fp.read()
assert command in py_text
@pytest.mark.parametrize(
"wrap_args",
[tuple(), ("-c", "print()", "script_name.py")],
ids=["empty_args", "command_and_script"],
)
def test_wrap_abort(invoke_cli: CLIInvoker, wrap_args: tuple[str]):
result = invoke_cli("create", "--wrap", *wrap_args)
assert result.exit_code == 1
@pytest.mark.parametrize(
"wrap_args, expected_output_filename",
[(("-o", "output.html"), "output.html"), (tuple(), "index.html")],
)
def test_wrap_file(
invoke_cli: CLIInvoker,
tmp_path: Path,
wrap_args: tuple[str],
expected_output_filename: str,
app_details_args: list[str],
) -> None:
command = 'print("Hello World!")'
input_file = tmp_path / "hello.py"
with input_file.open("w") as fp:
fp.write(command)
result = invoke_cli(
"create", str(input_file), "--wrap", *wrap_args, *app_details_args
)
assert result.exit_code == 0
expected_html_path = tmp_path / "hello" / expected_output_filename
assert expected_html_path.exists()
expected_main_py_path = tmp_path / "hello" / "main.py"
with expected_main_py_path.open() as fp:
py_text = fp.read()
assert command in py_text
@pytest.mark.parametrize(
"version, expected_version",
[(None, LATEST_PYSCRIPT_VERSION), ("2023.11.1", "2023.11.1")],
)
def test_wrap_pyscript_version(
invoke_cli: CLIInvoker,
version: str | None,
expected_version: str,
tmp_path: Path,
app_details_args: list[str],
) -> None:
"""
Test that when wrap is called passing a string code input and an explicit pyscript version
the project is created correctly
"""
command = 'print("Hello World!")'
args = ["create", "--wrap", "-c", command, "-o", "output.html", *app_details_args]
if version is not None:
args.extend(["--pyscript-version", version])
# GIVEN a call to wrap with a cmd input and specific pyscript version as arguments
result = invoke_cli(*args)
assert result.exit_code == 0
# EXPECT the output file to exist
expected_html_path = tmp_path / "output" / "output.html"
assert expected_html_path.exists()
with expected_html_path.open() as fp:
html_text = fp.read()
expected_main_py_path = tmp_path / "output" / "main.py"
with expected_main_py_path.open() as fp:
py_text = fp.read()
assert command in py_text
# EXPECT the right JS and CSS version to be present in the output file
version_str = (
'<script type="module" src="https://pyscript.net/releases/'
f'{expected_version}/core.js"></script>'
)
css_version_str = (
'<link rel="stylesheet" href="https://pyscript.net/releases/'
f'{expected_version}/core.css">'
)
assert version_str in html_text
assert css_version_str in html_text
@pytest.mark.parametrize(
"version, expected_version",
[(None, LATEST_PYSCRIPT_VERSION), ("2023.11.1", "2023.11.1")],
)
def test_wrap_pyscript_version_file(
invoke_cli: CLIInvoker,
version: str | None,
expected_version: str,
tmp_path: Path,
app_details_args: list[str],
) -> None:
"""
Test that when wrap is called passing a file input and an explicit pyscript version
the project is created correctly
"""
command = 'print("Hello World!")'
input_file = tmp_path / "hello.py"
with input_file.open("w") as fp:
fp.write(command)
args = ["create", "--wrap", str(input_file), "-o", "output.html", *app_details_args]
if version is not None:
args.extend(["--pyscript-version", version])
# GIVEN a call to wrap with a file and specific pyscript version as arguments
result = invoke_cli(*args)
assert result.exit_code == 0
# EXPECT the output file to exist
expected_html_path = tmp_path / "hello" / "output.html"
assert expected_html_path.exists()
with expected_html_path.open() as fp:
html_text = fp.read()
expected_main_py_path = tmp_path / "hello" / "main.py"
with expected_main_py_path.open() as fp:
py_text = fp.read()
assert command in py_text
# EXPECT the right JS and CSS version to be present in the output file
version_str = (
'<script type="module" src="https://pyscript.net/releases/'
f'{expected_version}/core.js"></script>'
)
css_version_str = (
'<link rel="stylesheet" href="https://pyscript.net/releases/'
f'{expected_version}/core.css">'
)
assert version_str in html_text
assert css_version_str in html_text
@pytest.mark.parametrize(
"create_args, expected_version",
[
(("myapp1",), LATEST_PYSCRIPT_VERSION),
(("myapp-w-version", "--pyscript-version", "2023.11.1"), "2023.11.1"),
],
)
def test_create_project_version(
invoke_cli: CLIInvoker,
tmp_path: Path,
create_args: tuple[str],
expected_version: str,
app_details_args: list[str],
) -> None:
"""
Test that project created with an explicit pyscript version are created correctly
"""
command = 'print("Hello World!")'
input_file = tmp_path / "hello.py"
with input_file.open("w") as fp:
fp.write(command)
cmd_args = list(create_args) + app_details_args
# GIVEN a call to wrap with a file and specific pyscript version as arguments
result = invoke_cli("create", *cmd_args)
assert result.exit_code == 0
# EXPECT the app folder to exist
expected_app_path = tmp_path / create_args[0]
assert expected_app_path.exists()
# EXPECT the app folder to contain the right index.html file
app_file = expected_app_path / "index.html"
assert app_file.exists()
with app_file.open() as fp:
html_text = fp.read()
# EXPECT the right JS and CSS version to be present in the html file
version_str = (
'<script type="module" src="https://pyscript.net/releases/'
f'{expected_version}/core.js"></script>'
)
css_version_str = (
'<link rel="stylesheet" href="https://pyscript.net/releases/'
f'{expected_version}/core.css">'
)
assert version_str in html_text
assert css_version_str in html_text
# EXPECT the folder to also contain the python main file
py_file = expected_app_path / config["project_main_filename"]
assert py_file.exists()
# EXPECT the folder to also contain the config file
config_file = expected_app_path / config["project_config_filename"]
assert config_file.exists()