Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for allow_duplicates! #346

Merged
merged 1 commit into from Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)*
})
}
}