1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-02 17:01:14 -05:00

test(std/wasi): add read, write and metadata tests (#6494)

This commit is contained in:
Casper Beyer 2020-06-27 03:36:46 +08:00 committed by GitHub
parent e10d74a3d3
commit 99f4a710f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 115 additions and 46 deletions

View file

@ -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();
});
}
}

View file

@ -0,0 +1 @@
directory/file

View file

@ -0,0 +1 @@
file

1
std/wasi/testdata/fixture/file vendored Normal file
View file

@ -0,0 +1 @@
file

View file

@ -0,0 +1 @@
directory

View file

@ -0,0 +1 @@
file

28
std/wasi/testdata/std_fs_metadata.rs vendored Normal file
View file

@ -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);
}

17
std/wasi/testdata/std_fs_read.rs vendored Normal file
View file

@ -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"
);
}

5
std/wasi/testdata/std_fs_write.rs vendored Normal file
View file

@ -0,0 +1,5 @@
// { "preopens": { "/scratch": "scratch" }, "files": { "scratch/file": "file" } }
fn main() {
assert!(std::fs::write("/scratch/file", b"file").is_ok())
}