This adds support for `.wat` tests in `cranelift-filetest`. The test runner translates the WAT to Wasm and then uses `cranelift-wasm` to translate the Wasm to CLIF. These tests are always precise output tests. The test expectations can be updated by running tests with the `CRANELIFT_TEST_BLESS=1` environment variable set, similar to our compile precise output tests. The test's expected output is contained in the last comment in the test file. The tests allow for configuring the kinds of heaps used to implement Wasm linear memory via TOML in a `;;!` comment at the start of the test. To get ISA and Cranelift flags parsing available in the filetests crate, I had to move the `parse_sets_and_triple` helper from the `cranelift-tools` binary crate to the `cranelift-reader` crate, where I think it logically fits. Additionally, I had to make some more bits of `cranelift-wasm`'s dummy environment `pub` so that I could properly wrap and compose it with the environment used for the `.wat` tests. I don't think this is a big deal, but if we eventually want to clean this stuff up, we can probably remove the dummy environments completely, remove `translate_module`, and fold them into these new test environments and test runner (since Wasmtime isn't using those things anyways).
49 lines
1.5 KiB
Rust
49 lines
1.5 KiB
Rust
//! Utility functions.
|
|
|
|
use anyhow::Context;
|
|
use std::fs::File;
|
|
use std::io::{self, Read};
|
|
use std::path::{Path, PathBuf};
|
|
use walkdir::WalkDir;
|
|
|
|
/// Read an entire file into a string.
|
|
pub fn read_to_string<P: AsRef<Path>>(path: P) -> anyhow::Result<String> {
|
|
let mut buffer = String::new();
|
|
let path = path.as_ref();
|
|
if path == Path::new("-") {
|
|
let stdin = io::stdin();
|
|
let mut stdin = stdin.lock();
|
|
stdin
|
|
.read_to_string(&mut buffer)
|
|
.context("failed to read stdin to string")?;
|
|
} else {
|
|
let mut file = File::open(path)?;
|
|
file.read_to_string(&mut buffer)
|
|
.with_context(|| format!("failed to read {} to string", path.display()))?;
|
|
}
|
|
Ok(buffer)
|
|
}
|
|
|
|
/// Iterate over all of the files passed as arguments, recursively iterating through directories.
|
|
pub fn iterate_files<'a>(files: &'a [PathBuf]) -> impl Iterator<Item = PathBuf> + 'a {
|
|
files
|
|
.iter()
|
|
.flat_map(WalkDir::new)
|
|
.filter(|f| match f {
|
|
Ok(d) => {
|
|
// Filter out hidden files (starting with .).
|
|
!d.file_name().to_str().map_or(false, |s| s.starts_with('.'))
|
|
// Filter out directories.
|
|
&& !d.file_type().is_dir()
|
|
}
|
|
Err(e) => {
|
|
println!("Unable to read file: {}", e);
|
|
false
|
|
}
|
|
})
|
|
.map(|f| {
|
|
f.expect("this should not happen: we have already filtered out the errors")
|
|
.into_path()
|
|
})
|
|
}
|