diff options
| author | Mica White <botahamec@outlook.com> | 2026-10-31 11:10:49 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-10-31 11:10:49 -0400 |
| commit | 264ea48cc8c6dd9c3c9e0fe1a559252863fc9962 (patch) | |
| tree | 84a4a58de14442f4fcfa06bd0d82480508f82759 /crates/cli/src/testsuite.rs | |
| parent | 7877eca4e8be283e7c51168ee3a3b05d9000f39c (diff) | |
| parent | b45a98a85c776d956a797773acd2f198c06b40ed (diff) | |
Merge branch 'main' into bota-tortuise
Diffstat (limited to 'crates/cli/src/testsuite.rs')
| -rw-r--r-- | crates/cli/src/testsuite.rs | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/crates/cli/src/testsuite.rs b/crates/cli/src/testsuite.rs new file mode 100644 index 0000000..b23b2c8 --- /dev/null +++ b/crates/cli/src/testsuite.rs @@ -0,0 +1,167 @@ +use crate::wast_runner::{GroupResult, TestFile as RunnerTestFile, WastRunner}; +use owo_colors::OwoColorize; +use serde::{Deserialize, Serialize}; +use std::fmt::{Display, Formatter}; +use std::io::{BufRead, BufReader, Seek, SeekFrom}; + +/// Result type used by the WAST test utilities. +pub type TestResult<T> = core::result::Result<T, Box<dyn core::error::Error>>; + +struct TestFailure(String); + +impl core::fmt::Debug for TestFailure { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Display::fmt(self, f) + } +} + +impl Display for TestFailure { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +impl core::error::Error for TestFailure {} + +#[derive(Serialize, Deserialize)] +pub struct TestGroupResult { + pub name: String, + pub passed: usize, + pub failed: usize, +} + +pub struct TestSuite { + runner: WastRunner, +} + +impl TestSuite { + pub fn set_log_level(level: log::LevelFilter) { + WastRunner::set_log_level(level); + } + + pub fn new() -> Self { + Self { runner: WastRunner::new() } + } + + pub fn run_paths(&mut self, tests: &[std::path::PathBuf]) -> TestResult<()> { + let mut files = Vec::new(); + for path in tests { + if path.is_dir() { + for entry in std::fs::read_dir(path)? { + let entry = entry?; + let path = entry.path(); + if path.extension().is_some_and(|ext| ext == "wast") { + files.push(path); + } + } + } else { + files.push(path.clone()); + } + } + files.sort(); + + let runner_files = files + .iter() + .map(|path| { + let contents = std::fs::read_to_string(path)?; + let name = path.to_string_lossy().into_owned(); + Ok((name, contents)) + }) + .collect::<TestResult<Vec<_>>>()?; + + self.runner.run_files(runner_files.iter().map(|(name, contents)| RunnerTestFile { + name: name.clone(), + parent: name.clone(), + contents, + }))?; + Ok(()) + } + + pub fn run_files<'a>( + &mut self, + tests: impl IntoIterator<Item = wasm_testsuite::data::TestFile<'a>>, + ) -> TestResult<()> { + self.runner.run_files(tests.into_iter().map(|file| RunnerTestFile { + name: file.name().to_string(), + parent: file.parent().to_string(), + contents: file.raw(), + }))?; + Ok(()) + } + + pub fn print_errors(&self) { + self.runner.print_errors(); + } + + pub fn report_status(&self) -> TestResult<()> { + self.print_errors(); + println!("{self}"); + if self.runner.failed() { Err(TestFailure("failed one or more tests".into()).into()) } else { Ok(()) } + } + + pub fn save_csv(&self, path: &str, version: &str) -> TestResult<()> { + use std::fs::OpenOptions; + use std::io::Write; + + let mut file = OpenOptions::new().create(true).append(true).read(true).open(path)?; + let last_line = BufReader::new(&file).lines().last().transpose()?; + + if let Some(last) = last_line + && last.starts_with(version) + { + let len_to_truncate = last.len() as i64; + file.set_len(file.metadata()?.len() - len_to_truncate as u64 - 1)?; + } + + file.seek(SeekFrom::End(0))?; + + let mut passed = 0; + let mut failed = 0; + let mut groups = Vec::new(); + + for group in self.runner.group_results() { + passed += group.passed; + failed += group.failed; + groups.push(TestGroupResult { name: group.name, passed: group.passed, failed: group.failed }); + } + + let groups = serde_json::to_string(&groups)?; + let line = format!("{version},{passed},{failed},{groups}\n"); + file.write_all(line.as_bytes())?; + Ok(()) + } + + fn group_results(&self) -> Vec<GroupResult> { + self.runner.group_results() + } +} + +impl Default for TestSuite { + fn default() -> Self { + Self::new() + } +} + +impl Display for TestSuite { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut total_passed = 0; + let mut total_failed = 0; + + for group in self.group_results() { + total_passed += group.passed; + total_failed += group.failed; + + writeln!(f, "{}", group.name.bold().underline())?; + writeln!(f, " Tests Passed: {}", group.passed.to_string().green())?; + if group.failed != 0 { + writeln!(f, " Tests Failed: {}", group.failed.to_string().red())?; + } + } + + writeln!(f, "\n{}", "Total Test Summary:".bold().underline())?; + writeln!(f, " Total Tests: {}", total_passed + total_failed)?; + writeln!(f, " Total Passed: {}", total_passed.to_string().green())?; + writeln!(f, " Total Failed: {}", total_failed.to_string().red())?; + Ok(()) + } +} |
