Skip to content

Commit 8bf957c

Browse files
Pass vitest test suite with Component
1 parent 16de0b4 commit 8bf957c

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

packages/npm-packages/ruby-wasm-wasi/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
],
4040
"license": "MIT",
4141
"scripts": {
42-
"test:run": "npm run test:unit && npm run test:vitest && npm run test:e2e",
43-
"test:vitest": "NODE_OPTIONS=\"--experimental-wasi-unstable-preview1\" vitest run --pool=forks --testTimeout 300000 ./test/",
42+
"test:run": "npm run test:unit && npm run test:vitest -- --run && npm run test:e2e",
43+
"test:vitest": "node ../../../node_modules/.bin/vitest ./test/",
4444
"test:unit": "./tools/run-test-unit.mjs",
4545
"test:e2e": "playwright install && npm run test:e2e:examples && npm run test:e2e:integrations",
4646
"test:e2e:examples": "playwright test -c test-e2e/playwright.examples.config.ts",

packages/npm-packages/ruby-wasm-wasi/test/gc.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ describe("GC integration", () => {
2828

2929
test("protect exported Ruby objects", async () => {
3030
function dropRbValue(value) {
31-
value.inner.drop();
31+
if (value.inner.drop) {
32+
value.inner.drop();
33+
} else if (global.gc) {
34+
global.gc();
35+
} else {
36+
console.warn("--expose-gc is not enabled. Skip GC test.")
37+
}
3238
}
3339
const vm = await initRubyVM();
3440
const initialGCCount = Number(vm.eval("GC.count").toString());

packages/npm-packages/ruby-wasm-wasi/test/init.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs/promises";
22
import * as path from "path";
33
import { WASI } from "wasi";
44
import { RubyVM } from "../src/index";
5+
import * as preview2Shim from "@bytecodealliance/preview2-shim"
56

67
const rubyModule = (async () => {
78
let binaryPath;
@@ -19,7 +20,7 @@ const rubyModule = (async () => {
1920
return await WebAssembly.compile(binary.buffer);
2021
})();
2122

22-
const initRubyVM = async ({ suppressStderr } = { suppressStderr: false }) => {
23+
const initModuleRubyVM = async ({ suppressStderr } = { suppressStderr: false }) => {
2324
let preopens = {};
2425
if (process.env.RUBY_ROOT) {
2526
preopens["/usr"] = path.join(process.env.RUBY_ROOT, "./usr");
@@ -51,6 +52,66 @@ const initRubyVM = async ({ suppressStderr } = { suppressStderr: false }) => {
5152
return vm;
5253
};
5354

55+
const moduleCache = new Map();
56+
async function initComponentRubyVM({ suppressStderr } = { suppressStderr: false }) {
57+
const pkgPath = process.env.RUBY_NPM_PACKAGE_ROOT
58+
if (!pkgPath) {
59+
throw new Error("RUBY_NPM_PACKAGE_ROOT must be set");
60+
}
61+
const componentJsPath = path.resolve(pkgPath, "dist/component/ruby.component.js");
62+
const { instantiate } = await import(componentJsPath);
63+
const getCoreModule = async (relativePath) => {
64+
const coreModulePath = path.resolve(pkgPath, "dist/component", relativePath);
65+
if (moduleCache.has(coreModulePath)) {
66+
return moduleCache.get(coreModulePath);
67+
}
68+
const buffer = await fs.readFile(coreModulePath);
69+
const module = WebAssembly.compile(buffer);
70+
moduleCache.set(coreModulePath, module);
71+
return module;
72+
}
73+
const vm = await RubyVM._instantiate(async (jsRuntime) => {
74+
const { cli, clocks, filesystem, io, random, sockets } = preview2Shim;
75+
filesystem._setPreopens({
76+
"/usr": path.join(process.env.RUBY_BUILD_ROOT, "usr"),
77+
"/bundle": path.join(process.env.RUBY_BUILD_ROOT, "bundle"),
78+
})
79+
cli._setArgs(["ruby.wasm"].concat(process.argv.slice(2)));
80+
cli._setCwd("/")
81+
const root = await instantiate(getCoreModule, {
82+
"ruby:js/js-runtime": jsRuntime,
83+
"wasi:cli/environment": cli.environment,
84+
"wasi:cli/exit": cli.exit,
85+
"wasi:cli/stderr": cli.stderr,
86+
"wasi:cli/stdin": cli.stdin,
87+
"wasi:cli/stdout": cli.stdout,
88+
"wasi:cli/terminal-input": cli.terminalInput,
89+
"wasi:cli/terminal-output": cli.terminalOutput,
90+
"wasi:cli/terminal-stderr": cli.terminalStderr,
91+
"wasi:cli/terminal-stdin": cli.terminalStdin,
92+
"wasi:cli/terminal-stdout": cli.terminalStdout,
93+
"wasi:clocks/monotonic-clock": clocks.monotonicClock,
94+
"wasi:clocks/wall-clock": clocks.wallClock,
95+
"wasi:filesystem/preopens": filesystem.preopens,
96+
"wasi:filesystem/types": filesystem.types,
97+
"wasi:io/error": io.error,
98+
"wasi:io/poll": io.poll,
99+
"wasi:io/streams": io.streams,
100+
"wasi:random/random": random.random,
101+
"wasi:sockets/tcp": sockets.tcp,
102+
})
103+
return root.rubyRuntime;
104+
}, {})
105+
return vm;
106+
}
107+
108+
const initRubyVM = async ({ suppressStderr } = { suppressStderr: false }) => {
109+
if (process.env.ENABLE_COMPONENT_TESTS && process.env.ENABLE_COMPONENT_TESTS !== 'false') {
110+
return initComponentRubyVM({ suppressStderr });
111+
}
112+
return initModuleRubyVM({ suppressStderr });
113+
}
114+
54115
class RubyVersion {
55116
constructor(version) {
56117
this.version = version;

packages/npm-packages/ruby-wasm-wasi/test/vm.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ eval:11:in \`<main>'`
120120
});
121121

122122
test("exception while formatting exception backtrace", async () => {
123-
const vm = await initRubyVM();
123+
const vm = await initRubyVM({ suppressStderr: true });
124124
const throwError = () => {
125125
vm.eval(`
126126
class BrokenException < Exception
@@ -155,7 +155,7 @@ eval:11:in \`<main>'`
155155
`,
156156
`JS::RubyVM.eval("raise 'Exception from nested eval'")`,
157157
])("nested VM rewinding operation should throw fatal error", async (code) => {
158-
const vm = await initRubyVM();
158+
const vm = await initRubyVM({ suppressStderr: true });
159159
const setVM = vm.eval(`proc { |vm| JS::RubyVM = vm }`);
160160
setVM.call("call", vm.wrap(vm));
161161
expect(() => {
@@ -189,7 +189,7 @@ eval:11:in \`<main>'`
189189
);
190190

191191
test("caught raise in nested eval is ok", async () => {
192-
const vm = await initRubyVM();
192+
const vm = await initRubyVM({ suppressStderr: true });
193193
const setVM = vm.eval(`proc { |vm| JS::RubyVM = vm }`);
194194
setVM.call("call", vm.wrap(vm));
195195
expect(() => {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
pool: "forks",
6+
poolOptions: {
7+
forks: {
8+
execArgv: ["--experimental-wasi-unstable-preview1", "--expose-gc"],
9+
}
10+
},
11+
testTimeout: 300000,
12+
exclude: [],
13+
},
14+
})

0 commit comments

Comments
 (0)