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
|
from __future__ import annotations
import contextlib
import os
import random
import textwrap
from collections.abc import Generator
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import pytest
from dummyserver.app import pyodide_testing_app
from dummyserver.hypercornserver import run_hypercorn_in_thread
from dummyserver.socketserver import DEFAULT_CERTS
_coverage_count = 0
def _get_coverage_filename(prefix: str) -> str:
global _coverage_count
_coverage_count += 1
rand_part = "".join([random.choice("1234567890") for x in range(20)])
return prefix + rand_part + f".{_coverage_count}"
@pytest.fixture(scope="module")
def testserver_http(
request: pytest.FixtureRequest,
) -> Generator[PyodideServerInfo]:
pyodide_dist_dir = Path(os.getcwd(), request.config.getoption("--dist-dir"))
pyodide_testing_app.config["pyodide_dist_dir"] = str(pyodide_dist_dir)
http_host = "localhost"
with contextlib.ExitStack() as stack:
http_port = stack.enter_context(
run_hypercorn_in_thread(http_host, None, pyodide_testing_app)
)
https_port = stack.enter_context(
run_hypercorn_in_thread(http_host, DEFAULT_CERTS, pyodide_testing_app)
)
yield PyodideServerInfo(
http_host=http_host,
http_port=http_port,
https_port=https_port,
pyodide_dist_dir=pyodide_dist_dir,
)
print("Server teardown")
@dataclass
class PyodideServerInfo:
http_port: int
https_port: int
http_host: str
pyodide_dist_dir: Path
@pytest.fixture()
def selenium_with_jspi_if_possible(
request: pytest.FixtureRequest, runtime: str, has_jspi: bool
) -> Generator[Any]:
if runtime.startswith("firefox") or not has_jspi:
fixture_name = "selenium"
with_jspi = False
else:
fixture_name = "selenium_jspi"
with_jspi = True
selenium_obj = request.getfixturevalue(fixture_name)
selenium_obj.with_jspi = with_jspi
yield selenium_obj
@pytest.fixture()
def selenium_coverage(
selenium_with_jspi_if_possible: Any, testserver_http: PyodideServerInfo
) -> Generator[Any]:
def _install_packages(self: Any) -> None:
if self.browser == "node":
# stop Node.js checking our https certificates
self.run_js('process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;')
# install urllib3 from our test server, rather than from existing package
result = self.run_js(
f'await pyodide.loadPackage("http://{testserver_http.http_host}:{testserver_http.http_port}/dist/urllib3.whl")'
)
if not self.with_jspi:
# force Chrome to execute the current test without JSPI
# even though it is always enabled in
# chrome >= 137. We do this by monkeypatching
# pyodide.ffi.can_run_sync
self.run_async(
"""
import pyodide.ffi
if pyodide.ffi.can_run_sync():
pyodide.ffi.can_run_sync = lambda: False
"""
)
print("Installed package:", result)
self.run_js(
"""
await pyodide.loadPackage("coverage")
await pyodide.runPythonAsync(`import coverage
_coverage= coverage.Coverage(source_pkgs=['urllib3'])
_coverage.start()
`
)"""
)
setattr(
selenium_with_jspi_if_possible,
"_install_packages",
_install_packages.__get__(
selenium_with_jspi_if_possible, selenium_with_jspi_if_possible.__class__
),
)
selenium_with_jspi_if_possible._install_packages()
yield selenium_with_jspi_if_possible
# on teardown, save _coverage output
coverage_out_binary = bytes(
selenium_with_jspi_if_possible.run_js(
"""
return await pyodide.runPythonAsync(`
_coverage.stop()
_coverage.save()
_coverage_datafile = open(".coverage","rb")
_coverage_outdata = _coverage_datafile.read()
# avoid polluting main namespace too much
import js as _coverage_js
# convert to js Array (as default conversion is TypedArray which does
# bad things in firefox)
_coverage_js.Array.from_(_coverage_outdata)
`)
"""
)
)
with open(f"{_get_coverage_filename('.coverage.emscripten.')}", "wb") as outfile:
outfile.write(coverage_out_binary)
class ServerRunnerInfo:
def __init__(
self, host: str, port: int, selenium: Any, dist_dir: Path, has_jspi: bool
) -> None:
self.host = host
self.port = port
self.selenium = selenium
self.dist_dir = dist_dir
self.has_jspi = has_jspi
def run_webworker(self, code: str) -> Any:
if isinstance(code, str) and code.startswith("\n"):
# we have a multiline string, fix indentation
code = textwrap.dedent(code)
# add coverage collection to this code
coverage_init_code = textwrap.dedent(
"""
import coverage
_coverage= coverage.Coverage(source_pkgs=['urllib3'])
_coverage.start()
"""
)
# Monkeypatch pyodide to force disable JSPI in newer chrome
# so those code paths get tested
if self.has_jspi is False:
jspi_fix_code = textwrap.dedent(
"""
import pyodide.ffi
if pyodide.ffi.can_run_sync():
pyodide.ffi.can_run_sync = lambda: False
"""
)
else:
jspi_fix_code = ""
coverage_end_code = textwrap.dedent(
"""
_coverage.stop()
_coverage.save()
_coverage_datafile = open(".coverage","rb")
_coverage_outdata = _coverage_datafile.read()
# avoid polluting main namespace too much
import js as _coverage_js
# convert to js Array (as default conversion is TypedArray which does
# bad things in firefox)
_coverage_js.Array.from_(_coverage_outdata)
"""
)
# the ordering of these code blocks is important - makes sure
# that the first thing that happens is our wheel is loaded
code = (
coverage_init_code
+ "\n"
+ jspi_fix_code
+ "\n"
+ code
+ "\n"
+ coverage_end_code
)
if self.selenium.browser == "firefox":
# running in worker is SLOW on firefox
self.selenium.set_script_timeout(30)
if self.selenium.browser == "node":
worker_path = str(self.dist_dir / "webworker_dev.js")
self.selenium.run_js(
f"""const {{
Worker, isMainThread, parentPort, workerData,
}} = require('node:worker_threads');
globalThis.Worker= Worker;
process.chdir('{self.dist_dir}');
"""
)
else:
worker_path = f"https://{self.host}:{self.port}/pyodide/webworker_dev.js"
coverage_out_binary = bytes(
self.selenium.run_js(
f"""
let worker = new Worker('{worker_path}');
let p = new Promise((res, rej) => {{
worker.onmessageerror = e => rej(e);
worker.onerror = e => rej(e);
worker.onmessage = e => {{
if (e.data.results) {{
res(e.data.results);
}} else {{
rej(e.data.error);
}}
}};
worker.postMessage({{ python: {repr(code)} }});
}});
return await p;
""",
pyodide_checks=False,
)
)
with open(
f"{_get_coverage_filename('.coverage.emscripten.worker.')}", "wb"
) as outfile:
outfile.write(coverage_out_binary)
# run pyodide on our test server instead of on the default
# pytest-pyodide one - this makes it so that
# we are at the same origin as web requests to server_host
@pytest.fixture()
def run_from_server(
selenium_coverage: Any, testserver_http: PyodideServerInfo
) -> Generator[ServerRunnerInfo]:
if selenium_coverage.browser != "node":
# on node, we don't need to be on the same origin
# so we can ignore all this
addr = f"https://{testserver_http.http_host}:{testserver_http.https_port}/pyodide/test.html"
selenium_coverage.goto(addr)
selenium_coverage.javascript_setup()
selenium_coverage.load_pyodide()
selenium_coverage.initialize_pyodide()
selenium_coverage.save_state()
selenium_coverage.restore_state()
selenium_coverage._install_packages()
dist_dir = testserver_http.pyodide_dist_dir
yield ServerRunnerInfo(
testserver_http.http_host,
testserver_http.https_port,
selenium_coverage,
dist_dir,
selenium_coverage.with_jspi,
)
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
"""Generate tests with WebAssembly JavaScript Promise Integration both
enabled and disabled depending on browser/node.js support for features.
Also drops any test that requires a browser or web-workers in Node.js.
).
"""
if "has_jspi" in metafunc.fixturenames:
can_run_with_jspi = False
can_run_without_jspi = False
# node only supports JSPI and doesn't support workers or
# webbrowser specific tests
if metafunc.config.getoption("--runtime").startswith("node"):
if (
metafunc.definition.get_closest_marker("webworkers") is None
and metafunc.definition.get_closest_marker("in_webbrowser") is None
):
can_run_with_jspi = True
if metafunc.definition.get_closest_marker("node_without_jspi"):
can_run_without_jspi = True
can_run_with_jspi = False
# firefox doesn't support JSPI
elif metafunc.config.getoption("--runtime").startswith("firefox"):
can_run_without_jspi = True
else:
# chrome supports JSPI on or off
can_run_without_jspi = True
can_run_with_jspi = True
# if the function is marked to only run with or without jspi,
# then disable the alternative option
if metafunc.definition.get_closest_marker("with_jspi"):
can_run_without_jspi = False
elif metafunc.definition.get_closest_marker("without_jspi"):
can_run_with_jspi = False
jspi_options = []
if can_run_without_jspi:
jspi_options.append(False)
if can_run_with_jspi:
jspi_options.append(True)
metafunc.parametrize("has_jspi", jspi_options)
|