Skip to content

Commit

Permalink
Handle new line characters as final characters (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty authored and mitsuhiko committed Jul 28, 2019
1 parent 9373b26 commit 52cc472
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cargo-insta/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub fn find_snapshots<'a>(
.filter_map(move |e| {
let fname = e.file_name().to_string_lossy();
if fname.ends_with(".new")
&& extensions.contains(&fname.rsplit('.').skip(1).next().unwrap_or(""))
&& extensions.contains(&fname.rsplit('.').nth(1).unwrap_or(""))
&& e.path()
.strip_prefix(&root)
.unwrap()
Expand Down
20 changes: 12 additions & 8 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,18 @@ fn get_inline_snapshot_value(frozen_value: &str) -> String {
}
}

if buf.ends_with('\n') {
buf.truncate(buf.len() - 1);
}

buf
buf.trim_end().to_string()
} else {
frozen_value.to_string()
frozen_value.trim_end().to_string()
}
}

#[test]
fn test_inline_snapshot_value_newline() {
// https://github.com/mitsuhiko/insta/issues/39
assert_eq!(get_inline_snapshot_value("\n"), "");
}

#[allow(clippy::too_many_arguments)]
pub fn assert_snapshot(
refval: ReferenceValue<'_>,
Expand Down Expand Up @@ -484,8 +486,10 @@ pub fn assert_snapshot(
};

// if the snapshot matches we're done.
if old.as_ref().map_or(false, |x| x.contents() == new_snapshot) {
return Ok(());
if let Some(ref x) = old {
if x.contents().trim_end() == new_snapshot.trim_end() {
return Ok(());
}
}

let new = Snapshot::from_components(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ fn test_unnamed_single_line() {
assert_snapshot_matches!("Testing-2");
}

#[test]
fn test_newline() {
// https://github.com/mitsuhiko/insta/issues/39
assert_snapshot_matches!("\n", @"");
}

#[test]
fn test_ron_inline() {
#[derive(Serialize)]
Expand Down

0 comments on commit 52cc472

Please sign in to comment.