#![cfg(unix)]
mod common;
use common::TestRepo;
#[test]
fn post_new_nonzero_exit_fails_gw_new() {
let repo = TestRepo::new();
std::fs::write(
repo.path().join(".cwconfig.json"),
r#"{"hooks":{"post_new":"exit 7"}}"#,
)
.unwrap();
let out = repo.cw(&["new", "feat-postnew-fail", "-T", "skip"]);
assert!(
!out.status.success(),
"gw new should fail when post_new exits non-zero. stdout={:?} stderr={:?}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("post_new"),
"stderr should mention post_new failure, got: {:?}",
stderr
);
}
#[test]
fn pre_rm_fires_with_force_flag() {
let repo = TestRepo::new();
let _ = repo.create_worktree("feat-prerm-force");
let marker_dir = tempfile::tempdir().expect("marker tempdir");
let marker = marker_dir.path().join("pre_rm-fired");
let cfg = format!(r#"{{"hooks":{{"pre_rm":"touch '{}'"}}}}"#, marker.display());
std::fs::write(repo.path().join(".cwconfig.json"), cfg).unwrap();
let out = repo.cw(&["rm", "feat-prerm-force", "--force"]);
assert!(
out.status.success(),
"gw rm --force should succeed. stdout={:?} stderr={:?}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
assert!(
marker.exists(),
"pre_rm hook should have fired even with --force"
);
}
#[test]
fn pre_rm_zero_exit_removes_worktree_cleanly() {
let repo = TestRepo::new();
let wt_path = repo.create_worktree("feat-prerm-ok");
std::fs::write(
repo.path().join(".cwconfig.json"),
r#"{"hooks":{"pre_rm":"exit 0"}}"#,
)
.unwrap();
let out = repo.cw(&["rm", "feat-prerm-ok", "--force"]);
assert!(
out.status.success(),
"gw rm should succeed when pre_rm exits 0. stdout={:?} stderr={:?}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
assert!(
!wt_path.exists(),
"worktree dir should be removed when pre_rm exits 0"
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains("pre_rm hook failed"),
"stderr should not contain a warning when hook succeeds, got: {:?}",
stderr
);
}
#[test]
fn pre_rm_nonzero_exit_is_advisory_rm_continues() {
let repo = TestRepo::new();
let wt_path = repo.create_worktree("feat-prerm-advisory");
std::fs::write(
repo.path().join(".cwconfig.json"),
r#"{"hooks":{"pre_rm":"exit 9"}}"#,
)
.unwrap();
let out = repo.cw(&["rm", "feat-prerm-advisory", "--force"]);
assert!(
out.status.success(),
"gw rm --force should succeed even when pre_rm exits non-zero. stdout={:?} stderr={:?}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
assert!(
!wt_path.exists(),
"worktree dir should be removed despite the failing pre_rm hook"
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("pre_rm hook failed"),
"stderr should contain a warning about the failing hook, got: {:?}",
stderr
);
}