mod common;
use common::Repo;
use std::process::Command;
fn run_check(r: &Repo) -> (i32, String) {
let out = Command::new(env!("CARGO_BIN_EXE_amont"))
.arg("run")
.arg("pre-commit-branch-pattern")
.current_dir(&r.dir)
.output()
.expect("amont run");
(
out.status.code().unwrap_or(-1),
format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
),
)
}
fn with_remote(r: &Repo) {
r.git(&["remote", "add", "origin", "/nowhere/in/particular"]);
}
#[test]
fn a_conforming_branch_passes() {
let r = Repo::new();
r.stage("a.txt", "x\n");
r.git(&["commit", "-m", "chore: init"]);
r.git(&["checkout", "-q", "-b", "feat/good-name"]);
with_remote(&r);
let (code, out) = run_check(&r);
assert_eq!(code, 0, "{out}");
assert!(out.contains("conforms"), "{out}");
}
#[test]
fn an_off_contract_branch_warns_without_blocking() {
let r = Repo::new();
r.stage("a.txt", "x\n");
r.git(&["commit", "-m", "chore: init"]);
r.git(&["checkout", "-q", "-b", "definitely-not-conforming"]);
with_remote(&r);
let (code, out) = run_check(&r);
assert_eq!(code, 0, "a warning must not block: {out}");
assert!(out.contains("refused at push time"), "{out}");
assert!(out.contains("git branch -m"), "the fix is named: {out}");
}
#[test]
fn a_remoteless_repository_is_quiet() {
let r = Repo::new();
r.stage("a.txt", "x\n");
r.git(&["commit", "-m", "chore: init"]);
r.git(&["checkout", "-q", "-b", "off-pattern"]);
let (code, out) = run_check(&r);
assert_eq!(code, 0, "{out}");
assert!(!out.contains("refused"), "{out}");
}
#[test]
fn a_branch_the_remote_already_has_is_quiet() {
let r = Repo::new();
r.stage("a.txt", "x\n");
r.git(&["commit", "-m", "chore: init"]);
r.git(&["checkout", "-q", "-b", "legacy-name"]);
with_remote(&r);
r.git(&["update-ref", "refs/remotes/origin/legacy-name", "HEAD"]);
let (code, out) = run_check(&r);
assert_eq!(code, 0, "{out}");
assert!(!out.contains("refused"), "{out}");
}
#[test]
fn a_detached_head_is_quiet() {
let r = Repo::new();
r.stage("a.txt", "x\n");
r.git(&["commit", "-m", "chore: init"]);
with_remote(&r);
r.git(&["checkout", "-q", "--detach", "HEAD"]);
let (code, out) = run_check(&r);
assert_eq!(code, 0, "{out}");
assert!(!out.contains("refused"), "{out}");
}