Skip to content

Commit

Permalink
Do not panic on outdated inline snapshots (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Mar 20, 2022
1 parent 943ff02 commit d631caf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
21 changes: 12 additions & 9 deletions cargo-insta/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,18 @@ impl SnapshotContainer {
pending_vec.sort_by_key(|pending| pending.line);
for (id, pending) in pending_vec.into_iter().enumerate() {
if let Some(new) = pending.new {
snapshots.push(PendingSnapshot {
id,
old: pending.old,
new,
op: Operation::Skip,
line: Some(pending.line),
});
patcher.add_snapshot_macro(pending.line as usize);
have_new = true;
if patcher.add_snapshot_macro(pending.line as usize) {
snapshots.push(PendingSnapshot {
id,
old: pending.old,
new,
op: Operation::Skip,
line: Some(pending.line),
});
have_new = true;
} else {
// this is an outdated snapshot and the file changed.
}
}
}
Some(patcher)
Expand Down
7 changes: 4 additions & 3 deletions cargo-insta/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ impl FilePatcher {
Ok(())
}

pub fn add_snapshot_macro(&mut self, line: usize) {
pub fn add_snapshot_macro(&mut self, line: usize) -> bool {
match self.find_snapshot_macro(line) {
Some(snapshot) => {
assert!(self
.inline_snapshots
.last()
.map_or(true, |x| x.end.0 <= line));
self.inline_snapshots.push(snapshot)
self.inline_snapshots.push(snapshot);
true
}
None => panic!("Could not find snapshot in line {}", line),
None => false,
}
}

Expand Down

0 comments on commit d631caf

Please sign in to comment.