Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_language_version:
python: "3.10"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.4.0"
rev: "v6.0.0"
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -15,19 +15,19 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.9.7"
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.15.20"
hooks:
- id: ruff
args: [--fix]

- repo: https://github.com/psf/black
rev: "23.1.0"
- repo: https://github.com/psf/black-pre-commit-mirror
rev: "26.5.1"
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.15.0"
rev: "v2.1.0"
hooks:
- id: mypy
exclude: (setup.py|^tests|conftest.py)
Expand All @@ -41,12 +41,12 @@ repos:
- pytest

- repo: https://github.com/shellcheck-py/shellcheck-py
rev: "v0.9.0.2"
rev: "v0.11.0.1"
hooks:
- id: shellcheck

- repo: https://github.com/codespell-project/codespell
rev: "v2.2.4"
rev: "v2.4.2"
hooks:
- id: codespell
args: ["-L", "te,slowy,aray,ba,nd,classs,crate,feld,lits"]
Expand Down
12 changes: 4 additions & 8 deletions examples/test_install_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ def test_install_from_custom_server(selenium_standalone):
url = base_url + "snowballstemmer-2.2.0-py2.py3-none-any.whl"

selenium = selenium_standalone
selenium.run_js(
f"""
selenium.run_js(f"""
await pyodide.loadPackage({url!r});
"""
)
selenium.run(
"""
""")
selenium.run("""
import snowballstemmer
stemmer = snowballstemmer.stemmer('english')
assert stemmer.stemWords(["university"]) == ["univers"]
"""
)
""")
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ lint.select = [
"RET505",
"RET506",
]
lint.ignore = ["E402", "E501", "E731", "E741"]
# line-length = 219 # E501: Recommended goal is 88 to match black
target-version = "py312"
lint.ignore = [
"E402", # import not at top of file
"E501", # line too long
"PLC0415", # import outside top level
]
target-version = "py311"


[tool.ruff.lint.isort]
Expand Down
4 changes: 2 additions & 2 deletions pytest_pyodide/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def set_initialize_script(self, initialize_script: str):
def get_initialize_script(self) -> str:
return self.initialize_script

def add_node_extra_globals(self, l: Iterable[str]):
self.node_extra_globals.extend(l)
def add_node_extra_globals(self, extra_globals: Iterable[str]):
self.node_extra_globals.extend(extra_globals)

def get_node_extra_globals(self) -> Sequence[str]:
return self.node_extra_globals
Expand Down
30 changes: 10 additions & 20 deletions pytest_pyodide/copy_files_to_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ def copy_files_to_emscripten_fs(
# fetch all files into the pyodide
# n.b. this might be slow for big packages

selenium.run(
"""
selenium.run("""
import os
from pathlib import Path
from pyodide.http import pyfetch
Expand All @@ -107,37 +106,28 @@ async def _fetch_file(src,dest):
byte_data = await response.bytes()
fp.write(byte_data)

"""
)
""")
for file, dest in new_files:
_copied_files[selenium].append((file, dest))
file_url = base_url + str(file.relative_to(base_path).as_posix())
if file.suffix == ".whl" and install_wheels:
# wheel - install the wheel on the pyodide side before
# any fetches (and don't copy it)
selenium.run_async(
f"""
selenium.run_async(f"""
all_wheels.append("{file_url}")
"""
)
""")
else:
# add file to fetches
selenium.run_async(
f"""
selenium.run_async(f"""
all_fetches.append(_fetch_file("{file_url}",Path("{dest}")))
"""
)
""")
# install all wheels with micropip
selenium.run_async(
"""
selenium.run_async("""
import micropip
await micropip.install(all_wheels)
"""
)
""")
# fetch everything all at once
selenium.run_async(
"""
selenium.run_async("""
import asyncio, os, os.path
await asyncio.gather(*all_fetches)
"""
)
""")
47 changes: 16 additions & 31 deletions pytest_pyodide/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@ class SeleniumType(Protocol):
JavascriptException: type
browser: str

def load_package(self, pkgs: str | list[str]):
...
def load_package(self, pkgs: str | list[str]): ...

def run_async(self, code: str):
...
def run_async(self, code: str): ...

def run_js(self, code: str):
...
def run_js(self, code: str): ...


class _ReadableFileobj(Protocol):
def read(self, __n: int) -> bytes:
...
def read(self, __n: int) -> bytes: ...

def readline(self) -> bytes:
...
def readline(self) -> bytes: ...


class Unpickler(pickle.Unpickler):
Expand Down Expand Up @@ -86,11 +81,9 @@ def __del__(self):
return
ptr = self.ptr
self.ptr = None
self.selenium.run_js(
f"""
self.selenium.run_js(f"""
pyodide._module._Py_DecRef({ptr});
"""
)
""")


def _encode(obj: Any) -> str:
Expand Down Expand Up @@ -246,12 +239,10 @@ def <func_name>(<selenium_arg_name>, arg1, arg2, arg3):
# Make onwards call with two args:
# 1. <selenium_arg_name>
# 2. all other arguments in a tuple
func_body = ast.parse(
"""\
func_body = ast.parse("""\
__tracebackhide__ = True; \
return run(selenium_arg_name, (arg1, arg2, ...)) \
""".strip()
).body
""".strip()).body
onwards_call = func_body[1].value # type: ignore[attr-defined]
onwards_call.func = ast.Name(id=run_id, ctx=ast.Load())
onwards_call.args[0].id = selenium_arg_name # Set variable name
Expand Down Expand Up @@ -293,8 +284,7 @@ def initialize_decorator(selenium):
_decorator_in_pyodide = (
Path(__file__).parent / "_decorator_in_pyodide.py"
).read_text()
selenium.run(
f"""
selenium.run(f"""
def temp():
_decorator_in_pyodide = '''{_decorator_in_pyodide}'''
from importlib.machinery import ModuleSpec
Expand All @@ -315,8 +305,7 @@ def temp():
sys.modules[modname] = mod
temp()
del temp
"""
)
""")


def _locate_funcdef(
Expand Down Expand Up @@ -547,24 +536,20 @@ def __init__(

def _get_code_prelude(self):
"""Start coverage with the coverage_args passed from the host"""
return dedent(
f"""
return dedent(f"""
from pytest_pyodide.decorator import start_coverage
coverage = start_coverage({_encode(self._coverage_args)!r})
"""
)
""")

def _get_code_epilogue(self):
"""Stop coverage and append the data to the result"""
return dedent(
"""
return dedent("""
from pytest_pyodide.decorator import end_coverage
coverage_outdata = end_coverage(coverage)
result = (*result, coverage_outdata)
"""
)
""")

def _process_extra(self, coverage_out_binary): # type:ignore[override]
def _process_extra(self, coverage_out_binary): # type: ignore[override]
"""Write coverage data to the file system"""
_get_coverage_path().write_bytes(b64decode(coverage_out_binary))

Expand Down
4 changes: 2 additions & 2 deletions pytest_pyodide/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def collect(self):
"""Call super and then if test includes the RUN_IN_PYODIDE option on the
first line, make one copy for each Pyodide runtime environment
"""
for item in super().collect(): # type:ignore[misc]
for item in super().collect(): # type: ignore[misc]
pyodide_test = RUN_IN_PYODIDE in item.dtest.examples[0].options
item.dtest.pyodide_test = pyodide_test
if not pyodide_test:
Expand Down Expand Up @@ -140,7 +140,7 @@ def run_doctest_in_pyodide_outer(
# So we just take the DocTestRunner apart and put it back together inside
# Pyodide.
optionflags = self.optionflags
continue_on_failure = self.continue_on_failure # type:ignore[attr-defined]
continue_on_failure = self.continue_on_failure # type: ignore[attr-defined]

return run_doctest_in_pyodide_inner(
selenium, optionflags, continue_on_failure, test, compileflags, out, clear_globs
Expand Down
41 changes: 23 additions & 18 deletions pytest_pyodide/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def wrapper(*args, **kwargs):
kwargs[orig_name] = kwargs.pop(new_name)
return f(*args, **kwargs)

wrapper.__signature__ = new_sig # type:ignore[attr-defined]
wrapper.__signature__ = new_sig # type: ignore[attr-defined]
return wrapper

return use_variant
Expand Down Expand Up @@ -295,10 +295,11 @@ def selenium_context_manager(selenium_module_scope):

@pytest.fixture
def selenium(request, selenium_module_scope):
with selenium_context_manager(
selenium_module_scope
) as selenium, set_webdriver_script_timeout(
selenium, script_timeout=parse_driver_timeout(request.node)
with (
selenium_context_manager(selenium_module_scope) as selenium,
set_webdriver_script_timeout(
selenium, script_timeout=parse_driver_timeout(request.node)
),
):
yield selenium

Expand Down Expand Up @@ -337,10 +338,11 @@ def selenium_worker_context_manager(selenium_worker_module_scope):

@pytest.fixture
def selenium_worker(request, selenium_worker_module_scope):
with selenium_worker_context_manager(
selenium_worker_module_scope
) as selenium, set_webdriver_script_timeout(
selenium, script_timeout=parse_driver_timeout(request.node)
with (
selenium_worker_context_manager(selenium_worker_module_scope) as selenium,
set_webdriver_script_timeout(
selenium, script_timeout=parse_driver_timeout(request.node)
),
):
yield selenium

Expand Down Expand Up @@ -368,15 +370,18 @@ def selenium_jspi_inner(
pytest.skip(f"jspi not supported in {runtime}")
if request.config.option.runner.lower() == "playwright":
pytest.skip("jspi not supported with playwright")
with selenium_common(
request,
runtime,
web_server_main,
browsers=playwright_browsers,
jspi=True,
worker=worker,
) as selenium, set_webdriver_script_timeout(
selenium, script_timeout=parse_driver_timeout(request.node)
with (
selenium_common(
request,
runtime,
web_server_main,
browsers=playwright_browsers,
jspi=True,
worker=worker,
) as selenium,
set_webdriver_script_timeout(
selenium, script_timeout=parse_driver_timeout(request.node)
),
):
yield selenium

Expand Down
6 changes: 2 additions & 4 deletions pytest_pyodide/run_tests_inside_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,15 @@ def run_test_in_pyodide(node_tree_id, selenium, ignore_fail=False):
"-o",
"junit_logging=out-err",
]
ret_xml = selenium.run_async(
f"""
ret_xml = selenium.run_async(f"""
import pytest
retcode = pytest.main({all_args})

output_xml=""
with open("test_output.xml","r") as f:
output_xml=f.read()
output_xml
"""
)
""")
# get the error from junitxml
root = ET.fromstring(ret_xml)
fails = root.findall("*/testcase[failure]")
Expand Down
Loading
Loading