use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
#[test]
fn diff_reports_canonical_json_matches_and_hashes() {
let temp_dir = std::env::temp_dir().join(format!(
"biors-diff-test-{}-{}",
std::process::id(),
"canonical-json"
));
fs::create_dir_all(&temp_dir).expect("create temp dir");
let expected = temp_dir.join("expected.json");
let observed = temp_dir.join("observed.json");
fs::write(&expected, br#"{"b":2,"a":1}"#).expect("write expected");
fs::write(&observed, br#"{"a":1,"b":2}"#).expect("write observed");
let output = run_diff(&expected, &observed);
assert!(output.status.success());
assert!(output.stderr.is_empty());
let json: Value = serde_json::from_slice(&output.stdout).expect("stdout json");
assert_eq!(json["ok"], true);
assert_eq!(json["data"]["matches"], true);
assert!(json["data"]["expected_sha256"]
.as_str()
.unwrap()
.starts_with("sha256:"));
assert!(json["data"]["observed_sha256"]
.as_str()
.unwrap()
.starts_with("sha256:"));
fs::remove_dir_all(temp_dir).expect("remove temp dir");
}
#[test]
fn diff_reports_first_difference_for_mismatch() {
let temp_dir = std::env::temp_dir().join(format!(
"biors-diff-test-{}-{}",
std::process::id(),
"mismatch"
));
fs::create_dir_all(&temp_dir).expect("create temp dir");
let expected = temp_dir.join("expected.txt");
let observed = temp_dir.join("observed.txt");
fs::write(&expected, b"ACDE").expect("write expected");
fs::write(&observed, b"ACXE").expect("write observed");
let output = run_diff(&expected, &observed);
assert!(output.status.success());
assert!(output.stderr.is_empty());
let json: Value = serde_json::from_slice(&output.stdout).expect("stdout json");
assert_eq!(json["data"]["matches"], false);
assert_eq!(
json["data"]["content_diff"]["first_difference"]["byte_offset"],
2
);
fs::remove_dir_all(temp_dir).expect("remove temp dir");
}
fn run_diff(expected: &PathBuf, observed: &PathBuf) -> std::process::Output {
Command::new(env!("CARGO_BIN_EXE_biors"))
.arg("diff")
.arg(expected)
.arg(observed)
.output()
.expect("run biors diff")
}