diff --git a/pretty_assertions/src/printer.rs b/pretty_assertions/src/printer.rs index 24c25f0..550920b 100644 --- a/pretty_assertions/src/printer.rs +++ b/pretty_assertions/src/printer.rs @@ -85,6 +85,17 @@ pub(crate) fn write_lines( ) -> 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(); @@ -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 = "";