Skip to content

Commit

Permalink
Add require_full_match option (#448)
Browse files Browse the repository at this point in the history
May need some refining. Closes #351, but with an additional option, since `force_update` seems like a bad term for this
  • Loading branch information
max-sixty committed Mar 2, 2024
1 parent 189290f commit 686de75
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cargo-insta/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,9 @@ fn prepare_test_runner<'snapshot_ref>(
if cmd.require_full_match {
proc.env("INSTA_REQUIRE_FULL_MATCH", "1");
}
if cmd.require_full_match {
proc.env("INSTA_REQUIRE_FULL_MATCH", "1");
}
let glob_filter =
cmd.glob_filter
.iter()
Expand Down
14 changes: 14 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl std::error::Error for Error {
pub struct ToolConfig {
force_update_snapshots: bool,
force_pass: bool,
require_full_match: bool,
output: OutputBehavior,
snapshot_update: SnapshotUpdate,
#[cfg(feature = "glob")]
Expand Down Expand Up @@ -157,6 +158,14 @@ impl ToolConfig {
Ok("1") => true,
_ => return Err(Error::Env("INSTA_FORCE_UPDATE")),
},
require_full_match: match env::var("INSTA_REQUIRE_FULL_MATCH").as_deref() {
Err(_) | Ok("") => resolve(&cfg, &["behavior", "require_full_match"])
.and_then(|x| x.as_bool())
.unwrap_or(false),
Ok("0") => false,
Ok("1") => true,
_ => return Err(Error::Env("INSTA_REQUIRE_FULL_MATCH")),
},
force_pass: match env::var("INSTA_FORCE_PASS").as_deref() {
Err(_) | Ok("") => resolve(&cfg, &["behavior", "force_pass"])
.and_then(|x| x.as_bool())
Expand Down Expand Up @@ -255,6 +264,11 @@ impl ToolConfig {
self.force_update_snapshots
}

/// Should we fail if metadata doesn't match?
pub fn require_full_match(&self) -> bool {
self.require_full_match
}

/// Is insta instructed to fail in tests?
pub fn force_pass(&self) -> bool {
self.force_pass
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@
//! behavior:
//! # also set by INSTA_FORCE_UPDATE
//! force_update: true/false
//! # also set by INSTA_REQUIRE_FULL_MATCH
//! require_full_match: true/false
//! # also set by INSTA_FORCE_PASS
//! force_pass: true/false
//! # also set by INSTA_OUTPUT
Expand Down
15 changes: 13 additions & 2 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,19 @@ pub fn assert_snapshot(
}
});

// pass if the snapshots are missing
if ctx.old_snapshot.as_ref().map(|x| x.contents()) == Some(new_snapshot.contents()) {
let pass = ctx
.old_snapshot
.as_ref()
.map(|x| {
if tool_config.require_full_match() {
x.matches_fully(&new_snapshot)
} else {
x.matches(&new_snapshot)
}
})
.unwrap_or(false);

if pass {
ctx.cleanup_passing()?;

if tool_config.force_update_snapshots() {
Expand Down
12 changes: 11 additions & 1 deletion src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl PendingInlineSnapshot {
}

/// Snapshot metadata information.
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct MetaData {
/// The source file (relative to workspace root).
pub(crate) source: Option<String>,
Expand Down Expand Up @@ -434,6 +434,16 @@ impl Snapshot {
&self.snapshot
}

/// Snapshot contents match another snapshot's.
pub fn matches(&self, other: &Snapshot) -> bool {
self.contents() == other.contents()
}

/// Snapshot contents _and_ metadata match another snapshot's.
pub fn matches_fully(&self, other: &Snapshot) -> bool {
self.matches(other) && self.metadata == other.metadata
}

/// The snapshot contents as a &str
pub fn contents_str(&self) -> &str {
self.snapshot.as_str()
Expand Down

0 comments on commit 686de75

Please sign in to comment.