-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ab0c9d9
commit b915d75
Showing
3 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import functools | ||
import os | ||
import time | ||
from typing import Any | ||
from pathlib import Path | ||
|
||
from pytest_pyodide.runner import _BrowserBaseRunner | ||
from pytest_pyodide.utils import package_is_built as _package_is_built | ||
import pytest | ||
|
||
from conftest import package_is_built | ||
from pyodide_build.io import MetaConfig | ||
|
||
PKG_DIR = Path(__file__).parent | ||
|
||
UNSUPPORTED_PACKAGES: dict[str, list[str]] = { | ||
"chrome": [], | ||
"firefox": [], | ||
"safari": [], | ||
"node": ["cmyt", "yt", "galpy"], | ||
} | ||
if "CI" in os.environ: | ||
UNSUPPORTED_PACKAGES["chrome"].extend(["statsmodels"]) | ||
|
||
XFAIL_PACKAGES: dict[str, str] = { | ||
"soupsieve": "Importing soupsieve without installing beautifulsoup4 fails.", | ||
} | ||
|
||
|
||
@functools.cache | ||
def registered_packages() -> list[str]: | ||
"""Returns a list of registered package names""" | ||
packages = [] | ||
for name in os.listdir(PKG_DIR): | ||
if (PKG_DIR / name).is_dir() and (PKG_DIR / name / "meta.yaml").exists(): | ||
packages.append(name) | ||
|
||
return packages | ||
|
||
|
||
def package_is_built(package_name): | ||
return _package_is_built(package_name, pytest.pyodide_dist_dir) | ||
|
||
|
||
@pytest.mark.parametrize("name", registered_packages()) | ||
def test_parse_package(name: str) -> None: | ||
# check that we can parse the meta.yaml | ||
meta = MetaConfig.from_yaml(PKG_DIR / name / "meta.yaml") | ||
|
||
sharedlibrary = meta.build.package_type == "shared_library" | ||
if name == "sharedlib-test": | ||
assert sharedlibrary is True | ||
elif name == "sharedlib-test-py": | ||
assert sharedlibrary is False | ||
|
||
|
||
@pytest.mark.skip_refcount_check | ||
@pytest.mark.driver_timeout(120) | ||
@pytest.mark.parametrize("name", registered_packages()) | ||
@pytest.mark.benchmark( | ||
max_time=3, | ||
min_rounds=1, | ||
timer=time.perf_counter, | ||
) | ||
def test_import( | ||
name: str, selenium_standalone: _BrowserBaseRunner, benchmark: Any | ||
) -> None: | ||
if not package_is_built(name): | ||
raise AssertionError( | ||
"Implementation error. Test for an unbuilt package " | ||
"should have been skipped in selenium_standalone fixture" | ||
) | ||
|
||
if name in XFAIL_PACKAGES: | ||
pytest.xfail(XFAIL_PACKAGES[name]) | ||
|
||
meta = MetaConfig.from_yaml(PKG_DIR / name / "meta.yaml") | ||
|
||
if name in UNSUPPORTED_PACKAGES[selenium_standalone.browser]: | ||
pytest.xfail( | ||
"{} fails to load and is not supported on {}.".format( | ||
name, selenium_standalone.browser | ||
) | ||
) | ||
|
||
selenium_standalone.run("import glob, os, site") | ||
|
||
def _get_file_count(expr): | ||
return selenium_standalone.run( | ||
f""" | ||
len(list(glob.glob( | ||
site.getsitepackages()[0] + '{expr}', | ||
recursive=True) | ||
)) | ||
""" | ||
) | ||
|
||
import_names = meta.test.imports | ||
if not import_names: | ||
import_names = meta.package.top_level | ||
|
||
if not import_names: | ||
# Nothing to test | ||
return | ||
|
||
def _import_pkg(): | ||
for import_name in import_names: | ||
selenium_standalone.run_async("import %s" % import_name) | ||
|
||
benchmark(_import_pkg) | ||
|
||
# Make sure that after importing, either .py or .pyc are present but not | ||
# both | ||
pyc_count = sum(_get_file_count(f"/{key}/**/*.pyc") for key in import_names) | ||
py_count = sum(_get_file_count(f"/{key}/**/*.py") for key in import_names) | ||
|
||
assert not ((py_count > 0) and (pyc_count > 0)) | ||
|
||
# Make sure no exe files were loaded! | ||
assert _get_file_count("/**/*.exe") == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
pytest | ||
pytest-pyodide==0.58.3 | ||
pytest-httpserver | ||
pytest-benchmark | ||
auditwheel-emscripten | ||
pytest-asyncio | ||
pyodide-build | ||
zstandard | ||
brotli |