From 517d555f6ea52e64d95885e8640b8550b4fdf9fb Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Tue, 14 May 2024 23:47:40 -0700 Subject: [PATCH] Be robust to removing pending snapshots during review (#484) Closes #380 --- cargo-insta/src/container.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/cargo-insta/src/container.rs b/cargo-insta/src/container.rs index 95fc20d6..062b79c7 100644 --- a/cargo-insta/src/container.rs +++ b/cargo-insta/src/container.rs @@ -164,6 +164,18 @@ impl SnapshotContainer { } pub(crate) fn commit(&mut self) -> Result<(), Box> { + // 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; @@ -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 @@ -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 => {} }