#![allow(deprecated)]
use super::test_repo::TestRepo;
#[test]
fn given_missing_base_sha_when_check_then_tool_error() {
let repo = TestRepo::new();
repo.write_file("src/lib.rs", "pub fn new_code() {}\n");
let head_sha = repo.commit("add new code");
let result =
repo.run_check_with_invalid_base(&head_sha, "0000000000000000000000000000000000000000");
result.assert_exit_code(1);
assert!(
result.stderr.contains("git diff failed")
|| result.stderr.contains("error")
|| result.stderr.contains("Error"),
"stderr should contain error message, got: {}",
result.stderr
);
}
#[test]
fn given_invalid_ref_name_when_check_then_tool_error() {
let repo = TestRepo::new();
repo.write_file("src/lib.rs", "pub fn f() {}\n");
let head_sha = repo.commit("add code");
let result = repo.run_check_with_invalid_base(&head_sha, "this-branch-does-not-exist");
result.assert_exit_code(1);
}
#[test]
fn given_invalid_head_sha_when_check_then_tool_error() {
let repo = TestRepo::new();
let out_path = repo.path().join("artifacts/diffguard/report.json");
let output = assert_cmd::Command::cargo_bin("diffguard")
.expect("binary")
.current_dir(repo.path())
.arg("check")
.arg("--base")
.arg(&repo.base_sha)
.arg("--head")
.arg("0000000000000000000000000000000000000000")
.arg("--out")
.arg(&out_path)
.output()
.expect("run command");
let exit_code = output.status.code().unwrap_or(-1);
assert_eq!(exit_code, 1, "Expected exit code 1 for invalid head SHA");
}
#[test]
fn given_truncated_sha_when_check_then_error_or_resolves() {
let repo = TestRepo::new();
repo.write_file("src/lib.rs", "pub fn f() {}\n");
let head_sha = repo.commit("add code");
let result = repo.run_check_with_invalid_base(&head_sha, "0000");
assert!(
result.exit_code == 1,
"Expected exit code 1 for unresolvable short SHA, got {}",
result.exit_code
);
}
#[test]
fn given_valid_short_sha_when_check_then_works() {
let repo = TestRepo::new();
repo.write_file("src/lib.rs", "pub fn clean() -> Option<u32> { Some(1) }\n");
let head_sha = repo.commit("add clean code");
let short_base = &repo.base_sha[..7.min(repo.base_sha.len())];
let short_head = &head_sha[..7.min(head_sha.len())];
let out_path = repo.path().join("artifacts/diffguard/report.json");
let output = assert_cmd::Command::cargo_bin("diffguard")
.expect("binary")
.current_dir(repo.path())
.arg("check")
.arg("--base")
.arg(short_base)
.arg("--head")
.arg(short_head)
.arg("--out")
.arg(&out_path)
.output()
.expect("run command");
let exit_code = output.status.code().unwrap_or(-1);
assert_eq!(
exit_code, 0,
"Expected exit code 0 for valid short SHAs, got {}",
exit_code
);
}