Skip to content

Commit

Permalink
Add support for allow_duplicates!
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Feb 12, 2023
1 parent 4884685 commit 9569708
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 175 deletions.
7 changes: 5 additions & 2 deletions cargo-insta/src/cli.rs
Expand Up @@ -8,7 +8,7 @@ use std::{io, process};
use console::{set_colors_enabled, style, Key, Term};
use insta::Snapshot;
use insta::_cargo_insta_support::{
is_ci, print_snapshot, print_snapshot_diff, SnapshotUpdate, TestRunner, ToolConfig,
is_ci, print_snapshot, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig,
UnreferencedSnapshots,
};
use serde::Serialize;
Expand Down Expand Up @@ -246,7 +246,10 @@ fn query_snapshot(
pkg_version,
);

print_snapshot_diff(workspace_root, new, old, snapshot_file, line, *show_info);
let mut printer = SnapshotPrinter::new(workspace_root, old, new);
printer.set_snapshot_file(snapshot_file);
printer.set_line(line);
printer.set_show_info(*show_info);

println!();
println!(
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -295,7 +295,7 @@ pub mod _cargo_insta_support {
Error as ToolConfigError, OutputBehavior, SnapshotUpdate, TestRunner, ToolConfig,
UnreferencedSnapshots,
},
output::{print_snapshot, print_snapshot_diff},
output::{print_snapshot, SnapshotPrinter},
snapshot::PendingInlineSnapshot,
snapshot::SnapshotContents,
utils::is_ci,
Expand All @@ -311,7 +311,7 @@ pub use crate::redaction::{dynamic_redaction, sorted_redaction};
pub mod _macro_support {
pub use crate::content::Content;
pub use crate::env::get_cargo_workspace;
pub use crate::runtime::{assert_snapshot, AutoName, ReferenceValue};
pub use crate::runtime::{assert_snapshot, with_allow_duplicates, AutoName, ReferenceValue};

#[cfg(feature = "serde")]
pub use crate::serialization::{serialize_value, SerializationFormat, SnapshotLocation};
Expand Down
23 changes: 23 additions & 0 deletions src/macros.rs
Expand Up @@ -554,3 +554,26 @@ macro_rules! glob {
$crate::_macro_support::glob_exec(env!("CARGO_MANIFEST_DIR"), &base, $glob, $closure);
}};
}

/// Utility macro to create a multi-snapshot run where all snapshots match.
///
/// Within this block, insta will allow an assertion to be run twice (even inline) without
/// generating another snapshot. Instead it will assert that snapshot expressions visited
/// more than once are matching.
///
/// ```rust
/// insta::allow_duplicates! {
/// for x in (0..10).step_by(2) {
/// let is_even = x % 2 == 0;
/// insta::assert_debug_snapshot!(is_even, @"true");
/// }
/// }
/// ```
#[macro_export]
macro_rules! allow_duplicates {
($($x:tt)*) => {
$crate::_macro_support::with_allow_duplicates(|| {
$($x)*
})
}
}

0 comments on commit 9569708

Please sign in to comment.