Skip to content

Commit

Permalink
Add testing script in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
kp992 committed Dec 1, 2024
1 parent 47968d9 commit 5eb4dd3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ jobs:
run: |
rm -r _build/.doctrees
jb build lectures --path-output ./ -nW --keep-going
# Soft check on CI to check the WASM compatibility.
- name: Check WASM lectures
shell: bash -l {0}
run: |
python testing/check_wasm.py
- name: Upload Execution Reports (HTML)
uses: actions/upload-artifact@v4
if: failure()
Expand Down
68 changes: 68 additions & 0 deletions testing/check_wasm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Testing script to check whether the lectures added in
# wasm_compatible.yml are compatible with WASM.
# This is a soft-check check based on the imports present.
# It is still recommended to test the lecture on the
# https://github.com/QuantEcon/project.lecture-wasm

import os
import yaml
import jupytext

from pyodide.code import find_imports

# This is the list of imports (libraries) that are not supported
# WASM pyodide kernel in Jupyterlite.
UNSUPPORTED_LIBS = {
'quantecon',
'numba',
}

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(CURRENT_DIR)
LECTURES_DIR = os.path.join(ROOT_DIR, "lectures")
CONFIG_FILE = os.path.join(ROOT_DIR, "wasm_compatible.yml")


def get_wasm_compatible_files():
"""
Get the list of lectures names from the config file.
"""
# Load the YAML configuration
with open(CONFIG_FILE, "r") as file:
config = yaml.safe_load(file)

return config.get("lectures", [])


def convert_md_to_py_string(md_file_path):
"""
Convert a .md(myst) file to Python code string without creating a .py file.
Args:
md_file_path (str): Path to the Markdown file (.md)
Returns:
str: Python code as a string
"""
# Read the markdown file as a Jupytext notebook
with open(md_file_path, 'r', encoding='utf-8') as md_file:
notebook = jupytext.read(md_file)

# Convert the notebook to Python script format
py_code = jupytext.writes(notebook, fmt="py")

return py_code


def test_compatibility():
file_names = get_wasm_compatible_files()
for file_name in file_names:
py_string = convert_md_to_py_string(os.path.join(LECTURES_DIR, file_name + ".md"))
file_imports = find_imports(py_string)
for file_import in file_imports:
if file_import in UNSUPPORTED_LIBS:
error = 'import `{}` in lecture `{}` is not supported by WASM'.format(file_import, file_name)
raise ValueError(error)

if __name__ == "__main__":
test_compatibility()

0 comments on commit 5eb4dd3

Please sign in to comment.