use super::test_repo::TestRepo;
#[test]
fn given_custom_config_when_check_then_custom_rules_applied() {
let repo = TestRepo::new();
repo.write_config(
r#"
[defaults]
fail_on = "error"
[[rule]]
id = "custom.no_todo"
severity = "error"
message = "TODOs must be resolved before merging"
patterns = ["\\bTODO\\b"]
paths = ["**/*.rs"]
"#,
);
repo.write_file(
"src/lib.rs",
"// TODO: implement this properly\npub fn f() {}\n",
);
let head_sha = repo.commit("add TODO");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("custom.no_todo"));
}
#[test]
fn given_custom_config_overrides_builtin_when_check_then_override_applied() {
let repo = TestRepo::new();
repo.write_config(
r#"
[defaults]
fail_on = "error"
[[rule]]
id = "rust.no_unwrap"
severity = "warn"
message = "Prefer ? over unwrap (warning only)"
languages = ["rust"]
patterns = ["\\.unwrap\\("]
paths = ["**/*.rs"]
"#,
);
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(0);
let receipt = result.parse_receipt();
assert_eq!(receipt.warn_count(), 1);
assert_eq!(receipt.error_count(), 0);
}
#[test]
fn given_no_default_rules_flag_when_check_then_only_custom_rules() {
let repo = TestRepo::new();
repo.write_config(
r#"
[[rule]]
id = "custom.marker"
severity = "warn"
message = "Found marker"
patterns = ["CUSTOM_MARKER"]
paths = ["**/*.rs"]
"#,
);
repo.write_file(
"src/lib.rs",
"// CUSTOM_MARKER\npub fn f() -> u32 { Some(1).unwrap() }\n",
);
let head_sha = repo.commit("add code");
let result = repo.run_check_with_args(&head_sha, &["--no-default-rules"]);
let receipt = result.parse_receipt();
assert!(
receipt.has_finding_with_rule("custom.marker"),
"Custom rule should fire"
);
assert!(
!receipt.has_finding_with_rule("rust.no_unwrap"),
"Built-in unwrap rule should NOT fire with --no-default-rules"
);
}
#[test]
fn given_custom_config_path_when_check_then_loaded() {
let repo = TestRepo::new();
repo.write_file(
"configs/strict.toml",
r#"
[[rule]]
id = "custom.strict"
severity = "error"
message = "Found STRICT marker"
patterns = ["STRICT_MARKER"]
paths = ["**/*.rs"]
"#,
);
repo.write_file("src/lib.rs", "// STRICT_MARKER\npub fn f() {}\n");
let head_sha = repo.commit("add marker");
let result = repo.run_check_with_args(
&head_sha,
&["--config", "configs/strict.toml", "--no-default-rules"],
);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("custom.strict"));
}
#[test]
fn given_default_config_file_when_check_then_auto_loaded() {
let repo = TestRepo::new();
repo.write_config(
r#"
[[rule]]
id = "auto.discovered"
severity = "error"
message = "Auto-discovered config works"
patterns = ["AUTO_DISCOVERY_TEST"]
paths = ["**/*.rs"]
"#,
);
repo.write_file("src/lib.rs", "// AUTO_DISCOVERY_TEST\npub fn f() {}\n");
let head_sha = repo.commit("add test marker");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_with_rule("auto.discovered"));
}
#[test]
fn given_config_with_path_filter_when_check_then_filter_applied() {
let repo = TestRepo::new();
repo.write_config(
r#"
[[rule]]
id = "core.no_debug"
severity = "error"
message = "No debug in core"
patterns = ["DEBUG_CALL"]
paths = ["src/core/**/*.rs"]
"#,
);
repo.write_file("src/core/lib.rs", "// DEBUG_CALL in core\n");
repo.write_file("src/utils/lib.rs", "// DEBUG_CALL in utils\n");
let head_sha = repo.commit("add debug calls");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_at("src/core/lib.rs", 1));
assert!(!receipt.has_finding_at("src/utils/lib.rs", 1));
}
#[test]
fn given_config_with_exclude_paths_when_check_then_excludes_applied() {
let repo = TestRepo::new();
repo.write_config(
r#"
[[rule]]
id = "no.marker"
severity = "error"
message = "Found marker"
patterns = ["MARKER"]
paths = ["**/*.rs"]
exclude_paths = ["**/tests/**"]
"#,
);
repo.write_file("src/lib.rs", "// MARKER in src\n");
repo.write_file("src/tests/test_lib.rs", "// MARKER in tests\n");
let head_sha = repo.commit("add markers");
let result = repo.run_check(&head_sha);
result.assert_exit_code(2);
let receipt = result.parse_receipt();
assert!(receipt.has_finding_at("src/lib.rs", 1));
assert!(!receipt.has_finding_at("src/tests/test_lib.rs", 1));
}
#[test]
fn given_invalid_config_when_check_then_tool_error() {
let repo = TestRepo::new();
repo.write_config("this is not valid [[[ toml syntax");
repo.write_file("src/lib.rs", "pub fn f() {}\n");
let head_sha = repo.commit("add code");
let result = repo.run_check(&head_sha);
result.assert_exit_code(1);
}