use super::test_repo::TestRepo;
#[test]
fn given_unwrap_only_in_base_when_check_then_pass() {
let repo = TestRepo::with_initial_content(&[(
"src/lib.rs",
"pub fn legacy() -> u32 { Some(1).unwrap() }\n",
)]);
repo.write_file(
"src/lib.rs",
r#"pub fn legacy() -> u32 { Some(1).unwrap() }
pub fn new_safe_function() -> Option<u32> { Some(42) }
"#,
);
let head_sha = repo.commit("add new safe function");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
assert_eq!(receipt.verdict_status(), Some("pass"));
}
#[test]
fn given_unwrap_in_base_when_other_file_changed_then_pass() {
let repo = TestRepo::with_initial_content(&[
(
"src/lib.rs",
"pub fn legacy() -> u32 { Some(1).unwrap() }\n",
),
("src/other.rs", "pub fn other() {}\n"),
]);
repo.write_file(
"src/other.rs",
"pub fn other() { println!(\"modified\"); }\n",
);
let head_sha = repo.commit("modify other file");
let result = repo.run_check(&head_sha);
let receipt = result.parse_receipt();
assert!(!receipt.has_finding_with_rule("rust.no_unwrap"));
}
#[test]
fn given_no_changes_when_check_then_pass() {
let repo = TestRepo::new();
let result = repo.run_check(&repo.base_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_unwrap_in_context_when_nearby_change_then_not_flagged() {
let repo = TestRepo::with_initial_content(&[(
"src/lib.rs",
r#"pub fn before() {}
pub fn risky() -> u32 {
Some(1).unwrap()
}
pub fn after() {}
"#,
)]);
repo.write_file(
"src/lib.rs",
r#"pub fn before() {}
pub fn new_before() {}
pub fn risky() -> u32 {
Some(1).unwrap()
}
pub fn new_after() {}
pub fn after() {}
"#,
);
let head_sha = repo.commit("add functions around risky");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert!(!receipt.has_finding_with_rule("rust.no_unwrap"));
}
#[test]
fn given_deletion_only_when_check_then_pass() {
let repo = TestRepo::with_initial_content(&[(
"src/lib.rs",
r#"pub fn keep_me() {}
pub fn delete_me() {}
pub fn also_keep() {}
"#,
)]);
repo.write_file(
"src/lib.rs",
r#"pub fn keep_me() {}
pub fn also_keep() {}
"#,
);
let head_sha = repo.commit("delete a function");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}