From b2a65123d937902880ac6c7418807d47db602916 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Tue, 6 Feb 2024 23:21:27 +0000 Subject: [PATCH] Update `diff` test --- Cargo.lock | 74 +++++++++++++++++++++++++++++++++++++++++++-- crate/Cargo.toml | 1 + crate/tests/diff.rs | 38 +++++++++++++++++------ 3 files changed, 100 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db401fe..1245efc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,7 +24,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00ad3f3a942eee60335ab4342358c161ee296829e0d16ff42fc1d6cb07815467" dependencies = [ "anstyle", - "bstr", + "bstr 1.9.0", "doc-comment", "predicates", "predicates-core", @@ -44,6 +44,17 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata 0.1.10", +] + [[package]] name = "bstr" version = "1.9.0" @@ -51,7 +62,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c48f0051a4b4c5e0b6d365cd04af53aeaa209e3cc15ec2cdb69e73cc87fbd0dc" dependencies = [ "memchr", - "regex-automata", + "regex-automata 0.4.5", "serde", ] @@ -61,6 +72,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys", +] + [[package]] name = "difflib" version = "0.4.0" @@ -73,6 +96,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "equivalent" version = "1.0.1" @@ -120,6 +149,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" @@ -209,10 +244,16 @@ checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", - "regex-automata", + "regex-automata 0.4.5", "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-automata" version = "0.4.5" @@ -272,12 +313,33 @@ dependencies = [ "serde", ] +[[package]] +name = "similar" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32fea41aca09ee824cc9724996433064c89f7777e60762749a4170a14abbfa21" +dependencies = [ + "bstr 0.2.17", + "unicode-segmentation", +] + +[[package]] +name = "similar-asserts" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e041bb827d1bfca18f213411d51b665309f1afb37a04a5d1464530e13779fc0f" +dependencies = [ + "console", + "similar", +] + [[package]] name = "solana-lints" version = "0.1.0" dependencies = [ "assert_cmd", "predicates", + "similar-asserts", "tempfile", "toml", ] @@ -351,6 +413,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + [[package]] name = "wait-timeout" version = "0.2.0" diff --git a/crate/Cargo.toml b/crate/Cargo.toml index 7cbd623..7712f51 100644 --- a/crate/Cargo.toml +++ b/crate/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dev-dependencies] assert_cmd = "2.0.13" predicates = "3.1.0" +similar-asserts = "1.5" tempfile = "3.10.0" toml = "0.8.10" diff --git a/crate/tests/diff.rs b/crate/tests/diff.rs index 2eefa45..a280ce2 100644 --- a/crate/tests/diff.rs +++ b/crate/tests/diff.rs @@ -6,7 +6,7 @@ // lint's actual changes differ from the changes reflected in the lint's diff file, the test fails. use assert_cmd::prelude::*; -use predicates::prelude::*; +use similar_asserts::SimpleDiff; use std::{fs::read_to_string, path::Path}; struct Diff { @@ -55,16 +55,17 @@ fn diff() { .success(); for diff in DIFFS { - let contents = read_to_string( - Path::new(env!("CARGO_MANIFEST_DIR")) - .join("diffs") - .join(diff.solana_lint.to_owned() + ".diff"), - ) - .unwrap(); + let path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("diffs") + .join(diff.solana_lint.to_owned() + ".diff"); - std::process::Command::new("diff") + let contents = read_to_string(&path).unwrap(); + + let assert = std::process::Command::new("diff") .args([ "-r", + "-x", + "Cargo.lock", ".", &Path::new("..") .join("..") @@ -81,7 +82,24 @@ fn diff() { .join("programs") .join(diff.sealevel_attacks_programs_dir), ) - .assert() - .stdout(predicate::eq(contents.as_str())); + .assert(); + + let stdout = std::str::from_utf8(&assert.get_output().stdout).unwrap(); + + if contents != stdout { + if enabled("BLESS") { + std::fs::write(path, stdout).unwrap(); + } else { + panic!( + "{}", + SimpleDiff::from_str(&contents, stdout, "left", "right") + ); + } + } } } + +#[must_use] +pub fn enabled(key: &str) -> bool { + std::env::var(key).map_or(false, |value| value != "0") +}