Skip to content

Commit

Permalink
Fix up some more WASI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Jun 23, 2020
1 parent c5fec72 commit a0ee282
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 54 deletions.
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use test_generator::{
};

fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=tests/ignores.txt");
println!("cargo:rerun-if-changed=tests/wasi/wasi/unstable/*");
println!("cargo:rerun-if-changed=tests/wasi/wasi/snapshot1/*");

let out_dir = PathBuf::from(
env::var_os("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
Expand Down
2 changes: 1 addition & 1 deletion tests/compilers/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn run_wasi(wast_path: &str, base_dir: &str, compiler: &str) -> anyhow::Resu
let source = {
let mut out = String::new();
let mut f = File::open(wast_path)?;
f.read_to_string(&mut out);
f.read_to_string(&mut out)?;
out
};
let tokens = WasiTest::lex_string(&source)?;
Expand Down
32 changes: 9 additions & 23 deletions tests/ignores.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ singlepass on windows # Singlepass is not yet supported on Windows

## WASI

# due to hard-coded direct calls into WASI for wasi unstable
### due to hard-coded direct calls into WASI for wasi unstable

wasitests_snapshot1::fd_read
wasitests_snapshot1::poll_oneoff
Expand All @@ -17,34 +17,20 @@ wasitests_snapshot1::fd_allocate
wasitests_snapshot1::close_preopen_fd
wasitests_snapshot1::envvar

# TODO: resolve the disabled tests below:
### TODO: resolve the disabled tests below. These are newly disabled tests from the migration:

# due to git clone not preserving symlinks:
### due to git clone not preserving symlinks:
wasitests_snapshot1::readlink
wasitests_unstable::readlink

# due to directory changes:
wasitests_snapshot1::quine
wasitests_unstable::quine


# failing for unknown reasons
wasitests_unstable::fd_pread
wasitests_unstable::create_dir
### failing due to `remove_dir_all`. this test is also bad for parallelism
wasitests_snapshot1::create_dir
wasitests_unstable::file_metadata
wasitests_snapshot1::file_metadata

wasitests_unstable::create_dir

# failing because it closes `stdout` which breaks our testing system
### failing because it closes `stdout` which breaks our testing system
wasitests_unstable::fd_close

# failing because we're missing an extra newline at the end of the output
# could this be due to stdout printing a newline when datasync is called or something?
# TODO: check WasiFile implementation
wasitests_unstable::fd_read

# failing because we're operating on stdout which is now overridden.
# TODO: check WasiFile implementation
# Alterative: split test into 2 parts, one printing to stderr, the other printing to stdout to test the real versions
### failing because we're operating on stdout which is now overridden.
### TODO: check WasiFile implementation
### Alterative: split test into 2 parts, one printing to stderr, the other printing to stdout to test the real versions
wasitests_unstable::poll_oneoff
3 changes: 1 addition & 2 deletions tests/lib/test-generator/src/processors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ pub fn emscripten_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
pub fn wasi_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
// TODO: fix this once we rename `out` to `wast`
if ext != "out" {
if ext != "wast" {
return None;
}

Expand Down
76 changes: 49 additions & 27 deletions tests/lib/wast/src/wasi_wast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ pub struct WasiTest<'a> {
// TODO: add `test_fs` here to sandbox better
const BASE_TEST_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../wasi/wasi/");

fn get_stdout_output(wasi_state: &WasiState) -> anyhow::Result<&str> {
let stdout_boxed = wasi_state.fs.stdout()?.as_ref().unwrap();
let stdout = (&**stdout_boxed)
.downcast_ref::<OutputCapturerer>()
.unwrap();
let stdout_str = std::str::from_utf8(&stdout.output)?;
Ok(stdout_str)
}

fn get_stderr_output(wasi_state: &WasiState) -> anyhow::Result<&str> {
let stderr_boxed = wasi_state.fs.stderr()?.as_ref().unwrap();
let stderr = (&**stderr_boxed)
.downcast_ref::<OutputCapturerer>()
.unwrap();
let stderr_str = std::str::from_utf8(&stderr.output)?;
Ok(stderr_str)
}

#[allow(dead_code)]
impl<'a> WasiTest<'a> {
/// Turn a WASI WAST string into a list of tokens.
Expand Down Expand Up @@ -58,30 +76,31 @@ impl<'a> WasiTest<'a> {

let start = instance.exports.get_function("_start")?;
// TODO: handle errors here when the error fix gets shipped
start
.call(&[])
.with_context(|| "failed to run WASI `_start` function")?;
match start.call(&[]) {
Ok(_) => {}
Err(e) => {
let wasi_state = env.state();
let stdout_str = get_stdout_output(&wasi_state)?;
let stderr_str = get_stderr_output(&wasi_state)?;
Err(e).with_context(|| {
format!(
"failed to run WASI `_start` function: failed with stdout: \"{}\"\nstderr: \"{}\"",
stdout_str,
stderr_str,
)
})?;
}
}

let wasi_state = env.state();
{
let stdout_boxed = wasi_state.fs.stdout()?.as_ref().unwrap();
let stdout = (&**stdout_boxed)
.downcast_ref::<OutputCapturerer>()
.unwrap();
let stdout_str = std::str::from_utf8(&stdout.output)?;
if let Some(expected_stdout) = &self.assert_stdout {
assert_eq!(stdout_str, expected_stdout.expected);
}

if let Some(expected_stdout) = &self.assert_stdout {
let stdout_str = get_stdout_output(&wasi_state)?;
assert_eq!(stdout_str, expected_stdout.expected);
}
{
let stderr_boxed = wasi_state.fs.stderr()?.as_ref().unwrap();
let stderr = (&**stderr_boxed)
.downcast_ref::<OutputCapturerer>()
.unwrap();
let stderr_str = std::str::from_utf8(&stderr.output)?;
if let Some(expected_stderr) = &self.assert_stderr {
assert_eq!(stderr_str, expected_stderr.expected);
}
if let Some(expected_stderr) = &self.assert_stderr {
let stderr_str = get_stderr_output(&wasi_state)?;
assert_eq!(stderr_str, expected_stderr.expected);
}

Ok(true)
Expand All @@ -93,20 +112,23 @@ impl<'a> WasiTest<'a> {
for (name, value) in &self.envs {
builder.env(name, value);
}
// TODO: check the order
for (alias, real_dir) in &self.mapped_dirs {
let mut dir = PathBuf::from(BASE_TEST_DIR);
dir.push(real_dir);
builder.map_dir(alias, dir)?;
}

// due to the structure of our code, all preopen dirs must be mapped now
for dir in &self.dirs {
let mut new_dir = PathBuf::from(BASE_TEST_DIR);
new_dir.push(dir);
builder.map_dir(dir, new_dir)?;
}

let out = builder
.args(&self.args)
.preopen_dirs(self.dirs.iter().map(|d| {
let mut dir = PathBuf::from(BASE_TEST_DIR);
dir.push(d);
dir
}))?
// adding this causes some tests to fail. TODO: investigate this
//.env("RUST_BACKTRACE", "1")
.stdout(Box::new(OutputCapturerer::new()))
.stderr(Box::new(OutputCapturerer::new()))
.finalize()?;
Expand Down

0 comments on commit a0ee282

Please sign in to comment.