#![cfg(not(windows))]
use std::fs;
use std::process::Command;
fn occupy(path: &std::path::Path) {
fs::create_dir_all(path.parent().expect("parent")).expect("create parent");
fs::write(path, "not a directory").expect("occupy hooks path");
}
#[test]
fn a_local_install_that_cannot_write_exits_non_zero() {
let repo = tempfile::tempdir().expect("temp repo");
let home = tempfile::tempdir().expect("temp home");
assert!(Command::new("git")
.args(["init", "-q"])
.current_dir(repo.path())
.status()
.expect("git init")
.success());
fs::create_dir_all(repo.path().join(".git/hooks/post-commit")).expect("occupy hook path");
let output = Command::new(env!("CARGO_BIN_EXE_tokensave"))
.args(["githooks", "on", "--local"])
.current_dir(repo.path())
.env("HOME", home.path())
.env("USERPROFILE", home.path())
.output()
.expect("run tokensave githooks on --local");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!output.status.success(),
"a failed local install must not exit 0. stderr: {stderr}"
);
assert!(
stderr.contains("could not install git hooks"),
"the failure has to say which hooks. stderr: {stderr}"
);
}
#[test]
fn a_global_install_that_cannot_write_exits_non_zero() {
let home = tempfile::tempdir().expect("temp home");
let cwd = tempfile::tempdir().expect("temp cwd");
occupy(&home.path().join(".config/git/hooks"));
let output = Command::new(env!("CARGO_BIN_EXE_tokensave"))
.args(["githooks", "on"])
.current_dir(cwd.path())
.env("HOME", home.path())
.env("USERPROFILE", home.path())
.env_remove("XDG_CONFIG_HOME")
.output()
.expect("run tokensave githooks on");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!output.status.success(),
"a failed global install must not exit 0. stderr: {stderr}"
);
}