From 0b77043a862dc5e3c094e4d54513a8be7268bf45 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 18 Apr 2024 13:53:11 -0500 Subject: [PATCH] fix(snap)!: Include a trailing newline in snapshots In using snapshots, I found the leading newline so good that I feel like we should have a trailing newline as well! Cherry pick 7fbcc9cc3349916188c12e7804d1399240827a52 (#272) --- crates/snapbox/src/data/runtime.rs | 8 +++++++- crates/snapbox/src/data/source.rs | 5 +++-- crates/snapbox/tests/testsuite/assert.rs | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/snapbox/src/data/runtime.rs b/crates/snapbox/src/data/runtime.rs index 2baaac36..2d8e0d8e 100644 --- a/crates/snapbox/src/data/runtime.rs +++ b/crates/snapbox/src/data/runtime.rs @@ -143,6 +143,9 @@ fn format_patch(patch: &str) -> String { buf.push('\n'); } buf.push_str(patch); + if is_multiline { + buf.push('\n'); + } lit_kind.write_end(&mut buf).unwrap(); if matches!(lit_kind, StrLitKind::Raw(_)) { buf.push(']'); @@ -359,7 +362,9 @@ mod tests { [r#" hello world -"#]"##]], + +"#] +"##]], ); let patch = format_patch(r"hello\tworld"); @@ -395,6 +400,7 @@ Patchwork { ), ], } + "#]], ); } diff --git a/crates/snapbox/src/data/source.rs b/crates/snapbox/src/data/source.rs index 9a193d89..b72ec41f 100644 --- a/crates/snapbox/src/data/source.rs +++ b/crates/snapbox/src/data/source.rs @@ -81,8 +81,9 @@ pub struct Inline { impl Inline { pub(crate) fn trimmed(&self) -> String { let mut data = self.data; - if data.contains('\n') && data.starts_with('\n') { - data = &data[1..] + if data.contains('\n') { + data = data.strip_prefix('\n').unwrap_or(data); + data = data.strip_suffix('\n').unwrap_or(data); } data.to_owned() } diff --git a/crates/snapbox/tests/testsuite/assert.rs b/crates/snapbox/tests/testsuite/assert.rs index 3af80f9d..6c34ad3c 100644 --- a/crates/snapbox/tests/testsuite/assert.rs +++ b/crates/snapbox/tests/testsuite/assert.rs @@ -17,6 +17,7 @@ line1 str![[r#" line1 line2 + "#]], ); }