mod common;
use common::TestRepo;
#[cfg(unix)]
use std::process::Command;
#[cfg(unix)]
#[test]
fn run_event_executes_configured_command() {
let repo = TestRepo::new();
let marker = repo.path().join(".gw-marker");
let cfg = format!(
r#"{{"hooks":{{"post_new":"touch '{}'"}}}}"#,
marker.display()
);
std::fs::write(repo.path().join(".cwconfig.json"), cfg).unwrap();
let result = git_worktree_manager::hooks::run_event("post_new", repo.path());
assert!(result.is_ok(), "hook should succeed: {:?}", result);
assert!(
marker.exists(),
"post_new hook should have touched the marker"
);
}
#[test]
fn run_event_no_op_for_unknown_event() {
let repo = TestRepo::new();
let result = git_worktree_manager::hooks::run_event("definitely-not-an-event", repo.path());
assert!(result.is_ok(), "unknown event should be a no-op");
}
#[test]
fn run_event_no_op_when_hook_unset() {
let repo = TestRepo::new();
let result = git_worktree_manager::hooks::run_event("post_new", repo.path());
assert!(result.is_ok(), "unset hook should be a no-op");
}
#[test]
fn run_event_propagates_nonzero_exit() {
let repo = TestRepo::new();
std::fs::write(
repo.path().join(".cwconfig.json"),
r#"{"hooks":{"post_new":"exit 7"}}"#,
)
.unwrap();
let result = git_worktree_manager::hooks::run_event("post_new", repo.path());
assert!(result.is_err(), "non-zero hook exit should error");
}
#[cfg(unix)]
#[test]
fn run_event_finds_main_repo_config_from_worktree() {
let repo = TestRepo::new();
let marker_dir = tempfile::tempdir().expect("Failed to create marker tempdir");
let marker = marker_dir.path().join("hook-fired");
let cfg = format!(
r#"{{"hooks":{{"post_new":"touch '{}'"}}}}"#,
marker.display()
);
std::fs::write(repo.path().join(".cwconfig.json"), cfg).unwrap();
let wt_dir = tempfile::tempdir().expect("Failed to create worktree tempdir");
let wt_path = wt_dir.path().join("sibling-wt");
let status = Command::new("git")
.args([
"worktree",
"add",
"--detach",
wt_path.to_str().unwrap(),
"HEAD",
])
.current_dir(repo.path())
.status()
.expect("Failed to run git worktree add");
assert!(status.success(), "git worktree add --detach failed");
let result = git_worktree_manager::hooks::run_event("post_new", &wt_path);
assert!(
result.is_ok(),
"hook should succeed from sibling worktree: {:?}",
result
);
assert!(
marker.exists(),
"post_new hook should have created the marker file"
);
}