Skip to content

Commit

Permalink
Update diff test
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Feb 6, 2024
1 parent a6c9903 commit 781fba1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 14 deletions.
74 changes: 71 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
38 changes: 28 additions & 10 deletions crate/tests/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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("..")
Expand All @@ -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")
}
2 changes: 1 addition & 1 deletion lints/arbitrary_cpi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitraryCpi {
.basic_blocks
.iter_enumerated()
.map(|(block_id, block)| (block_id, &block.terminator));
for (_idx, (block_id, terminator)) in terminators.enumerate() {
for (block_id, terminator) in terminators {
if_chain! {
if let t = terminator.as_ref().unwrap();
if let TerminatorKind::Call {
Expand Down

0 comments on commit 781fba1

Please sign in to comment.