Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test for wrap cmd
  • Loading branch information
fpliger committed Jan 11, 2023
commit 594915ce29c738024cb7a32721f1ff76b3e5cd54
23 changes: 20 additions & 3 deletions src/pyscript/plugins/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Optional

from pyscript import app, cli, console, plugins
from pyscript import LATEST_PYSCRIPT_VERSION, app, cli, console, plugins
from pyscript._generator import file_to_html, string_to_html

try:
Expand All @@ -29,6 +29,11 @@ def wrap(
),
show: Optional[bool] = typer.Option(None, help="Open output file in web browser."),
title: Optional[str] = typer.Option(None, help="Add title to HTML file."),
pyscript_version: Optional[str] = typer.Option(
LATEST_PYSCRIPT_VERSION,
"--pyscript-version",
help="If provided, defines what version of pyscript will be used to create the app",
),
) -> None:
"""Wrap a Python script inside an HTML file."""
title = title or "PyScript App"
Expand All @@ -52,9 +57,21 @@ def wrap(
else:
raise cli.Abort("Must provide an output file or use `--show` option")
if input_file is not None:
file_to_html(input_file, title, output, template_name="wrap.html")
file_to_html(
input_file,
title,
output,
template_name="wrap.html",
pyscript_version=pyscript_version,
)
if command:
string_to_html(command, title, output, template_name="wrap.html")
string_to_html(
command,
title,
output,
template_name="wrap.html",
pyscript_version=pyscript_version,
)
if output:
if show:
console.print("Opening in web browser!")
Expand Down
4 changes: 2 additions & 2 deletions src/pyscript/templates/wrap.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html lang="en">
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css"/>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/{{ pyscript_version }}/pyscript.css"/>
<script defer src="https://pyscript.net/releases/{{ pyscript_version }}/pyscript.js"></script>
</head>
<body>
<py-script>
Expand Down
42 changes: 42 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,43 @@ def test_wrap_title(
assert f"<title>{expected_title}</title>" in html_text


@pytest.mark.parametrize(
"version, expected_version",
[(None, LATEST_PYSCRIPT_VERSION), ("2022.9.1", "2022.9.1")],
)
def test_wrap_pyscript_version(
invoke_cli: CLIInvoker,
version: Optional[str],
expected_version: str,
tmp_path: Path,
) -> None:
command = 'print("Hello World!")'
args = ["wrap", "-c", command, "-o", "output.html"]
if version is not None:
args.extend(["--pyscript-version", version])
result = invoke_cli(*args)
assert result.exit_code == 0

expected_html_path = tmp_path / "output.html"
assert expected_html_path.exists()

with expected_html_path.open() as fp:
html_text = fp.read()

assert f"<py-script>\n{command}\n</py-script>" in html_text

version_str = (
f'<script defer src="https://pyscript.net/releases/{expected_version}'
'/pyscript.js"></script>'
)
css_version_str = (
'<link rel="stylesheet" href="https://pyscript.net/releases/'
f'{expected_version}/pyscript.css"/>'
)
assert version_str in html_text
assert css_version_str in html_text


@pytest.mark.parametrize(
"create_args, expected_version",
[
Expand Down Expand Up @@ -209,4 +246,9 @@ def test_create_project_version(
f'<script defer src="https://pyscript.net/releases/{expected_version}'
'/pyscript.js"></script>'
)
css_version_str = (
'<link rel="stylesheet" href="https://pyscript.net/releases/'
f'{expected_version}/pyscript.css"/>'
)
assert version_str in html_text
assert css_version_str in html_text