use std::path::PathBuf;
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.canonicalize()
.expect("resolve repo root")
}
fn hook() -> String {
std::fs::read_to_string(repo_root().join("scripts/hooks/pre-commit"))
.expect("read scripts/hooks/pre-commit")
}
#[test]
fn branch_is_detected_via_symbolic_ref_not_rev_parse() {
let source = hook();
assert!(
source.contains("git symbolic-ref --short HEAD"),
"the guard must detect the branch via `git symbolic-ref --short HEAD`; \
`git rev-parse --abbrev-ref HEAD` returns `HEAD` on an unborn branch and \
would miss the very first commit on develop/main"
);
}
#[test]
fn both_protected_branches_refuse_with_a_nonzero_exit() {
let source = hook();
assert!(
source.lines().any(|line| line.trim() == "develop | main)"),
"the guard's case arm must match `develop | main`"
);
assert!(
source.contains("exit 1"),
"the guard must exit non-zero when it matches a protected branch"
);
}