From 16318d302e69ef3a486ecbcad8bf7aa484d009c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaya=20G=C3=B6kalp?= Date: Mon, 4 Nov 2024 20:13:35 -0800 Subject: [PATCH] chore: remove unmaintained `difference` in favor of `similar` (#6694) ## Description closes #5421. Removes unmaintained `difference` create used in swayfmt and uses `similar` instead. Security wise this is not an important problem as the dependency is only used as a dev-dependency. But the pr is a part of an effort cleaning `RUSTSEC` issues mainly for housekeeping reasons of our ever growing repo. --- Cargo.lock | 8 +------- swayfmt/Cargo.toml | 2 +- swayfmt/test_macros/src/lib.rs | 23 ++++++++++++++--------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ab07481f7a..95a6bab5a68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2071,12 +2071,6 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" -[[package]] -name = "difference" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" - [[package]] name = "digest" version = "0.9.0" @@ -7807,7 +7801,6 @@ name = "swayfmt" version = "0.66.4" dependencies = [ "anyhow", - "difference", "forc-tracing 0.66.4", "indoc", "paste", @@ -7815,6 +7808,7 @@ dependencies = [ "ropey", "serde", "serde_ignored", + "similar", "sway-ast", "sway-core", "sway-error", diff --git a/swayfmt/Cargo.toml b/swayfmt/Cargo.toml index 584bc0320ec..475cbe5d1b2 100644 --- a/swayfmt/Cargo.toml +++ b/swayfmt/Cargo.toml @@ -25,7 +25,7 @@ thiserror.workspace = true toml = { workspace = true, features = ["parse"] } [dev-dependencies] -difference = "2.0.0" paste = "1.0" prettydiff = "0.6" +similar = "2.0" test-macros = { path = "test_macros" } diff --git a/swayfmt/test_macros/src/lib.rs b/swayfmt/test_macros/src/lib.rs index faa93230e8e..8ec3decc2fd 100644 --- a/swayfmt/test_macros/src/lib.rs +++ b/swayfmt/test_macros/src/lib.rs @@ -119,18 +119,23 @@ macro_rules! fmt_test_inner { #[macro_export] macro_rules! assert_eq_pretty { ($got:expr, $expected:expr) => { - let got = &$got; - let expected = &$expected; + let got = &$got[..]; + let expected = &$expected[..]; + if got != expected { - use difference::{Changeset, Difference}; - let changeset = Changeset::new(expected, got, "\n"); - for diff in changeset.diffs { - match diff { - Difference::Same(s) => println!("{}", s), - Difference::Add(s) => println!("\x1b[32m+{}\x1b[0m", s), // Green color for additions - Difference::Rem(s) => println!("\x1b[31m-{}\x1b[0m", s), // Red color for removals + use similar::TextDiff; + + let diff = TextDiff::from_lines(expected, got); + for op in diff.ops() { + for change in diff.iter_changes(op) { + match change.tag() { + similar::ChangeTag::Equal => print!("{}", change), + similar::ChangeTag::Insert => print!("\x1b[32m+{}\x1b[0m", change), // Green for additions + similar::ChangeTag::Delete => print!("\x1b[31m-{}\x1b[0m", change), // Red for deletions + } } } + println!(); panic!("printed outputs differ!"); } };