Skip to content

Commit

Permalink
Be robust to removing pending snapshots during review (#484)
Browse files Browse the repository at this point in the history
Closes #380
  • Loading branch information
max-sixty committed May 15, 2024
1 parent bc73039 commit 517d555
Showing 1 changed file with 15 additions and 8 deletions.
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

0 comments on commit 517d555

Please sign in to comment.