use esbuild for tests

This commit is contained in:
Remko Tronçon 2022-04-14 08:53:53 +02:00
parent 10bfbf3876
commit 387f339780
7 changed files with 111 additions and 1365 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@ node_modules/
public/waforth/
src/waforth.bulkmem.wat
src/waforth.vanilla.wat
build/
*.wasm
*.wasm.hex
*.tmp

View file

@ -5,8 +5,7 @@ const esbuild = require("esbuild");
const path = require("path");
const fs = require("fs");
const { createServer } = require("http");
const { promisify } = require("util");
const exec = promisify(require("child_process").exec);
const { wasmTextPlugin } = require("./scripts/esbuild/wasm-text");
function withWatcher(config, handleBuildFinished = () => {}, port = 8880) {
const watchClients = [];
@ -71,49 +70,10 @@ let buildConfig = {
loader: {
".wasm": "binary",
".js": "jsx",
".png": "file",
".m4a": "file",
".woff2": "file",
".svg": "file",
".woff": "file",
".ttf": "file",
".eot": "file",
".md": "text",
},
sourcemap: true,
metafile: true,
plugins: [
{
name: "wat",
setup(build) {
build.onResolve({ filter: /.\.wat$/ }, async (args) => {
if (args.resolveDir === "") {
return;
}
const watPath = path.isAbsolute(args.path)
? args.path
: path.join(args.resolveDir, args.path);
return {
path: watPath,
namespace: "wat",
watchFiles: [watPath],
};
});
build.onLoad({ filter: /.*/, namespace: "wat" }, async (args) => {
// Would be handy if we could get output from stdout without going through file
const out = args.path.replace(".wat", ".wasm");
const flags = "";
// flags = --debug-names
// console.log("wat: compiling %s", args.path);
await exec(`wat2wasm ${flags} --output=${out} ${args.path}`);
return {
contents: await fs.promises.readFile(out),
loader: "binary",
};
});
},
},
],
plugins: [wasmTextPlugin()],
};
const INDEX_TEMPLATE = `<!doctype html>

View file

@ -6,9 +6,6 @@
"jquery": "^3.5.0"
},
"devDependencies": {
"@babel/core": "^7.17.9",
"@babel/preset-env": "^7.16.11",
"@babel/register": "^7.17.7",
"chai": "^4.3.6",
"esbuild": "^0.14.36",
"eslint": "^8.13.0",
@ -25,8 +22,8 @@
"scripts": {
"build": "./build.js",
"dev": "./build.js --watch --development",
"test": "mocha src/tests/tests.node.js",
"test-watch": "mocha --watch src/tests/tests.node.js",
"test": "./test.js",
"test-watch": "./test.js --watch",
"lint": "eslint ."
}
}

View file

@ -0,0 +1,43 @@
/* eslint-env node */
const { promisify } = require("util");
const exec = promisify(require("child_process").exec);
const fs = require("fs");
const path = require("path");
function wasmTextPlugin() {
return {
name: "wasm-text",
setup(build) {
build.onResolve({ filter: /.\.wat$/ }, async (args) => {
if (args.resolveDir === "") {
return;
}
const watPath = path.isAbsolute(args.path)
? args.path
: path.join(args.resolveDir, args.path);
return {
path: watPath,
namespace: "wasm-text",
watchFiles: [watPath],
};
});
build.onLoad({ filter: /.*/, namespace: "wasm-text" }, async (args) => {
// Would be handy if we could get output from stdout without going through file
const out = args.path.replace(".wat", ".wasm");
const flags = "";
// flags = --debug-names
// console.log("wat: compiling %s", args.path);
await exec(`wat2wasm ${flags} --output=${out} ${args.path}`);
return {
contents: await fs.promises.readFile(out),
loader: "binary",
};
});
},
};
}
module.exports = {
wasmTextPlugin,
};

View file

@ -1,10 +1,4 @@
const fs = require("fs");
const path = require("path");
require("@babel/register")({
presets: ["@babel/preset-env"],
});
const loadTests = require("./suite.js").default;
const wasmModule = fs.readFileSync(path.join(__dirname, "../waforth.wasm"));
loadTests(wasmModule, (s) => {
loadTests(undefined, (s) => {
return Buffer.from(s).toString("base64");
});

56
test.js Executable file
View file

@ -0,0 +1,56 @@
#!/usr/bin/env node
/* eslint-env node */
const esbuild = require("esbuild");
const path = require("path");
const { wasmTextPlugin } = require("./scripts/esbuild/wasm-text");
const Mocha = require("mocha");
let watch = false;
for (const arg of process.argv.slice(2)) {
switch (arg) {
case "--watch":
watch = true;
break;
}
}
function runTests() {
const mocha = new Mocha();
delete require.cache[path.join(__dirname, "build", "tests.js")];
mocha.addFile("build/tests.js");
mocha.run((failures) => (process.exitCode = failures ? 1 : 0));
}
let buildConfig = {
bundle: true,
logLevel: "warning",
entryPoints: [path.join(__dirname, "src", "tests", "tests.node")],
target: "node17",
outdir: path.join(__dirname, "build"),
external: ["fs", "stream", "util", "events", "path"],
minify: false,
loader: {
".wasm": "binary",
},
sourcemap: true,
plugins: [wasmTextPlugin()],
...(watch
? {
watch: {
async onRebuild(error, result) {
if (error) {
console.error(error);
} else {
runTests();
}
},
},
}
: {}),
};
esbuild.build(buildConfig).then(runTests, (e) => {
console.error(e);
process.exit(1);
});

1317
yarn.lock

File diff suppressed because it is too large Load diff