From 99f4a710f16bc62ac79cfcc79533f76e646c2889 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Sat, 27 Jun 2020 03:36:46 +0800 Subject: [PATCH] test(std/wasi): add read, write and metadata tests (#6494) --- std/wasi/snapshot_preview1_test.ts | 106 ++++++++++-------- std/wasi/testdata/fixture/directory/file | 1 + .../fixture/directory/symlink_to_file | 1 + std/wasi/testdata/fixture/file | 1 + .../testdata/fixture/symlink_to_directory | 1 + std/wasi/testdata/fixture/symlink_to_file | 1 + std/wasi/testdata/std_fs_metadata.rs | 28 +++++ std/wasi/testdata/std_fs_read.rs | 17 +++ std/wasi/testdata/std_fs_write.rs | 5 + 9 files changed, 115 insertions(+), 46 deletions(-) create mode 100644 std/wasi/testdata/fixture/directory/file create mode 120000 std/wasi/testdata/fixture/directory/symlink_to_file create mode 100644 std/wasi/testdata/fixture/file create mode 120000 std/wasi/testdata/fixture/symlink_to_directory create mode 120000 std/wasi/testdata/fixture/symlink_to_file create mode 100644 std/wasi/testdata/std_fs_metadata.rs create mode 100644 std/wasi/testdata/std_fs_read.rs create mode 100644 std/wasi/testdata/std_fs_write.rs diff --git a/std/wasi/snapshot_preview1_test.ts b/std/wasi/snapshot_preview1_test.ts index 37502e606c..262aa7f959 100644 --- a/std/wasi/snapshot_preview1_test.ts +++ b/std/wasi/snapshot_preview1_test.ts @@ -72,54 +72,68 @@ if (import.meta.main) { const prelude = await Deno.readTextFile(path.resolve(outdir, basename)); const options = JSON.parse(prelude); - 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", - }); + await Deno.mkdir(`${testdir}/scratch`); - if (options.stdin) { - const stdin = new TextEncoder().encode(options.stdin); - await Deno.writeAll(process.stdin, stdin); + 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); + + if (options.stdout) { + assertEquals(new TextDecoder().decode(stdout), options.stdout); + } else { + await Deno.writeAll(Deno.stdout, stdout); + } + + process.stdout.close(); + + const stderr = await Deno.readAll(process.stderr); + + if (options.stderr) { + assertEquals(new TextDecoder().decode(stderr), options.stderr); + } else { + await Deno.writeAll(Deno.stderr, stderr); + } + + process.stderr.close(); + + if (options.files) { + for (const [key, value] of Object.entries(options.files)) { + assertEquals(value, await Deno.readTextFile(`${testdir}/${key}`)); + } + } + + const status = await process.status(); + assertEquals(status.code, options.exitCode ? +options.exitCode : 0); + + process.close(); + } catch (err) { + throw err; + } finally { + await Deno.remove(`${testdir}/scratch`, { recursive: true }); } - - process.stdin.close(); - - const stdout = await Deno.readAll(process.stdout); - - if (options.stdout) { - assertEquals(new TextDecoder().decode(stdout), options.stdout); - } else { - await Deno.writeAll(Deno.stdout, stdout); - } - - process.stdout.close(); - - const stderr = await Deno.readAll(process.stderr); - - if (options.stderr) { - assertEquals(new TextDecoder().decode(stderr), options.stderr); - } else { - await Deno.writeAll(Deno.stderr, stderr); - } - - process.stderr.close(); - - const status = await process.status(); - assertEquals(status.code, options.exitCode ? +options.exitCode : 0); - - process.close(); }); } } diff --git a/std/wasi/testdata/fixture/directory/file b/std/wasi/testdata/fixture/directory/file new file mode 100644 index 0000000000..3c4dadafb3 --- /dev/null +++ b/std/wasi/testdata/fixture/directory/file @@ -0,0 +1 @@ +directory/file diff --git a/std/wasi/testdata/fixture/directory/symlink_to_file b/std/wasi/testdata/fixture/directory/symlink_to_file new file mode 120000 index 0000000000..1a010b1c0f --- /dev/null +++ b/std/wasi/testdata/fixture/directory/symlink_to_file @@ -0,0 +1 @@ +file \ No newline at end of file diff --git a/std/wasi/testdata/fixture/file b/std/wasi/testdata/fixture/file new file mode 100644 index 0000000000..f73f3093ff --- /dev/null +++ b/std/wasi/testdata/fixture/file @@ -0,0 +1 @@ +file diff --git a/std/wasi/testdata/fixture/symlink_to_directory b/std/wasi/testdata/fixture/symlink_to_directory new file mode 120000 index 0000000000..6d0450cc24 --- /dev/null +++ b/std/wasi/testdata/fixture/symlink_to_directory @@ -0,0 +1 @@ +directory \ No newline at end of file diff --git a/std/wasi/testdata/fixture/symlink_to_file b/std/wasi/testdata/fixture/symlink_to_file new file mode 120000 index 0000000000..1a010b1c0f --- /dev/null +++ b/std/wasi/testdata/fixture/symlink_to_file @@ -0,0 +1 @@ +file \ No newline at end of file diff --git a/std/wasi/testdata/std_fs_metadata.rs b/std/wasi/testdata/std_fs_metadata.rs new file mode 100644 index 0000000000..914e9ab740 --- /dev/null +++ b/std/wasi/testdata/std_fs_metadata.rs @@ -0,0 +1,28 @@ +// { "preopens": { "/fixture": "fixture" } } + +fn main() { + let metadata = std::fs::metadata("/fixture/directory").unwrap(); + assert!(metadata.is_dir()); + assert!(metadata.len() > 0); + + let metadata = std::fs::metadata("/fixture/symlink_to_directory").unwrap(); + assert!(metadata.is_dir()); + assert!(metadata.len() > 0); + + let metadata = std::fs::metadata("/fixture/file").unwrap(); + assert!(metadata.is_file()); + assert!(metadata.len() > 0); + + let metadata = std::fs::metadata("/fixture/symlink_to_file").unwrap(); + assert!(metadata.is_file()); + assert!(metadata.len() > 0); + + let metadata = std::fs::metadata("/fixture/directory/file").unwrap(); + assert!(metadata.is_file()); + assert!(metadata.len() > 0); + + let metadata = + std::fs::metadata("/fixture/directory/symlink_to_file").unwrap(); + assert!(metadata.is_file()); + assert!(metadata.len() > 0); +} diff --git a/std/wasi/testdata/std_fs_read.rs b/std/wasi/testdata/std_fs_read.rs new file mode 100644 index 0000000000..aa19d3ba43 --- /dev/null +++ b/std/wasi/testdata/std_fs_read.rs @@ -0,0 +1,17 @@ +// { "preopens": { "/fixture": "fixture" } } + +fn main() { + assert_eq!(std::fs::read("/fixture/file").unwrap(), b"file\n"); + assert_eq!( + std::fs::read("/fixture/symlink_to_file").unwrap(), + b"file\n" + ); + assert_eq!( + std::fs::read("/fixture/directory/file").unwrap(), + b"directory/file\n" + ); + assert_eq!( + std::fs::read("/fixture/directory/symlink_to_file").unwrap(), + b"directory/file\n" + ); +} diff --git a/std/wasi/testdata/std_fs_write.rs b/std/wasi/testdata/std_fs_write.rs new file mode 100644 index 0000000000..91f8ca160e --- /dev/null +++ b/std/wasi/testdata/std_fs_write.rs @@ -0,0 +1,5 @@ +// { "preopens": { "/scratch": "scratch" }, "files": { "scratch/file": "file" } } + +fn main() { + assert!(std::fs::write("/scratch/file", b"file").is_ok()) +}