Skip to content

Commit

Permalink
refactor(harness): Reuse more Assert logic
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 19, 2024
1 parent 0791d65 commit 5871198
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 94 deletions.
2 changes: 1 addition & 1 deletion crates/snapbox/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::Action;
/// ```
#[derive(Clone, Debug)]
pub struct Assert {
action: Action,
pub(crate) action: Action,
action_var: Option<String>,
normalize_paths: bool,
substitutions: crate::Substitutions,
Expand Down
110 changes: 17 additions & 93 deletions crates/snapbox/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! }
//! ```
use crate::data::{DataFormat, NormalizeNewlines};
use crate::data::DataFormat;
use crate::Action;

use libtest_mimic::Trial;
Expand All @@ -49,7 +49,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 +71,7 @@ where
overrides: None,
setup,
test,
action: Action::Verify,
config: crate::Assert::new().action_env(crate::DEFAULT_ACTION_ENV),
}
}

Expand All @@ -89,14 +89,19 @@ 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
}

/// Customize the assertion behavior
pub fn with_assert(mut self, config: crate::Assert) -> Self {
self.config = config;
self
}

Expand All @@ -118,26 +123,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, case.format);
let actual = (test)(&case.fixture)?;
let actual = actual.to_string();
let mut actual = crate::Data::text(actual).normalize(NormalizeNewlines);
if let Some(format) = case.format {
actual = actual.coerce_to(format);
}
#[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 @@ -146,83 +147,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::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::Result<()> {
actual.write_to_path(expected_path)?;
Ok(())
}

fn try_verify(
&self,
expected_path: &std::path::Path,
actual: crate::Data,
) -> crate::Result<()> {
let expected = crate::Data::read_from(expected_path, Some(DataFormat::Text))
.normalize(NormalizeNewlines);

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 5871198

Please sign in to comment.