Skip to content

Commit

Permalink
refactor(harness): Reuse more Assert logic
Browse files Browse the repository at this point in the history
Cherry pick 5871198 (assert-rs#276)
  • Loading branch information
epage committed May 16, 2024
1 parent c7d61c8 commit 591d3a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 97 deletions.
2 changes: 1 addition & 1 deletion crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use error::Result;
/// ```
#[derive(Clone, Debug)]
pub struct Assert {
action: Action,
pub(crate) action: Action,
action_var: Option<String>,
normalize_paths: bool,
substitutions: crate::Redactions,
Expand Down
106 changes: 10 additions & 96 deletions crates/snapbox/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
//! ```
use crate::data::DataFormat;
use crate::filter::{Filter as _, FilterNewlines};
use crate::Action;

use libtest_mimic::Trial;
Expand All @@ -49,7 +48,7 @@ pub struct Harness<S, T> {
overrides: Option<ignore::overrides::Override>,
setup: S,
test: T,
action: Action,
config: crate::Assert,
}

impl<S, T, I, E> Harness<S, T>
Expand All @@ -71,7 +70,7 @@ where
overrides: None,
setup,
test,
action: Action::Verify,
config: crate::Assert::new().action_env(crate::assert::DEFAULT_ACTION_ENV),
}
}

Expand All @@ -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
}

Expand All @@ -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();

Expand All @@ -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
Expand Down

0 comments on commit 591d3a4

Please sign in to comment.