-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace difference library with similar
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
Showing
3 changed files
with
27 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters