use super::test_repo::TestRepo;
#[test]
fn given_max_findings_2_when_5_violations_then_2_findings_but_5_in_counts() {
let repo = TestRepo::new();
repo.write_config(
r#"
[defaults]
max_findings = 2
[[rule]]
id = "rust.no_unwrap"
severity = "error"
message = "Avoid unwrap"
languages = ["rust"]
patterns = ["\\.unwrap\\("]
paths = ["**/*.rs"]
"#,
);
repo.write_file(
"src/lib.rs",
r#"pub fn one() -> u32 { Some(1).unwrap() }
pub fn two() -> u32 { Some(2).unwrap() }
pub fn three() -> u32 { Some(3).unwrap() }
pub fn four() -> u32 { Some(4).unwrap() }
pub fn five() -> u32 { Some(5).unwrap() }
"#,
);
let head_sha = repo.commit("add 5 unwraps");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert_eq!(
receipt.findings_count(),
2,
"Should only have 2 findings in the list"
);
assert_eq!(
receipt.error_count(),
5,
"Error count should be 5 (all violations)"
);
}
#[test]
fn given_cli_max_findings_1_when_multiple_violations_then_1_finding() {
let repo = TestRepo::new();
repo.write_file(
"src/lib.rs",
r#"pub fn a() -> u32 { Some(1).unwrap() }
pub fn b() -> u32 { Some(2).unwrap() }
pub fn c() -> u32 { Some(3).unwrap() }
"#,
);
let head_sha = repo.commit("add 3 unwraps");
let result = repo.run_check_with_args(&head_sha, &["--max-findings", "1"]);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 1);
assert_eq!(receipt.error_count(), 3);
}
#[test]
fn given_many_violations_when_default_limit_then_all_included() {
let repo = TestRepo::new();
let mut content = String::new();
for i in 1..=10 {
content.push_str(&format!(
"pub fn f{}() -> u32 {{ Some({}).unwrap() }}\n",
i, i
));
}
repo.write_file("src/lib.rs", &content);
let head_sha = repo.commit("add 10 unwraps");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 10);
assert_eq!(receipt.error_count(), 10);
}
#[test]
fn given_violations_in_multiple_files_when_max_findings_3_then_3_total() {
let repo = TestRepo::new();
repo.write_config(
r#"
[defaults]
max_findings = 3
[[rule]]
id = "rust.no_unwrap"
severity = "error"
message = "Avoid unwrap"
languages = ["rust"]
patterns = ["\\.unwrap\\("]
paths = ["**/*.rs"]
"#,
);
repo.write_file(
"src/a.rs",
"pub fn a1() -> u32 { Some(1).unwrap() }\npub fn a2() -> u32 { Some(2).unwrap() }\n",
);
repo.write_file(
"src/b.rs",
"pub fn b1() -> u32 { Some(1).unwrap() }\npub fn b2() -> u32 { Some(2).unwrap() }\n",
);
repo.write_file(
"src/c.rs",
"pub fn c1() -> u32 { Some(1).unwrap() }\npub fn c2() -> u32 { Some(2).unwrap() }\n",
);
let head_sha = repo.commit("add unwraps in multiple files");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 3);
assert_eq!(receipt.error_count(), 6);
}
#[test]
fn given_truncation_when_check_then_reason_mentions_omitted() {
let repo = TestRepo::new();
repo.write_config(
r#"
[defaults]
max_findings = 1
[[rule]]
id = "rust.no_unwrap"
severity = "error"
message = "Avoid unwrap"
languages = ["rust"]
patterns = ["\\.unwrap\\("]
paths = ["**/*.rs"]
"#,
);
repo.write_file(
"src/lib.rs",
"pub fn a() -> u32 { Some(1).unwrap() }\npub fn b() -> u32 { Some(2).unwrap() }\npub fn c() -> u32 { Some(3).unwrap() }\n",
);
let head_sha = repo.commit("add 3 unwraps");
let result = repo.run_check(&head_sha);
result.assert_receipt_contains("truncated");
}
#[test]
fn given_mixed_severities_when_max_findings_2_then_counts_accurate() {
let repo = TestRepo::new();
repo.write_config(
r#"
[defaults]
max_findings = 2
[[rule]]
id = "rust.no_unwrap"
severity = "error"
message = "Avoid unwrap"
languages = ["rust"]
patterns = ["\\.unwrap\\("]
paths = ["**/*.rs"]
[[rule]]
id = "rust.no_dbg"
severity = "warn"
message = "Remove dbg!"
languages = ["rust"]
patterns = ["\\bdbg!\\("]
paths = ["**/*.rs"]
"#,
);
repo.write_file(
"src/lib.rs",
r#"pub fn a() -> u32 { dbg!(Some(1).unwrap()) }
pub fn b() -> u32 { dbg!(Some(2).unwrap()) }
pub fn c() -> u32 { Some(3).unwrap() }
"#,
);
let head_sha = repo.commit("add mixed violations");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert_eq!(receipt.findings_count(), 2);
assert!(receipt.error_count() >= 2, "Should have at least 2 errors");
assert!(
receipt.warn_count() >= 1 || receipt.error_count() >= 3,
"Should have violations"
);
}