diff options
author | Casper Beyer <caspervonb@pm.me> | 2020-08-25 20:36:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-25 08:36:50 -0400 |
commit | f7174267e3e521636cd4abebbecd107d7079a313 (patch) | |
tree | d12db43eda7395fe0dd659dc9d604116d815942e /std/wasi/snapshot_preview1_test.ts | |
parent | 54d336ab20e19664dc2bfa4218f09f8713a06244 (diff) |
test(std/wasi): add wasi-testsuite as a submodule (#7042)
Diffstat (limited to 'std/wasi/snapshot_preview1_test.ts')
-rw-r--r-- | std/wasi/snapshot_preview1_test.ts | 175 |
1 files changed, 79 insertions, 96 deletions
diff --git a/std/wasi/snapshot_preview1_test.ts b/std/wasi/snapshot_preview1_test.ts index dce37d220..aaffb3e20 100644 --- a/std/wasi/snapshot_preview1_test.ts +++ b/std/wasi/snapshot_preview1_test.ts @@ -1,17 +1,32 @@ /* eslint-disable */ import { assert, assertEquals } from "../testing/asserts.ts"; +import { copy } from "../fs/mod.ts"; import * as path from "../path/mod.ts"; import Context from "./snapshot_preview1.ts"; +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"); +} + if (import.meta.main) { const options = JSON.parse(Deno.args[0]); - const binary = await Deno.readFile(Deno.args[1]); + const pathname = Deno.args[1]; + const binary = await Deno.readFile(pathname); const module = await WebAssembly.compile(binary); const context = new Context({ env: options.env, - args: options.args, + args: [pathname].concat(options.args), preopens: options.preopens, }); @@ -25,115 +40,83 @@ if (import.meta.main) { } else { const rootdir = path.dirname(path.fromFileUrl(import.meta.url)); const testdir = path.join(rootdir, "testdata"); - const outdir = path.join(testdir, "snapshot_preview1"); for await (const entry of Deno.readDir(testdir)) { - if (!entry.name.endsWith(".rs")) { - continue; - } - - const process = Deno.run({ - cmd: [ - "rustc", - "--target", - "wasm32-wasi", - "--out-dir", - outdir, - path.join(testdir, entry.name), - ], - stdout: "inherit", - stderr: "inherit", - }); - - const status = await process.status(); - assert(status.success); - - process.close(); - - // TODO(caspervonb) allow the prelude to span multiple lines - const source = await Deno.readTextFile(path.join(testdir, entry.name)); - const prelude = source.match(/^\/\/\s*\{.*/); - if (prelude) { - const basename = entry.name.replace(/.rs$/, ".json"); - await Deno.writeTextFile( - path.join(outdir, basename), - prelude[0].slice(2), - ); - } - } - - for await (const entry of Deno.readDir(outdir)) { if (!entry.name.endsWith(".wasm")) { continue; } - Deno.test(entry.name, async function () { - const basename = entry.name.replace(/\.wasm$/, ".json"); - const prelude = await Deno.readTextFile(path.resolve(outdir, basename)); - const options = JSON.parse(prelude); - - await Deno.mkdir(`${testdir}/scratch`); - - try { - const process = await Deno.run({ - cwd: testdir, - cmd: [ - `${Deno.execPath()}`, - "run", - "--quiet", - "--unstable", - "--allow-all", - import.meta.url, - prelude, - path.resolve(outdir, entry.name), - ], - stdin: "piped", - stdout: "piped", - stderr: "piped", - }); - - if (options.stdin) { - const stdin = new TextEncoder().encode(options.stdin); - await Deno.writeAll(process.stdin, stdin); - } - - process.stdin.close(); - - const stdout = await Deno.readAll(process.stdout); + 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); + } - if (options.stdout) { - assertEquals(new TextDecoder().decode(stdout), options.stdout); - } else { - await Deno.writeAll(Deno.stdout, stdout); - } + process.stdin.close(); - process.stdout.close(); + const stdout = await Deno.readAll(process.stdout); - const stderr = await Deno.readAll(process.stderr); + if (options.stdout) { + assertEquals(new TextDecoder().decode(stdout), options.stdout); + } else { + await Deno.writeAll(Deno.stdout, stdout); + } - if (options.stderr) { - assertEquals(new TextDecoder().decode(stderr), options.stderr); - } else { - await Deno.writeAll(Deno.stderr, stderr); - } + process.stdout.close(); - process.stderr.close(); + const stderr = await Deno.readAll(process.stderr); - if (options.files) { - for (const [key, value] of Object.entries(options.files)) { - assertEquals(value, await Deno.readTextFile(`${testdir}/${key}`)); + if (options.stderr) { + assertEquals(new TextDecoder().decode(stderr), options.stderr); + } else { + await Deno.writeAll(Deno.stderr, stderr); } - } - const status = await process.status(); - assertEquals(status.code, options.exitCode ? +options.exitCode : 0); + process.stderr.close(); - process.close(); - } catch (err) { - throw err; - } finally { - await Deno.remove(`${testdir}/scratch`, { recursive: true }); - } + 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 }); + } + }, }); } } |