-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwasm_test.js
51 lines (43 loc) · 1.51 KB
/
wasm_test.js
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
// Simple smoke test for WASM mypy wheels
const { loadPyodide } = require("pyodide");
const process = require("process");
const fs = require("fs");
const path = require("path");
const EXPECTED_ERROR = `test.py:1: error: Unsupported operand types for + ("int" and "str") [operator]
Found 1 error in 1 file (checked 1 source file)
`
async function test_mypy() {
let pyodide = await loadPyodide();
await pyodide.loadPackage("micropip")
let micropip = pyodide.pyimport("micropip");
let requirements = fs.readFileSync("mypy/mypy-requirements.txt", {encoding: "utf8"});
await Promise.all(
requirements.split("\n").map(async (requirement) => {
if (!requirement.startsWith("#") && requirement != "") {
await micropip.install([requirement]);
}
})
);
const dist_dir = "mypy/dist";
const files = await fs.promises.readdir(dist_dir);
await pyodide.loadPackage(path.join(dist_dir, files[0]));
pyodide.runPython(`
with open('test.py', 'w') as f:
f.write("1 + ''")
`);
return pyodide.runPython("import mypy.api; mypy.api.run(['test.py'])");
}
function assert(value, expected, message) {
if (value != expected) {
console.log(message);
console.log(value);
process.exit(1);
}
}
test_mypy().then((result) => {
let [stdout, stderr, code] = result.toJs();
assert(code, 1, "Exit code wasn't 1");
assert(stderr, "", "Stderr not empty");
assert(stdout, EXPECTED_ERROR, "Stdout not the expected value");
console.log("Success! Tests passed as expected.");
});