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 7, 2024
1 parent a6c9903 commit b2a6512
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 13 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")
}

0 comments on commit b2a6512

Please sign in to comment.