Skip to content

Commit

Permalink
Test with pretty_assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 14, 2023
1 parent e0514c4 commit d617ee6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[workspace]

members = ["selene", "selene-lib"]

[workspace.package]
version = "0.25.0"
authors = ["Kampfkarren <kampfkarren@gmail.com>"]
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"
[workspace]

members = ["selene", "selene-lib"]

[workspace.package]
version = "0.25.0"
authors = ["Kampfkarren <kampfkarren@gmail.com>"]
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"
2 changes: 1 addition & 1 deletion selene-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
30 changes: 19 additions & 11 deletions selene-lib/src/lints/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
test_util::{get_standard_library, PrettyString},
StandardLibrary,
};
use similar::{ChangeTag, TextDiff};
use std::{
fs,
io::Write,
Expand Down Expand Up @@ -75,12 +76,13 @@ fn apply_diagnostics_fixes(code: &str, diagnostics: &Vec<Diagnostic>) -> 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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion selene-lib/tests/lints/empty_loop/empty_loop.fixed.diff
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@
-until true
+
-- comment here shouldn't break anything

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
end

-for _ = 1, 10 do


-
-end
-

-end

for _ in pairs({}) do
return "Should not warn"
end
Expand Down Expand Up @@ -70,4 +70,3 @@
-until true
+
-- comment here shouldn't break anything

0 comments on commit d617ee6

Please sign in to comment.