2020-06-24 09:27:31 -04:00
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
import { assert, assertEquals } from "../testing/asserts.ts";
|
2020-08-25 08:36:50 -04:00
|
|
|
import { copy } from "../fs/mod.ts";
|
2020-06-24 09:27:31 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2020-08-24 12:21:06 -04:00
|
|
|
import Context from "./snapshot_preview1.ts";
|
2020-06-24 09:27:31 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
const ignore = [
|
|
|
|
"wasi_clock_time_get_realtime.wasm",
|
|
|
|
];
|
|
|
|
|
|
|
|
// TODO(caspervonb) investigate why these tests are failing on windows and fix
|
|
|
|
// them.
|
|
|
|
if (Deno.build.os == "windows") {
|
|
|
|
ignore.push("std_fs_metadata_absolute.wasm");
|
|
|
|
ignore.push("std_fs_metadata_relative.wasm");
|
|
|
|
ignore.push("std_fs_read_dir_absolute.wasm");
|
|
|
|
ignore.push("std_fs_read_dir_relative.wasm");
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:27:31 -04:00
|
|
|
if (import.meta.main) {
|
|
|
|
const options = JSON.parse(Deno.args[0]);
|
2020-08-25 08:36:50 -04:00
|
|
|
const pathname = Deno.args[1];
|
|
|
|
const binary = await Deno.readFile(pathname);
|
2020-06-24 09:27:31 -04:00
|
|
|
const module = await WebAssembly.compile(binary);
|
|
|
|
|
2020-08-24 12:21:06 -04:00
|
|
|
const context = new Context({
|
2020-06-24 09:27:31 -04:00
|
|
|
env: options.env,
|
2020-08-25 08:36:50 -04:00
|
|
|
args: [pathname].concat(options.args),
|
2020-06-24 09:27:31 -04:00
|
|
|
preopens: options.preopens,
|
|
|
|
});
|
|
|
|
|
|
|
|
const instance = new WebAssembly.Instance(module, {
|
2020-08-24 12:21:06 -04:00
|
|
|
wasi_snapshot_preview1: context.exports,
|
2020-06-24 09:27:31 -04:00
|
|
|
});
|
|
|
|
|
2020-08-24 12:21:06 -04:00
|
|
|
context.memory = instance.exports.memory;
|
2020-06-24 09:27:31 -04:00
|
|
|
|
|
|
|
instance.exports._start();
|
|
|
|
} else {
|
|
|
|
const rootdir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdir = path.join(rootdir, "testdata");
|
|
|
|
|
|
|
|
for await (const entry of Deno.readDir(testdir)) {
|
|
|
|
if (!entry.name.endsWith(".wasm")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
Deno.test({
|
|
|
|
name: entry.name,
|
|
|
|
ignore: ignore.includes(entry.name),
|
|
|
|
fn: async function () {
|
|
|
|
const basename = entry.name.replace(/\.wasm$/, ".json");
|
|
|
|
const prelude = await Deno.readTextFile(
|
|
|
|
path.resolve(testdir, basename),
|
|
|
|
);
|
|
|
|
const options = JSON.parse(prelude);
|
|
|
|
|
|
|
|
const workdir = await Deno.makeTempDir();
|
|
|
|
await copy(
|
|
|
|
path.join(testdir, "fixtures"),
|
|
|
|
path.join(workdir, "fixtures"),
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const process = await Deno.run({
|
|
|
|
cwd: workdir,
|
|
|
|
cmd: [
|
|
|
|
`${Deno.execPath()}`,
|
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--unstable",
|
|
|
|
"--allow-all",
|
|
|
|
import.meta.url,
|
|
|
|
prelude,
|
|
|
|
path.resolve(testdir, entry.name),
|
|
|
|
],
|
|
|
|
stdin: "piped",
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.stdin) {
|
|
|
|
const stdin = new TextEncoder().encode(options.stdin);
|
|
|
|
await Deno.writeAll(process.stdin, stdin);
|
|
|
|
}
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
process.stdin.close();
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
const stdout = await Deno.readAll(process.stdout);
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
if (options.stdout) {
|
|
|
|
assertEquals(new TextDecoder().decode(stdout), options.stdout);
|
|
|
|
} else {
|
|
|
|
await Deno.writeAll(Deno.stdout, stdout);
|
|
|
|
}
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
process.stdout.close();
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
const stderr = await Deno.readAll(process.stderr);
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
if (options.stderr) {
|
|
|
|
assertEquals(new TextDecoder().decode(stderr), options.stderr);
|
|
|
|
} else {
|
|
|
|
await Deno.writeAll(Deno.stderr, stderr);
|
2020-06-26 15:36:46 -04:00
|
|
|
}
|
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
process.stderr.close();
|
2020-06-26 15:36:46 -04:00
|
|
|
|
2020-08-25 08:36:50 -04:00
|
|
|
const status = await process.status();
|
|
|
|
assertEquals(status.code, options.exitCode ? +options.exitCode : 0);
|
|
|
|
|
|
|
|
process.close();
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(workdir, { recursive: true });
|
|
|
|
}
|
|
|
|
},
|
2020-06-24 09:27:31 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|