#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
#[path = "../build_support/git_watch.rs"]
mod git_watch;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread::sleep;
use std::time::{Duration, SystemTime};
fn git(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.current_dir(dir)
.args([
"-c",
"user.name=ant-node test",
"-c",
"user.email=test@example.invalid",
"-c",
"commit.gpgsign=false",
])
.args(args)
.output()
.expect("git is installed");
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout).unwrap().trim().to_string()
}
fn repo() -> (tempfile::TempDir, PathBuf) {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("repo");
fs::create_dir(&dir).unwrap();
git(&dir, &["init", "-q", "-b", "main"]);
git(&dir, &["commit", "-q", "--allow-empty", "-m", "one"]);
(tmp, dir)
}
fn watch_paths_for(dir: &Path) -> Vec<PathBuf> {
let resolve = |p: String| {
let p = PathBuf::from(p);
if p.is_absolute() {
p
} else {
dir.join(p)
}
};
let git_dir = resolve(git(dir, &["rev-parse", "--git-dir"]));
let common_dir = resolve(git(dir, &["rev-parse", "--git-common-dir"]));
git_watch::watch_paths(&git_dir, &common_dir)
}
fn newest_mtime(path: &Path) -> SystemTime {
let meta = fs::metadata(path).unwrap();
let mut newest = meta.modified().unwrap();
if meta.is_dir() {
for entry in fs::read_dir(path).unwrap() {
newest = newest.max(newest_mtime(&entry.unwrap().path()));
}
}
newest
}
fn newest_of(paths: &[PathBuf]) -> SystemTime {
paths
.iter()
.map(|p| newest_mtime(p))
.max()
.expect("the watch set is not empty")
}
fn tick() {
sleep(Duration::from_millis(1100));
}
fn assert_all_exist(paths: &[PathBuf]) {
assert!(!paths.is_empty(), "nothing is watched");
for p in paths {
assert!(
p.exists(),
"watch set contains a missing path: {}",
p.display()
);
}
}
#[test]
fn a_commit_on_a_branch_changes_a_watched_path() {
let (_tmp, dir) = repo();
let paths = watch_paths_for(&dir);
assert_all_exist(&paths);
let before = newest_of(&paths);
tick();
git(&dir, &["commit", "-q", "--allow-empty", "-m", "two"]);
assert!(
newest_of(&paths) > before,
"a commit changed no watched path"
);
}
#[test]
fn with_refs_packed_and_no_refs_heads_the_next_commit_is_still_seen() {
let (_tmp, dir) = repo();
git(&dir, &["pack-refs", "--all", "--prune"]);
let heads = dir.join(".git").join("refs").join("heads");
if heads.exists() {
fs::remove_dir(&heads).unwrap();
}
assert!(!heads.exists());
let paths = watch_paths_for(&dir);
assert_all_exist(&paths);
assert!(
!paths.contains(&heads),
"the missing refs/heads must not be emitted"
);
let head = dir.join(".git").join("HEAD");
let packed = dir.join(".git").join("packed-refs");
let head_before = newest_mtime(&head);
let packed_before = newest_mtime(&packed);
let before = newest_of(&paths);
tick();
git(&dir, &["commit", "-q", "--allow-empty", "-m", "two"]);
assert!(heads.join("main").exists(), "git recreated the loose ref");
assert_eq!(newest_mtime(&head), head_before, "HEAD was not rewritten");
assert_eq!(
newest_mtime(&packed),
packed_before,
"packed-refs was not rewritten"
);
assert!(
newest_of(&paths) > before,
"the commit changed no watched path, so ANT_GIT_COMMIT would go stale"
);
}
#[test]
fn in_a_worktree_every_watched_path_exists_and_a_commit_is_seen() {
let (tmp, dir) = repo();
let wt = tmp.path().join("wt");
git(
&dir,
&[
"worktree",
"add",
"-q",
"-b",
"feature",
wt.to_str().unwrap(),
],
);
assert!(wt.join(".git").is_file(), "a worktree's .git is a file");
let paths = watch_paths_for(&wt);
assert_all_exist(&paths);
let before = newest_of(&paths);
tick();
git(&wt, &["commit", "-q", "--allow-empty", "-m", "two"]);
assert!(
newest_of(&paths) > before,
"a worktree commit changed no watched path"
);
}