use super::test_repo::TestRepo;
#[test]
fn given_unwrap_in_comment_when_check_then_not_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"// Consider using .unwrap() here for simplicity
pub fn safe() -> Option<u32> { Some(1) }
"#,
);
let head_sha = repo.commit("add commented unwrap");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_unwrap_in_string_when_check_then_not_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn help_text() -> &'static str {
"Use .unwrap() carefully in production code."
}
"#,
);
let head_sha = repo.commit("add string with unwrap");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_unwrap_in_block_comment_when_check_then_not_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"/*
* This function used to call .unwrap() but was refactored.
* Old code: some_option.unwrap()
*/
pub fn safe_now() -> Option<u32> { Some(1) }
"#,
);
let head_sha = repo.commit("add block comment with unwrap");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_real_unwrap_with_comment_when_check_then_still_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"// This is safe because we know it's always Some
pub fn risky() -> u32 { Some(1).unwrap() }
"#,
);
let head_sha = repo.commit("add unwrap with justification comment");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("rust.no_unwrap"));
}
#[test]
fn given_unwrap_in_doc_comment_when_check_then_not_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"/// Example usage:
/// ```
/// let x = some_option.unwrap();
/// ```
pub fn documented() -> Option<u32> { Some(1) }
"#,
);
let head_sha = repo.commit("add doc comment with unwrap example");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_unwrap_in_raw_string_when_check_then_not_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r##"pub fn code_sample() -> &'static str {
r#"let value = option.unwrap();"#
}
"##,
);
let head_sha = repo.commit("add raw string with unwrap");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_unwrap_in_trailing_comment_when_check_then_not_flagged() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn safe() -> Option<u32> { Some(1) } // not using .unwrap() here
"#,
);
let head_sha = repo.commit("add trailing comment mentioning unwrap");
let result = repo.run_check(&head_sha);
result.assert_exit_code(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 0);
}
#[test]
fn given_suppression_comment_when_check_then_finding_suppressed() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
"pub fn f() -> u32 { Some(1).unwrap() } // diffguard: ignore rust.no_unwrap\n",
);
let head_sha = repo.commit("add unwrap with suppression");
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.error_count(), 0);
}
#[test]
fn given_directive_in_string_when_check_then_not_suppressed() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn f() -> u32 { let _ = "diffguard: ignore rust.no_unwrap"; Some(1).unwrap() }
"#,
);
let head_sha = repo.commit("directive in string");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("rust.no_unwrap"));
}
#[test]
fn given_ignore_next_line_in_string_when_check_then_not_suppressed() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn f() -> u32 {
let _ = "diffguard: ignore-next-line rust.no_unwrap";
Some(1).unwrap()
}
"#,
);
let head_sha = repo.commit("ignore-next-line in string");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("rust.no_unwrap"));
}