use anyhow::{Context, Result};
use std::{fs, path::Path};
use wit_parser::{Resolve, WorldId};
#[test]
fn targets() -> Result<()> {
drop(env_logger::try_init());
for entry in fs::read_dir("tests/targets")? {
let path = entry?.path();
if !path.is_dir() {
continue;
}
let test_case = path.file_stem().unwrap().to_str().unwrap();
println!("testing {test_case}");
let (resolve, world) = load_test_wit(&path)?;
let component = wat::parse_file(path.join("test.wat"))
.with_context(|| "failed to parse component WAT".to_string())?;
match wit_component::targets(&resolve, world, &component) {
Ok(_) => {
assert!(
!test_case.starts_with("error-"),
"should have failed targets check"
);
}
Err(e) => {
assert!(test_case.starts_with("error-"), "{e:?}");
assert_output(&path.join("error.txt"), &format!("{e:#}"))?;
}
}
}
Ok(())
}
fn assert_output(expected: &Path, actual: &str) -> Result<()> {
if std::env::var_os("BLESS").is_some() {
fs::create_dir_all(expected.parent().unwrap())?;
fs::write(expected, actual).with_context(|| format!("failed to write {expected:?}"))?;
} else {
assert_eq!(
fs::read_to_string(expected)
.with_context(|| format!("failed to read {expected:?}"))?
.replace("\r\n", "\n"),
actual,
"expectation `{}` did not match actual",
expected.display(),
);
}
Ok(())
}
fn load_test_wit(path: &Path) -> Result<(Resolve, WorldId)> {
const TEST_TARGET_WORLD_ID: &str = "foobar";
let test_wit_path = path.join("test.wit");
let mut resolve = Resolve::default();
let pkg = resolve.push_file(&test_wit_path)?;
let world_id = resolve
.select_world(&[pkg], Some(TEST_TARGET_WORLD_ID))
.with_context(|| "failed to select world from package".to_string())?;
Ok((resolve, world_id))
}