Skip to content

Commit ffc3a9b

Browse files
authored
Trim on diff (#445)
1 parent 0d9c6c2 commit ffc3a9b

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to insta and cargo-insta are documented here.
44

5+
## 1.35.1
6+
7+
- Fixed a bug with diffs showing bogus newlines.
8+
59
## 1.35.0
610

711
- Fixed a crash when a file named `.config` was in the root.

src/snapshot.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl Snapshot {
436436

437437
/// The snapshot contents as a &str
438438
pub fn contents_str(&self) -> &str {
439-
&self.snapshot.0
439+
self.snapshot.as_str()
440440
}
441441

442442
fn serialize_snapshot(&self, md: &MetaData) -> String {
@@ -510,6 +510,13 @@ impl SnapshotContents {
510510
SnapshotContents(get_inline_snapshot_value(value))
511511
}
512512

513+
/// Returns the snapshot contents as string with surrounding whitespace removed.
514+
pub fn as_str(&self) -> &str {
515+
self.0
516+
.trim_start_matches(|x| x == '\r' || x == '\n')
517+
.trim_end()
518+
}
519+
513520
pub fn to_inline(&self, indentation: usize) -> String {
514521
let contents = &self.0;
515522
let mut out = String::new();
@@ -576,13 +583,7 @@ impl From<SnapshotContents> for String {
576583

577584
impl PartialEq for SnapshotContents {
578585
fn eq(&self, other: &Self) -> bool {
579-
self.0
580-
.trim_start_matches(|x| x == '\r' || x == '\n')
581-
.trim_end()
582-
== other
583-
.0
584-
.trim_start_matches(|x| x == '\r' || x == '\n')
585-
.trim_end()
586+
self.as_str() == other.as_str()
586587
}
587588
}
588589

0 commit comments

Comments
 (0)