Skip to content

Commit

Permalink
Fix snapshots within a nested crate
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jul 21, 2024
1 parent 747d6a7 commit 2986754
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions cargo-insta/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ impl SnapshotContainer {
}
} else {
// should only be one or this is weird
debug_assert!(self.snapshots.len() == 1);
for snapshot in self.snapshots.iter() {
match snapshot.op {
Operation::Accept => {
Expand Down
32 changes: 26 additions & 6 deletions cargo-insta/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ fn is_hidden(entry: &DirEntry) -> bool {

/// Finds all snapshots
pub(crate) fn find_snapshots<'a>(
root: &Path,
package_root: &Path,
extensions: &'a [&'a str],
flags: FindFlags,
) -> impl Iterator<Item = Result<SnapshotContainer, Box<dyn Error>>> + 'a {
make_snapshot_walker(root, extensions, flags)
make_snapshot_walker(package_root, extensions, flags)
.filter_map(|e| e.ok())
.filter_map(move |e| {
let fname = e.file_name().to_string_lossy();
Expand All @@ -55,17 +55,21 @@ pub(crate) fn find_snapshots<'a>(
})
}

/// Creates a walker for snapshots.
pub(crate) fn make_snapshot_walker(path: &Path, extensions: &[&str], flags: FindFlags) -> Walk {
let mut builder = WalkBuilder::new(path);
/// Creates a walker for snapshots within a package.
pub(crate) fn make_snapshot_walker(
package_root: &Path,
extensions: &[&str],
flags: FindFlags,
) -> Walk {
let mut builder = WalkBuilder::new(package_root);
builder.standard_filters(!flags.include_ignored);
if flags.include_hidden {
builder.hidden(false);
} else {
builder.filter_entry(|e| e.file_type().map_or(false, |x| x.is_file()) || !is_hidden(e));
}

let mut override_builder = OverrideBuilder::new(path);
let mut override_builder = OverrideBuilder::new(package_root);
override_builder
.add(".*.pending-snap")
.unwrap()
Expand All @@ -77,6 +81,22 @@ pub(crate) fn make_snapshot_walker(path: &Path, extensions: &[&str], flags: Find
}

builder.overrides(override_builder.build().unwrap());

let root_path = package_root.to_path_buf();

// Add a custom filter to skip interior crates; otherwise we get duplicate
// snapshots (https://github.com/mitsuhiko/insta/issues/396)
builder.filter_entry(move |entry| {
if entry.file_type().map_or(false, |ft| ft.is_dir()) {
let cargo_toml_path = entry.path().join("Cargo.toml");
if cargo_toml_path.exists() && entry.path() != root_path {
// Skip this directory if it contains a Cargo.toml and is not the root
return false;
}
}
true
});

builder.build()
}

Expand Down

0 comments on commit 2986754

Please sign in to comment.