Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(std/wasi): build testdata resources offline #6695

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ yarn.lock
# yarn creates this in error.
tools/node_modules/

# compiled wasm files
std/wasi/testdata/snapshot_preview1/

# MacOS generated files
.DS_Store
.DS_Store?
10 changes: 0 additions & 10 deletions std/wasi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,3 @@ if (module.exports._start) {
throw new Error("No entry point found");
}
```

## Testing

The test suite for this module spawns rustc processes to compile various example
Rust programs. You must have wasm targets enabled:

```
rustup target add wasm32-wasi
rustup target add wasm32-unknown-unknown
```
50 changes: 8 additions & 42 deletions std/wasi/snapshot_preview1_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable */

import { assert, assertEquals } from "../testing/asserts.ts";
import { walk } from "../fs/mod.ts";
import * as path from "../path/mod.ts";
import WASI from "./snapshot_preview1.ts";

Expand All @@ -27,48 +28,13 @@ if (import.meta.main) {
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;
}
const entries = walk(outdir, {
exts: ["wasm"],
});

Deno.test(entry.name, async function () {
const basename = entry.name.replace(/\.wasm$/, ".json");
for await (const entry of entries) {
Deno.test(path.relative(testdir, entry.path), async function () {
const basename = entry.path.replace(/\.wasm$/, ".json");
const prelude = await Deno.readTextFile(path.resolve(outdir, basename));
const options = JSON.parse(prelude);

Expand All @@ -85,7 +51,7 @@ if (import.meta.main) {
"--allow-all",
import.meta.url,
prelude,
path.resolve(outdir, entry.name),
entry.path,
],
stdin: "piped",
stdout: "piped",
Expand Down
24 changes: 24 additions & 0 deletions std/wasi/testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# WebAssembly System Interface Test Suite

This directory contains a runtime agnostic test suite for the WebAssembly
System Interface.

The tests are written as standalone WebAssembly command modules compiled
against a specific snapshot of the ABI.

Failure of a test is typically signaled by assertions but may also come from
post conditions specified in the test configuration which is a JSON object
contained at the top of the source code of a test case.

## Prerequisites

- Python
- Rust

## Building

To build all the tests run the following command:

```shell
python build.py
```
61 changes: 61 additions & 0 deletions std/wasi/testdata/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3

import glob
import json
import os
import subprocess


def ensure_dir(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)


def extract_prelude(filepath):
prelude = ''
with open(filepath, 'r') as f:
for line in f:
if line == '\n':
break

prelude += line.split('//')[1]

return prelude


def build_test(filepath, outdir):
dirname = os.path.dirname(filepath)
ensure_dir(os.path.join(outdir, dirname))

basename, ext = os.path.splitext(filepath)

if ext == '.rs':
subprocess.check_call([
'rustc',
'--target',
'wasm32-wasi',
'-o',
os.path.join(outdir, basename + '.wasm'),
filepath,
])
else:
raise ValueError('Invalid source format')

config = json.loads(extract_prelude(filepath))
with open(os.path.join(outdir, basename + '.json'), 'w') as f:
f.write(json.dumps(config, sort_keys=True, indent=2))
f.write('\n')


def main():
snapshots = [
'snapshot_preview1',
]

for snapshot in snapshots:
for filepath in glob.iglob('*/*.rs'):
build_test(filepath, snapshot)


if __name__ == '__main__':
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_env_args.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"args": [
"one",
"two",
"three"
]
}
Binary file not shown.
7 changes: 7 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_env_vars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"one": "1",
"three": "3",
"two": "2"
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_create_dir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/fixture": "fixture"
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_file_seek.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/fixture": "fixture"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
6 changes: 6 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_hard_link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"preopens": {
"/fixture": "fixture",
"/scratch": "scratch"
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/fixture": "fixture"
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_read.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/fixture": "fixture"
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_read_dir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/fixture": "fixture"
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_rename.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"preopens": {
"/fixture": "fixture"
}
}
Binary file not shown.
8 changes: 8 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_fs_write.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": {
"scratch/file": "file"
},
"preopens": {
"/scratch": "scratch"
}
}
Binary file not shown.
3 changes: 3 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_io_stderr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stderr": "Hello, stderr!"
}
Binary file not shown.
3 changes: 3 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_io_stdin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stdin": "Hello, stdin!"
}
Binary file not shown.
3 changes: 3 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_io_stdout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stdout": "Hello, stdout!"
}
Binary file not shown.
3 changes: 3 additions & 0 deletions std/wasi/testdata/snapshot_preview1/rs/std_process_exit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exitCode": "120"
}
Binary file not shown.