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

Be robust to removing pending snapshots during review #484

Merged
merged 2 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions cargo-insta/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ impl SnapshotContainer {
}

pub(crate) fn commit(&mut self) -> Result<(), Box<dyn Error>> {
// Try removing the snapshot file. If it fails, it's
// likely because it another process removed it; which
// is fine — print a message and continue.
let try_removing_snapshot = |p| {
fs::remove_file(p).unwrap_or_else(|_| {
eprintln!(
"Pending snapshot file at {:?} couldn't be removed. It was likely removed by another process.",
p
);
});
};

if let Some(ref mut patcher) = self.patcher {
let mut new_pending = vec![];
let mut did_accept = false;
Expand Down Expand Up @@ -193,8 +205,7 @@ impl SnapshotContainer {
if did_skip {
PendingInlineSnapshot::save_batch(&self.snapshot_path, &new_pending)?;
} else {
fs::remove_file(&self.snapshot_path)
.map_err(|e| ContentError::FileIo(e, self.snapshot_path.to_path_buf()))?;
try_removing_snapshot(&self.snapshot_path);
}
} else {
// should only be one or this is weird
Expand All @@ -203,14 +214,10 @@ impl SnapshotContainer {
Operation::Accept => {
let snapshot = Snapshot::from_file(&self.snapshot_path)?;
snapshot.save(&self.target_path)?;
fs::remove_file(&self.snapshot_path).map_err(|e| {
ContentError::FileIo(e, self.snapshot_path.to_path_buf())
})?;
try_removing_snapshot(&self.snapshot_path);
}
Operation::Reject => {
fs::remove_file(&self.snapshot_path).map_err(|e| {
ContentError::FileIo(e, self.snapshot_path.to_path_buf())
})?;
try_removing_snapshot(&self.snapshot_path);
}
Operation::Skip => {}
}
Expand Down
Loading