Skip to content

Commit

Permalink
Ignore leading newlines on snapshot comparison (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Feb 19, 2024
1 parent 2a60c07 commit 0974fe8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to insta and cargo-insta are documented here.
- Added new alternative `match .. { ... }` syntax to redactions for better
`rustfmt` support. (#428)
- The `--package` parameter can be supplied multiple times now. (#427)
- Leading newlines in snapshots are now ignored to resolve issues with
inline snapshots that were never able to match. (#444)

## 1.34.0

Expand Down
8 changes: 7 additions & 1 deletion src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,13 @@ impl From<SnapshotContents> for String {

impl PartialEq for SnapshotContents {
fn eq(&self, other: &Self) -> bool {
self.0.trim_end() == other.0.trim_end()
self.0
.trim_start_matches(|x| x == '\r' || x == '\n')
.trim_end()
== other
.0
.trim_start_matches(|x| x == '\r' || x == '\n')
.trim_end()
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/test_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,15 @@ fn test_inline_test_in_loop() {
assert_snapshot!(i.to_string(), @"0");
}
}

#[test]
fn test_inline_snapshot_whitespace() {
assert_snapshot!("\n\nfoo\n\n bar\n\n", @r###"
foo
bar
"###);
}

0 comments on commit 0974fe8

Please sign in to comment.