-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run emscripten tests with Pyodide on CI
- Loading branch information
Showing
6 changed files
with
146 additions
and
6 deletions.
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 |
---|---|---|
|
@@ -14,3 +14,4 @@ tags | |
test-crates/wheels/ | ||
test-crates/targets/ | ||
test-crates/venvs/ | ||
tests/pyodide/ |
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,54 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import nox | ||
|
||
|
||
def download_pyodide(session: nox.Session, pyodide_dir: Path) -> None: | ||
pyodide_dir.mkdir() | ||
|
||
PYODIDE_DEV = "https://pyodide-cdn2.iodide.io/dev/full/" | ||
pyodide_files = [ | ||
"pyodide.js", | ||
"packages.json", | ||
"pyodide.asm.js", | ||
"pyodide.asm.data", | ||
"pyodide.asm.wasm", | ||
"pyodide_py.tar", | ||
] | ||
with session.chdir(pyodide_dir): | ||
for file in pyodide_files: | ||
session.run("wget", "-q", PYODIDE_DEV + file, external=True) | ||
session.run("npm", "i", "node-fetch", external=True) | ||
|
||
|
||
@nox.session(name="test-emscripten") | ||
def test_emscripten(session: nox.Session): | ||
emscripten_dir = Path("./tests").resolve() | ||
pyodide_dir = emscripten_dir / "pyodide" | ||
if not pyodide_dir.exists(): | ||
download_pyodide(session, pyodide_dir) | ||
|
||
test_crates = [ | ||
"test-crates/pyo3-mixed", | ||
] | ||
for crate in test_crates: | ||
crate = Path(crate).resolve() | ||
|
||
ver = sys.version_info | ||
session.run( | ||
"cargo", | ||
"+nightly", | ||
"run", | ||
"build", | ||
"-m", | ||
str(crate / "Cargo.toml"), | ||
"--target", | ||
"wasm32-unknown-emscripten", | ||
"-i", | ||
f"python{ver.major}.{ver.minor}", | ||
external=True, | ||
) | ||
|
||
with session.chdir(emscripten_dir): | ||
session.run("node", "emscripten_runner.js", str(crate), external=True) |
File renamed without changes.
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ isolated_build = True | |
|
||
[testenv] | ||
deps = pytest | ||
commands = pytest | ||
commands = pytest tests/ |
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,75 @@ | ||
const http = require("http"); | ||
const fs = require("fs"); | ||
const { opendir } = require("node:fs/promises"); | ||
|
||
const { loadPyodide } = require("./pyodide/pyodide.js"); | ||
|
||
const PORT = 8124; | ||
|
||
const server = http | ||
.createServer(function (request, response) { | ||
const filePath = "." + request.url; | ||
const contentType = "application/octet-stream"; | ||
fs.readFile( | ||
process.argv[2] + "/target/wheels/" + filePath, | ||
function (error, content) { | ||
if (error) { | ||
if (error.code == "ENOENT") { | ||
response.writeHead(404); | ||
response.end("Not found"); | ||
response.end(); | ||
} else { | ||
response.writeHead(500); | ||
response.end("error: " + error.code); | ||
response.end(); | ||
} | ||
} else { | ||
response.writeHead(200, { "Content-Type": contentType }); | ||
response.end(content, "utf-8"); | ||
} | ||
} | ||
); | ||
}) | ||
.listen(PORT); | ||
|
||
async function findWheel(distDir) { | ||
const dir = await opendir(distDir); | ||
for await (const dirent of dir) { | ||
if (dirent.name.endsWith("whl")) { | ||
return dirent.name; | ||
} | ||
} | ||
} | ||
|
||
const localhost = `http://0.0.0.0:${PORT}`; | ||
const pkgDir = process.argv[2]; | ||
const distDir = pkgDir + "/target/wheels"; | ||
const testDir = pkgDir + "/tests"; | ||
|
||
async function main() { | ||
const wheelName = await findWheel(distDir); | ||
const wheelURL = `${localhost}/${wheelName}`; | ||
|
||
let errcode = 1; | ||
try { | ||
pyodide = await loadPyodide({ indexURL: "./pyodide", fullStdLib: false }); | ||
pyodide._api.setCdnUrl("https://pyodide-cdn2.iodide.io/dev/full/"); | ||
const FS = pyodide.FS; | ||
const NODEFS = FS.filesystems.NODEFS; | ||
FS.mkdir("/test_dir"); | ||
FS.mount(NODEFS, { root: testDir }, "/test_dir"); | ||
await pyodide.loadPackage(["micropip", "pytest", "tomli"]); | ||
const micropip = pyodide.pyimport("micropip"); | ||
await micropip.install(wheelURL); | ||
const pytest = pyodide.pyimport("pytest"); | ||
errcode = pytest.main(pyodide.toPy(["/test_dir", "-vv"])); | ||
} catch (e) { | ||
console.error(e); | ||
errcode = 1; | ||
} finally { | ||
server.close(); | ||
} | ||
process.exit(errcode); | ||
} | ||
|
||
main(); |