diff --git a/crates/snapbox/src/assert/mod.rs b/crates/snapbox/src/assert/mod.rs index d4bc7de4..5cf0f0ec 100644 --- a/crates/snapbox/src/assert/mod.rs +++ b/crates/snapbox/src/assert/mod.rs @@ -29,7 +29,7 @@ pub use error::Result; /// ``` #[derive(Clone, Debug)] pub struct Assert { - action: Action, + pub(crate) action: Action, action_var: Option, normalize_paths: bool, substitutions: crate::Redactions, diff --git a/crates/snapbox/src/harness.rs b/crates/snapbox/src/harness.rs index 0ade60f7..f5927824 100644 --- a/crates/snapbox/src/harness.rs +++ b/crates/snapbox/src/harness.rs @@ -36,7 +36,6 @@ //! ``` use crate::data::DataFormat; -use crate::filter::{Filter as _, FilterNewlines}; use crate::Action; use libtest_mimic::Trial; @@ -49,7 +48,7 @@ pub struct Harness { overrides: Option, setup: S, test: T, - action: Action, + config: crate::Assert, } impl Harness @@ -71,7 +70,7 @@ where overrides: None, setup, test, - action: Action::Verify, + config: crate::Assert::new().action_env(crate::assert::DEFAULT_ACTION_ENV), } } @@ -89,14 +88,13 @@ where /// Read the failure action from an environment variable pub fn action_env(mut self, var_name: &str) -> Self { - let action = Action::with_env_var(var_name); - self.action = action.unwrap_or(self.action); + self.config = self.config.action_env(var_name); self } /// Override the failure action pub fn action(mut self, action: Action) -> Self { - self.action = action; + self.config = self.config.action(action); self } @@ -118,23 +116,22 @@ where } }); + let shared_config = std::sync::Arc::new(self.config); let tests: Vec<_> = tests .into_iter() .map(|path| { let case = (self.setup)(path); let test = self.test.clone(); + let config = shared_config.clone(); Trial::test(case.name.clone(), move || { + let expected = crate::Data::read_from(&case.expected, Some(DataFormat::Text)); let actual = (test)(&case.fixture)?; let actual = actual.to_string(); - let actual = FilterNewlines.filter(crate::Data::text(actual)); - #[allow(deprecated)] - let verify = Verifier::new() - .palette(crate::report::Palette::auto()) - .action(self.action); - verify.verify(&case.expected, actual)?; + let actual = crate::Data::text(actual); + config.try_eq(expected, actual, Some(&case.name))?; Ok(()) }) - .with_ignored_flag(self.action == Action::Ignore) + .with_ignored_flag(shared_config.action == Action::Ignore) }) .collect(); @@ -143,89 +140,6 @@ where } } -struct Verifier { - palette: crate::report::Palette, - action: Action, -} - -impl Verifier { - fn new() -> Self { - Default::default() - } - - fn palette(mut self, palette: crate::report::Palette) -> Self { - self.palette = palette; - self - } - - fn action(mut self, action: Action) -> Self { - self.action = action; - self - } - - fn verify( - &self, - expected_path: &std::path::Path, - actual: crate::Data, - ) -> crate::assert::Result<()> { - match self.action { - Action::Skip => Ok(()), - Action::Ignore => { - let _ = self.try_verify(expected_path, actual); - Ok(()) - } - Action::Verify => self.try_verify(expected_path, actual), - Action::Overwrite => self.try_overwrite(expected_path, actual), - } - } - - fn try_overwrite( - &self, - expected_path: &std::path::Path, - actual: crate::Data, - ) -> crate::assert::Result<()> { - actual.write_to_path(expected_path)?; - Ok(()) - } - - fn try_verify( - &self, - expected_path: &std::path::Path, - actual: crate::Data, - ) -> crate::assert::Result<()> { - let expected = FilterNewlines.filter(crate::Data::read_from( - expected_path, - Some(DataFormat::Text), - )); - - if expected != actual { - let mut buf = String::new(); - crate::report::write_diff( - &mut buf, - &expected, - &actual, - Some(&expected_path.display()), - None, - self.palette, - ) - .map_err(|e| e.to_string())?; - Err(buf.into()) - } else { - Ok(()) - } - } -} - -impl Default for Verifier { - fn default() -> Self { - Self { - #[allow(deprecated)] - palette: crate::report::Palette::auto(), - action: Action::Verify, - } - } -} - /// A test case enumerated by the [`Harness`] with data from the `setup` function /// /// See [`harness`][crate::harness] for more details