diff --git a/Cargo.lock b/Cargo.lock index ab5be762..6785f076 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -966,7 +966,6 @@ version = "0.25.0" dependencies = [ "codespan", "codespan-reporting", - "diff", "full_moon", "id-arena", "if_chain", @@ -979,6 +978,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", + "similar", "termcolor", "toml", ] @@ -1051,6 +1051,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "similar" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aeaf503862c419d66959f5d7ca015337d864e9c49485d771b732e2a20453597" + [[package]] name = "smallvec" version = "1.10.0" diff --git a/Cargo.toml b/Cargo.toml index df846898..97581cf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,18 +1,18 @@ -[workspace] - -members = ["selene", "selene-lib"] - -[workspace.package] -version = "0.25.0" -authors = ["Kampfkarren "] -edition = "2021" -homepage = "https://kampfkarren.github.io/selene/" -license = "MPL-2.0" -repository = "https://github.com/Kampfkarren/selene" - -[workspace.dependencies] -full_moon = "0.18.0" -toml = "0.7.2" - -# Do not update this without confirming profiling uses the same version of tracy-client as selene -profiling = "1.0.7" \ No newline at end of file +[workspace] + +members = ["selene", "selene-lib"] + +[workspace.package] +version = "0.25.0" +authors = ["Kampfkarren "] +edition = "2021" +homepage = "https://kampfkarren.github.io/selene/" +license = "MPL-2.0" +repository = "https://github.com/Kampfkarren/selene" + +[workspace.dependencies] +full_moon = "0.18.0" +toml = "0.7.2" + +# Do not update this without confirming profiling uses the same version of tracy-client as selene +profiling = "1.0.7" diff --git a/selene-lib/Cargo.toml b/selene-lib/Cargo.toml index 44c58138..9811f638 100644 --- a/selene-lib/Cargo.toml +++ b/selene-lib/Cargo.toml @@ -12,7 +12,6 @@ edition.workspace = true [dependencies] codespan = "0.11" codespan-reporting = "0.11" -diff = "0.1.13" full_moon.workspace = true id-arena = "2.2" if_chain = "1.0.2" @@ -23,6 +22,7 @@ profiling.workspace = true regex = "1.7.1" serde = "1.0.152" serde_yaml = "0.9.16" +similar = "2.3.0" toml.workspace = true [dev-dependencies] diff --git a/selene-lib/src/lints/test_util.rs b/selene-lib/src/lints/test_util.rs index acab0a8d..e55bdd8b 100644 --- a/selene-lib/src/lints/test_util.rs +++ b/selene-lib/src/lints/test_util.rs @@ -3,6 +3,7 @@ use crate::{ test_util::{get_standard_library, PrettyString}, StandardLibrary, }; +use similar::{ChangeTag, TextDiff}; use std::{ fs, io::Write, @@ -75,12 +76,13 @@ fn apply_diagnostics_fixes(code: &str, diagnostics: &Vec) -> String fn generate_diff(source1: &str, source2: &str) -> String { let mut result = String::new(); - for line in diff::lines(source1, source2) { - match line { - diff::Result::Left(l) => result.push_str(&format!("-{}\n", l)), - diff::Result::Both(l, _) => result.push_str(&format!(" {}\n", l)), - diff::Result::Right(r) => result.push_str(&format!("+{}\n", r)), - } + for change in TextDiff::from_lines(source1, source2).iter_all_changes() { + let sign = match change.tag() { + ChangeTag::Delete => "-", + ChangeTag::Insert => "+", + ChangeTag::Equal => " ", + }; + result.push_str(&format!("{}{}", sign, change.value())); } result @@ -132,11 +134,17 @@ pub fn test_lint_config_with_output< let mut output = termcolor::NoColor::new(Vec::new()); let fixed_lua_code = apply_diagnostics_fixes(&lua_source, &diagnostics); - if let Err(e) = fs::write( - &path_base.with_file_name(format!("{}.fixed.diff", test_name)), - generate_diff(lua_source.as_str(), fixed_lua_code.as_str()), - ) { - eprintln!("Failed to write to file: {}", e); + let fixed_diff = generate_diff(&lua_source, &fixed_lua_code); + let diff_output_path = path_base.with_extension("fixed.diff"); + + if let Ok(expected) = fs::read_to_string(&diff_output_path) { + pretty_assertions::assert_eq!(PrettyString(&expected), PrettyString(&fixed_diff)); + } else { + let mut output_file = + fs::File::create(diff_output_path).expect("couldn't create fixed.diff output file"); + output_file + .write_all(fixed_diff.as_bytes()) + .expect("couldn't write fixed.diff to output file"); } for diagnostic in diagnostics diff --git a/selene-lib/tests/lints/empty_loop/empty_loop.fixed.diff b/selene-lib/tests/lints/empty_loop/empty_loop.fixed.diff index f190ae84..0edc5d4a 100644 --- a/selene-lib/tests/lints/empty_loop/empty_loop.fixed.diff +++ b/selene-lib/tests/lints/empty_loop/empty_loop.fixed.diff @@ -71,4 +71,3 @@ -until true + -- comment here shouldn't break anything - diff --git a/selene-lib/tests/lints/empty_loop/empty_loop_comments.fixed.diff b/selene-lib/tests/lints/empty_loop/empty_loop_comments.fixed.diff index 6ddbff5b..498d8198 100644 --- a/selene-lib/tests/lints/empty_loop/empty_loop_comments.fixed.diff +++ b/selene-lib/tests/lints/empty_loop/empty_loop_comments.fixed.diff @@ -17,11 +17,11 @@ end -for _ = 1, 10 do - - - --end - + +-end + for _ in pairs({}) do return "Should not warn" end @@ -70,4 +70,3 @@ -until true + -- comment here shouldn't break anything -