use super::test_repo::TestRepo;
#[test]
fn given_unwrap_added_when_check_then_finding_reported() {
let repo = TestRepo::new();
repo.write_file("src/lib.rs", "pub fn f() -> u32 { Some(1).unwrap() }\n");
let head_sha = repo.commit("add unwrap");
let result = repo.run_check(&head_sha);
result
.assert_exit_code(2)
.assert_receipt_exists()
.assert_receipt_contains("rust.no_unwrap");
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("rust.no_unwrap"));
assert!(receipt.has_finding_at("src/lib.rs", 1));
assert_eq!(receipt.error_count(), 1);
}
#[test]
fn given_multiple_unwraps_added_when_check_then_all_findings_reported() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn f() -> u32 {
let a = Some(1).unwrap();
let b = Some(2).unwrap();
let c = Some(3).expect("oops");
a + b + c
}
"#,
);
let head_sha = repo.commit("add multiple unwraps");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert_eq!(receipt.error_count(), 3);
}
#[test]
fn given_new_file_with_unwrap_when_check_then_finding_reported() {
let repo = TestRepo::new();
repo.write_file(
"src/new_module.rs",
"pub fn risky() { let _ = None::<i32>.unwrap(); }\n",
);
let head_sha = repo.commit("add new module");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_at("src/new_module.rs", 1));
}
#[test]
fn given_line_changed_to_unwrap_when_scope_changed_then_finding_reported() {
let repo = TestRepo::with_initial_content(&[(
"src/lib.rs",
"pub fn f() -> Option<u32> { Some(1) }\n",
)]);
repo.write_file("src/lib.rs", "pub fn f() -> u32 { Some(1).unwrap() }\n");
let head_sha = repo.commit("change to use unwrap");
let result = repo.run_check_with_args(&head_sha, &["--scope", "changed"]);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("rust.no_unwrap"));
}
#[test]
fn given_line_changed_to_unwrap_when_scope_modified_then_finding_reported() {
let repo = TestRepo::with_initial_content(&[(
"src/lib.rs",
"pub fn f() -> Option<u32> { Some(1) }\n",
)]);
repo.write_file("src/lib.rs", "pub fn f() -> u32 { Some(1).unwrap() }\n");
let head_sha = repo.commit("change to use unwrap");
let result = repo.run_check_with_args(&head_sha, &["--scope", "modified"]);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("rust.no_unwrap"));
}
#[test]
fn given_unwrap_removed_when_scope_deleted_then_finding_reported() {
let repo = TestRepo::with_initial_content(&[(
"src/lib.rs",
"pub fn f() -> u32 { Some(1).unwrap() }\n",
)]);
repo.write_file("src/lib.rs", "pub fn f() -> Option<u32> { Some(1) }\n");
let head_sha = repo.commit("remove unwrap");
let deleted_result = repo.run_check_with_args(&head_sha, &["--scope", "deleted"]);
deleted_result.assert_exit_code(2);
let deleted_receipt = deleted_result.parse_receipt();
assert!(deleted_receipt.has_finding_with_rule("rust.no_unwrap"));
assert!(deleted_receipt.has_finding_at("src/lib.rs", 1));
let added_result = repo.run_check_with_args(&head_sha, &["--scope", "added"]);
added_result.assert_exit_code(0);
}
#[test]
fn given_clean_code_added_when_check_then_pass() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn f() -> Option<u32> { Some(1) }
pub fn g() -> Option<u32> { Some(2) }
"#,
);
let head_sha = repo.commit("add clean code");
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"));
}