use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;
fn quorum() -> Command {
Command::cargo_bin("quorum").expect("quorum binary built")
}
fn init_repo() -> TempDir {
let td = TempDir::new().unwrap();
git2::Repository::init(td.path()).unwrap();
td
}
#[test]
fn unknown_hook_mode_exits_2_with_clear_error() {
let td = init_repo();
quorum()
.current_dir(td.path())
.args(["review", "--hook-mode=pre-rebase"])
.assert()
.failure()
.code(predicate::eq(2))
.stderr(predicate::str::contains("unknown --hook-mode"));
}
#[test]
fn pre_push_with_no_stdin_tuples_exits_0_with_note() {
let td = init_repo();
quorum()
.current_dir(td.path())
.args(["review", "--hook-mode=pre-push"])
.write_stdin("")
.assert()
.success()
.stderr(predicate::str::contains(
"pre-push received no ref tuples on stdin",
));
}
#[test]
fn pre_push_with_delete_tuple_only_exits_0_with_d4_skip_note() {
let td = init_repo();
let stdin = format!(
"(delete) {zeros} refs/heads/feature/x abc1234567890abcdef1234567890abcdef123456\n",
zeros = "0".repeat(40)
);
quorum()
.current_dir(td.path())
.args(["review", "--hook-mode=pre-push"])
.write_stdin(stdin)
.assert()
.success()
.stderr(predicate::str::contains("branch deletion detected"));
}
#[test]
fn pre_push_with_tag_tuple_only_exits_0_with_d5_skip_note() {
let td = init_repo();
let stdin = format!(
"refs/tags/v1.0 abc1234567890abcdef1234567890abcdef123456 refs/tags/v1.0 {zeros}\n",
zeros = "0".repeat(40)
);
quorum()
.current_dir(td.path())
.args(["review", "--hook-mode=pre-push"])
.write_stdin(stdin)
.assert()
.success()
.stderr(predicate::str::contains("tag push detected"))
.stderr(predicate::str::contains("refs/tags/*"));
}
#[test]
fn pre_push_mixed_tag_and_delete_tuples_both_skipped() {
let td = init_repo();
let stdin = format!(
"(delete) {zeros} refs/heads/old-feature abc1234567890abcdef1234567890abcdef123456\n\
refs/tags/v0.1 abc1234567890abcdef1234567890abcdef123456 refs/tags/v0.1 {zeros}\n",
zeros = "0".repeat(40)
);
quorum()
.current_dir(td.path())
.args(["review", "--hook-mode=pre-push"])
.write_stdin(stdin)
.assert()
.success()
.stderr(predicate::str::contains("branch deletion detected"))
.stderr(predicate::str::contains("tag push detected"));
}
#[test]
fn pre_commit_outside_a_git_repo_errors_cleanly() {
let td = TempDir::new().unwrap(); quorum()
.current_dir(td.path())
.args(["review", "--hook-mode=pre-commit"])
.assert()
.failure()
.code(predicate::eq(2));
}