use std::path::{Path, PathBuf};
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.canonicalize()
.expect("resolve repo root")
}
fn read(path: &Path) -> String {
std::fs::read_to_string(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()))
}
fn hook() -> String {
read(&repo_root().join("scripts/hooks/pre-push"))
}
#[test]
fn unsigned_tag_extraction_cannot_abort_the_hook_before_it_explains_itself() {
let source = hook();
let line = source
.lines()
.find(|line| line.contains("got_fpr=") && line.contains("grep -oE"))
.expect("pre-push must extract a signature fingerprint into got_fpr via grep");
assert!(
line.contains("|| true"),
"the got_fpr extraction must tolerate a no-match grep. Without `|| true`, \
`set -e` aborts the hook at this assignment for an UNSIGNED tag, refusing \
the push silently instead of printing the 'it is not signed' diagnostic \
that follows.\n offending line: {}",
line.trim()
);
}
#[test]
fn policy_compares_key_fingerprints_not_signer_identity() {
let source = hook();
assert!(
source.contains("RELEASE_FPR") && source.contains("ssh-keygen -lf"),
"the release key must be reduced to a fingerprint with `ssh-keygen -lf` \
and compared as RELEASE_FPR"
);
assert!(
source.contains(r#""$got_fpr" != "$RELEASE_FPR""#),
"the policy check must compare the tag's fingerprint against the \
configured release key's fingerprint"
);
}
#[test]
fn policy_runs_before_the_expensive_container_check() {
let source = hook();
let policy = source
.find("RELEASE_KEY=")
.expect("pre-push must read devflow.releaseSigningKey");
let container = source
.find("check-in-container.sh")
.expect("pre-push must run the container check");
assert!(
policy < container,
"the signing-policy guard must run before the container check so a \
rejected push fails in milliseconds rather than minutes"
);
}
#[test]
fn direct_pushes_to_main_are_refused() {
let source = hook();
assert!(
source.contains("refs/heads/main"),
"pre-push must refuse a direct push to main"
);
}
#[test]
fn policy_is_opt_in_by_config_and_has_no_override_escape_hatch() {
let source = hook();
assert!(
source.contains("devflow.releaseSigningKey"),
"enforcement must key off devflow.releaseSigningKey"
);
for escape in [
"DEVFLOW_SKIP_SIGNING",
"SKIP_SIGNING",
"DEVFLOW_ALLOW_AGENT_TAG",
] {
assert!(
!source.contains(escape),
"no environment override may bypass the signing policy (found `{escape}`). \
The escape hatch is to re-sign the tag with the correct key."
);
}
}