From ffc3a9bf83502ccea512cc23ea984258f1347fe2 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 19 Feb 2024 21:49:33 +0100 Subject: [PATCH] Trim on diff (#445) --- CHANGELOG.md | 4 ++++ src/snapshot.rs | 17 +++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c8e6350..8ffc84cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to insta and cargo-insta are documented here. +## 1.35.1 + +- Fixed a bug with diffs showing bogus newlines. + ## 1.35.0 - Fixed a crash when a file named `.config` was in the root. diff --git a/src/snapshot.rs b/src/snapshot.rs index 4d90c78b..dead284b 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -436,7 +436,7 @@ impl Snapshot { /// The snapshot contents as a &str pub fn contents_str(&self) -> &str { - &self.snapshot.0 + self.snapshot.as_str() } fn serialize_snapshot(&self, md: &MetaData) -> String { @@ -510,6 +510,13 @@ impl SnapshotContents { SnapshotContents(get_inline_snapshot_value(value)) } + /// Returns the snapshot contents as string with surrounding whitespace removed. + pub fn as_str(&self) -> &str { + self.0 + .trim_start_matches(|x| x == '\r' || x == '\n') + .trim_end() + } + pub fn to_inline(&self, indentation: usize) -> String { let contents = &self.0; let mut out = String::new(); @@ -576,13 +583,7 @@ impl From for String { impl PartialEq for SnapshotContents { fn eq(&self, other: &Self) -> bool { - self.0 - .trim_start_matches(|x| x == '\r' || x == '\n') - .trim_end() - == other - .0 - .trim_start_matches(|x| x == '\r' || x == '\n') - .trim_end() + self.as_str() == other.as_str() } }