mirror of
https://github.com/denoland/deno.git
synced 2024-12-12 02:27:46 -05:00
test(std/wasi): add read, write and metadata tests (#6494)
This commit is contained in:
parent
e10d74a3d3
commit
99f4a710f1
9 changed files with 115 additions and 46 deletions
|
@ -72,54 +72,68 @@ if (import.meta.main) {
|
||||||
const prelude = await Deno.readTextFile(path.resolve(outdir, basename));
|
const prelude = await Deno.readTextFile(path.resolve(outdir, basename));
|
||||||
const options = JSON.parse(prelude);
|
const options = JSON.parse(prelude);
|
||||||
|
|
||||||
const process = await Deno.run({
|
await Deno.mkdir(`${testdir}/scratch`);
|
||||||
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) {
|
try {
|
||||||
const stdin = new TextEncoder().encode(options.stdin);
|
const process = await Deno.run({
|
||||||
await Deno.writeAll(process.stdin, stdin);
|
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
std/wasi/testdata/fixture/directory/file
vendored
Normal file
1
std/wasi/testdata/fixture/directory/file
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
directory/file
|
1
std/wasi/testdata/fixture/directory/symlink_to_file
vendored
Symbolic link
1
std/wasi/testdata/fixture/directory/symlink_to_file
vendored
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
file
|
1
std/wasi/testdata/fixture/file
vendored
Normal file
1
std/wasi/testdata/fixture/file
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
file
|
1
std/wasi/testdata/fixture/symlink_to_directory
vendored
Symbolic link
1
std/wasi/testdata/fixture/symlink_to_directory
vendored
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
directory
|
1
std/wasi/testdata/fixture/symlink_to_file
vendored
Symbolic link
1
std/wasi/testdata/fixture/symlink_to_file
vendored
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
file
|
28
std/wasi/testdata/std_fs_metadata.rs
vendored
Normal file
28
std/wasi/testdata/std_fs_metadata.rs
vendored
Normal 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
17
std/wasi/testdata/std_fs_read.rs
vendored
Normal 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
5
std/wasi/testdata/std_fs_write.rs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// { "preopens": { "/scratch": "scratch" }, "files": { "scratch/file": "file" } }
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert!(std::fs::write("/scratch/file", b"file").is_ok())
|
||||||
|
}
|
Loading…
Reference in a new issue