Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for runtime config inside py-config and remove usage of indexURL #734

Merged
merged 10 commits into from
Aug 31, 2022
4 changes: 2 additions & 2 deletions pyscriptjs/tests/integration/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def wait_for_pyscript(self, *, timeout=None, check_errors=True):
check_errors=check_errors,
)

def pyscript_run(self, snippet, config=""):
def pyscript_run(self, snippet, *, extra_head=""):
"""
Main entry point for pyscript tests.

Expand All @@ -197,7 +197,7 @@ def pyscript_run(self, snippet, config=""):
<head>
<link rel="stylesheet" href="{self.http_server}/build/pyscript.css" />
<script defer src="{self.http_server}/build/pyscript.js"></script>
{config}
{extra_head}
</head>
<body>
{snippet}
Expand Down
43 changes: 0 additions & 43 deletions pyscriptjs/tests/integration/test_py_config.py

This file was deleted.

68 changes: 68 additions & 0 deletions pyscriptjs/tests/integration/test_py_runtime_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import tarfile
import tempfile

import pytest
import requests

from .support import PyScriptTest

URL = "https://github.com/pyodide/pyodide/releases/download/0.20.0/pyodide-build-0.20.0.tar.bz2"
madhur-tandon marked this conversation as resolved.
Show resolved Hide resolved
TAR_NAME = "pyodide-build-0.20.0.tar.bz2"


@pytest.fixture
def tar_location(request):
val = request.config.cache.get("pyodide-0.20-tar", None)
if val is None:
response = requests.get(URL, stream=True)
TMP_DIR = tempfile.mkdtemp()
TMP_TAR_LOCATION = os.path.join(TMP_DIR, TAR_NAME)
with open(TMP_TAR_LOCATION, "wb") as f:
f.write(response.raw.read())
val = TMP_TAR_LOCATION
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

veeeery nitpicking, but note that here you are loading 151MB in memory all at once, just to write them to disk. I'm sure there are better ways to save an http request to disk.
That said, it's not a blocker for this PR, but just wanted to say it as a general comment/suggestion for the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chunking perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chunking is surely a good option.
I would have expected requests to have a direct way to save to disk but apparently there isn't.
A quick google reveals this.

request.config.cache.set("pyodide-0.20-tar", val)
return val


def unzip(location, extract_to="."):
file = tarfile.open(name=location, mode="r:bz2")
file.extractall(path=extract_to)


class TestRuntimeConfig(PyScriptTest):
# The default pyodide version is 0.21.2 as of writing
# this test which is newer than the one we are loading below
# (after downloading locally) -- which is 0.20.0

# The test checks if loading a different runtime is possible
# and that too from a locally downloaded file without needing
# the use of explicit `indexURL` calculation.
def test_runtime_config(self, tar_location):
unzip(
location=tar_location,
extract_to=self.tmpdir,
)

self.pyscript_run(
snippet="""
<py-script>
import sys, js
pyodide_version = sys.modules["pyodide"].__version__
js.console.info("version", pyodide_version)
pyodide_version
</py-script>
""",
extra_head="""
<py-config>
runtimes:
- src: "/pyodide/pyodide.js"
name: pyodide-0.20.0
lang: python
</py-config>
""",
)

assert self.console.info.lines == ["version 0.20.0"]
version = self.page.locator("py-script").inner_text()
assert version == "0.20.0"