Skip to content

Commit

Permalink
Print warning when we cannot print good diff
Browse files Browse the repository at this point in the history
  • Loading branch information
lemolatoon committed Sep 18, 2022
1 parent 27ee628 commit 629f740
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pretty_assertions/src/printer.rs
Expand Up @@ -85,6 +85,17 @@ pub(crate) fn write_lines<TWrite: fmt::Write>(
) -> fmt::Result {
let diff = ::diff::lines(left, right);

if left != right
&& diff
.iter()
.all(|result| matches!(result, ::diff::Result::Both(..)))
{
// If left and right are not the same and all changes are both, then we cannot print good diff.
// We should print the warning here instead.
writeln!(f, "Warning: values were different, but no line diff was generated. Do your values have the same line endings?")?;
return Ok(());
}

let mut changes = diff.into_iter().peekable();
let mut previous_deletion = LatentDeletion::default();

Expand Down Expand Up @@ -249,6 +260,19 @@ mod test {
assert_eq!(actual, expected);
}

/// Test diffing newlines from different conventions.
///
/// See: https://github.com/colin-kiegel/rust-pretty-assertions/issues/100
#[test]
fn different_newlines() {
// The below inputs caused an output with no visible diff
let left = "\r\n";
let right = "\n";
let expected = String::from("Warning: values were different, but no line diff was generated. Do your values have the same line endings?\n");

check_printer(write_lines, left, right, &expected);
}

#[test]
fn write_inline_diff_empty() {
let left = "";
Expand Down

0 comments on commit 629f740

Please sign in to comment.