Skip to content

Commit

Permalink
Replace difference library with similar
Browse files Browse the repository at this point in the history
Replace difference library with similar because difference is no longer
maintained. See advisory: https://rustsec.org/advisories/RUSTSEC-2020-0095

Updated related tests due to slightly different diffing output.
  • Loading branch information
Lee Bradley committed Feb 26, 2022
1 parent fc1d376 commit 309a5bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ httparse = "1.3.3"
regex = "1.0.5"
lazy_static = "1.1.0"
serde_json = "1.0.17"
difference = "2.0"
similar = "2.1"

colored = { version = "2.0.0", optional = true }
log = "0.4.6"
assert-json-diff = "2.0.0"
Expand Down
55 changes: 23 additions & 32 deletions src/diff.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,60 @@
#[cfg(feature = "color")]
use colored::*;
use difference::{Changeset, Difference};
use similar::{Change, ChangeTag, TextDiff};

pub fn compare(expected: &str, actual: &str) -> String {
let mut result = String::new();

let clean_expected = expected.replace("\r\n", "\n");
let clean_actual = actual.replace("\r\n", "\n");

let Changeset { diffs, .. } = Changeset::new(&clean_expected, &clean_actual, "\n");

for i in 0..diffs.len() {
match diffs[i] {
Difference::Same(ref x) => {
let mut last: Option<Change<_>> = None;
for diff in TextDiff::from_lines(&clean_expected, &clean_actual).iter_all_changes() {
let x = diff.value();
match diff.tag() {
ChangeTag::Equal => {
result.push_str(x);
result.push('\n');
}
Difference::Add(ref x) => {
if let Difference::Rem(ref y) = diffs[i - 1] {
let Changeset { diffs, .. } = Changeset::new(y, x, " ");
for (i, change) in diffs.iter().enumerate() {
match change {
Difference::Same(ref z) => {
ChangeTag::Insert => {
if let Some((y, ChangeTag::Delete)) = last.map(|d| (d.value(), d.tag())) {
for change in TextDiff::from_words(y, x).iter_all_changes() {
match change.tag() {
ChangeTag::Equal => {
let z = change.value();
#[cfg(feature = "color")]
result.push_str(&z.green().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&z);

if i < diffs.len() - 1 {
result.push(' ');
}
result.push_str(z);
}
Difference::Add(ref z) => {
ChangeTag::Insert => {
let z = change.value();
#[cfg(feature = "color")]
result.push_str(&z.white().on_green().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&z);

if i < diffs.len() - 1 {
result.push(' ');
}
result.push_str(z);
}
_ => (),
}
}
result.push('\n');
} else {
#[cfg(feature = "color")]
result.push_str(&x.bright_green().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&x);

result.push('\n');
result.push_str(x);
}
}
Difference::Rem(ref x) => {
ChangeTag::Delete => {
#[cfg(feature = "color")]
result.push_str(&x.red().to_string());
#[cfg(not(feature = "color"))]
result.push_str(&x);

result.push('\n');
result.push_str(x);
}
}

last = Some(diff);
}

result.push('\n');

result
}
4 changes: 2 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ fn test_assert_panics_with_too_many_requests() {

#[test]
#[should_panic(
expected = "\n> Expected 1 request(s) to:\n\r\nGET /hello\r\n\n...but received 0\n\n> The last unmatched request was:\n\r\nGET /bye\r\n\n> Difference:\n\n\u{1b}[31mGET /hello\u{1b}[0m\n\u{1b}[32mGET\u{1b}[0m \u{1b}[42;37m/bye\u{1b}[0m\n\n\n"
expected = "\n> Expected 1 request(s) to:\n\r\nGET /hello\r\n\n...but received 0\n\n> The last unmatched request was:\n\r\nGET /bye\r\n\n> Difference:\n\n\u{1b}[31mGET /hello\n\u{1b}[0m\u{1b}[32mGET\u{1b}[0m\u{1b}[32m \u{1b}[0m\u{1b}[42;37m/bye\u{1b}[0m\u{1b}[32m\n\u{1b}[0m\n\n"
)]
#[cfg(feature = "color")]
fn test_assert_with_last_unmatched_request() {
Expand All @@ -1154,7 +1154,7 @@ fn test_assert_with_last_unmatched_request() {

#[test]
#[should_panic(
expected = "\n> Expected 1 request(s) to:\n\r\nGET /hello\r\n\n...but received 0\n\n> The last unmatched request was:\n\r\nGET /bye\r\nauthorization: 1234\r\naccept: text\r\n\n> Difference:\n\n\u{1b}[31mGET /hello\u{1b}[0m\n\u{1b}[32mGET\u{1b}[0m \u{1b}[42;37m/bye\nauthorization: 1234\naccept: text\u{1b}[0m\n\n\n"
expected = "\n> Expected 1 request(s) to:\n\r\nGET /hello\r\n\n...but received 0\n\n> The last unmatched request was:\n\r\nGET /bye\r\nauthorization: 1234\r\naccept: text\r\n\n> Difference:\n\n\u{1b}[31mGET /hello\n\u{1b}[0m\u{1b}[32mGET\u{1b}[0m\u{1b}[32m \u{1b}[0m\u{1b}[42;37m/bye\u{1b}[0m\u{1b}[32m\n\u{1b}[0m\u{1b}[92mauthorization: 1234\n\u{1b}[0m\u{1b}[92maccept: text\n\u{1b}[0m\n\n"
)]
#[cfg(feature = "color")]
fn test_assert_with_last_unmatched_request_and_headers() {
Expand Down

0 comments on commit 309a5bd

Please sign in to comment.