Skip to content

Commit

Permalink
Remove assertion_line from stored snapshots (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Mar 4, 2022
1 parent e8f3f27 commit 5ab9f40
Show file tree
Hide file tree
Showing 56 changed files with 30 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to insta and cargo-insta are documented here.
## 1.13.0

- Fixed a bug where an extra newline was emitted following the snapshot header.
- `assertion_line` is no longer retained in snapshots. (#218)

## 1.12.0

Expand Down
4 changes: 3 additions & 1 deletion cargo-insta/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ impl SnapshotContainer {
for snapshot in self.snapshots.iter() {
match snapshot.op {
Operation::Accept => {
fs::rename(&self.snapshot_path, &self.target_path)?;
let snapshot = Snapshot::from_file(&self.snapshot_path)?;
snapshot.save(&self.target_path)?;
fs::remove_file(&self.snapshot_path)?;
}
Operation::Reject => {
fs::remove_file(&self.snapshot_path)?;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<'a> SnapshotAssertionContext<'a> {
if let Some(ref snapshot_file) = self.snapshot_file {
let mut new_path = snapshot_file.to_path_buf();
new_path.set_extension("snap.new");
new_snapshot.save(&new_path)?;
new_snapshot.save_new(&new_path)?;
if should_print {
elog!(
"{} {}",
Expand Down
27 changes: 25 additions & 2 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,41 @@ impl Snapshot {
&self.snapshot.0
}

pub(crate) fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>> {
fn save_with_metadata<P: AsRef<Path>>(
&self,
path: P,
md: &MetaData,
) -> Result<(), Box<dyn Error>> {
let path = path.as_ref();
if let Some(folder) = path.parent() {
fs::create_dir_all(&folder)?;
}
let mut f = fs::File::create(&path)?;
serde_yaml::to_writer(&mut f, &self.metadata)?;
serde_yaml::to_writer(&mut f, md)?;
f.write_all(b"---\n")?;
f.write_all(self.contents_str().as_bytes())?;
f.write_all(b"\n")?;
Ok(())
}

/// Saves the snapshot.
#[doc(hidden)]
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>> {
// we do not want to retain the assertion line on the metadata when storing
// as a regular snapshot.
if self.metadata.assertion_line.is_some() {
let mut metadata = self.metadata.clone();
metadata.assertion_line = None;
self.save_with_metadata(path, &metadata)
} else {
self.save_with_metadata(path, &self.metadata)
}
}

/// Same as `save` but also holds information only relevant for `.new` files.
pub(crate) fn save_new<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>> {
self.save_with_metadata(path, &self.metadata)
}
}

/// The contents of a Snapshot
Expand Down
1 change: 0 additions & 1 deletion src/snapshots/insta__test__embedded.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/test.rs
assertion_line: 3
expression: "\"Just a string\""
---
Just a string
1 change: 0 additions & 1 deletion tests/snapshots/snapshot_no_module_prepending.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_settings.rs
assertion_line: 74
expression: "vec![1, 2, 3]"
---
- 1
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__debug_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 8
expression: "vec![1, 2, 3]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__display.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 69
expression: td
---
TestDisplay struct
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__json_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 41
expression: "vec![1, 2, 3]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__nested__nested_module.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 54
expression: "\"aoeu\""
---
aoeu
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_debug_vector-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 14
expression: "vec![1, 2, 3, 4]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_debug_vector-3.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 15
expression: "vec![1, 2, 3, 4, 5]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_debug_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 13
expression: "vec![1, 2, 3]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_display-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 76
expression: "\"whatever\""
---
whatever
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_display.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 75
expression: td
---
TestDisplay struct
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_json_vector-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 47
expression: "vec![1, 2, 3, 4]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_json_vector-3.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 48
expression: "vec![1, 2, 3, 4, 5]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_json_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 46
expression: "vec![1, 2, 3]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_nested_closure.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 22
expression: "vec![1, 2, 3]"
---
[
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_yaml_vector-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 35
expression: "vec![1, 2, 3, 4]"
---
- 1
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_yaml_vector-3.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 36
expression: "vec![1, 2, 3, 4, 5]"
---
- 1
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__unnamed_yaml_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 34
expression: "vec![1, 2, 3]"
---
- 1
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_basic__yaml_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_basic.rs
assertion_line: 29
expression: "vec![1, 2, 3]"
---
- 1
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_bugs__crlf.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_bugs.rs
assertion_line: 3
expression: "\"foo\\r\\nbar\\r\\nbaz\""
---
foo
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_bugs__trailing_crlf.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_bugs.rs
assertion_line: 8
expression: "\"foo\\r\\nbar\\r\\nbaz\\r\\n\""
---
foo
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_glob.rs
assertion_line: 7
expression: "&contents"
input_file: tests/inputs/goodbye.txt
---
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_glob.rs
assertion_line: 7
expression: "&contents"
input_file: tests/inputs/hello.txt
---
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_glob.rs
assertion_line: 15
expression: "&contents"
input_file: tests/inputs/goodbye.txt
---
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_glob.rs
assertion_line: 15
expression: "&contents"
input_file: tests/inputs/hello.txt
---
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_inline__unnamed_single_line-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_inline.rs
assertion_line: 31
expression: "\"Testing-2\""
---
Testing-2
1 change: 0 additions & 1 deletion tests/snapshots/test_inline__unnamed_single_line.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_inline.rs
assertion_line: 30
expression: "\"Testing\""
---
Testing
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_inline.rs
assertion_line: 43
expression: "\"Testing-thread-2\""
---
Testing-thread-2
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_inline.rs
assertion_line: 42
expression: "\"Testing-thread\""
---
Testing-thread
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 22
expression: "Selector::parse(\".foo.bar\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar_alt.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 23
expression: "Selector::parse(\".foo[\\\"bar\\\"]\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar_deep.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 28
expression: "Selector::parse(\".foo.bar.**\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar_full_range.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 24
expression: "Selector::parse(\".foo.bar[]\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar_range.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 27
expression: "Selector::parse(\".foo.bar[10:20]\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar_range_from.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 26
expression: "Selector::parse(\".foo.bar[10:]\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__foo_bar_range_to.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 25
expression: "Selector::parse(\".foo.bar[:10]\").unwrap()"
---
Selector {
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__map_key_redaction.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 340
expression: foo
---
hm:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 307
expression: "vec![checkout]"
---
- _id: "[checkout_id]"
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 56
expression: "&User{id: 23,\n username: \"john_doe\".to_string(),\n email: Email(\"[email protected]\".to_string()),\n extra: \"\".to_string(),}"
---
id: "[id]"
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user_csv.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 85
expression: "&User{id: 44,\n username: \"julius_csv\".to_string(),\n email: Email(\"[email protected]\".to_string()),\n extra: \"\".to_string(),}"
---
id,username,email,extra
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user_json.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 123
expression: "&User{id: 9999,\n username: \"jason_doe\".to_string(),\n email: Email(\"[email protected]\".to_string()),\n extra: \"ssn goes here\".to_string(),}"
---
{
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user_json_flags.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 359
expression: "&User{id: 122,\n username: \"jason_doe\".to_string(),\n flags:\n vec![\"zzz\".into(), \"foo\".into(), \"aha\".into(),\n \"is_admin\".into()].into_iter().collect(),}"
---
{
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user_json_flags_alt.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 384
expression: "&User{id: 122,\n username: \"jason_doe\".to_string(),\n flags:\n MySet(vec![\"zzz\".into(), \"foo\".into(), \"aha\".into(),\n \"is_admin\".into()].into_iter().collect()),}"
---
{
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user_json_settings.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 140
expression: "&User{id: 122,\n username: \"jason_doe\".to_string(),\n email: Email(\"[email protected]\".to_string()),\n extra: \"ssn goes here\".to_string(),}"
---
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 161
expression: "&User{id: 1234,\n username: \"jason_doe\".to_string(),\n email: Email(\"[email protected]\".to_string()),\n extra: \"extra here\".to_string(),}"
---
{
Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/test_redaction__user_ron.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: tests/test_redaction.rs
assertion_line: 98
expression: "&User{id: 53,\n username: \"john_ron\".to_string(),\n email: Email(\"[email protected]\".to_string()),\n extra: \"\".to_string(),}"
---
User(
Expand Down
Loading

0 comments on commit 5ab9f40

Please sign in to comment.