#![allow(clippy::expect_used, clippy::unwrap_used)]
mod support;
use std::path::{Path, PathBuf};
use std::process::Command;
#[cfg(unix)]
use predicates::prelude::PredicateBooleanExt;
use support::snapshot;
use tempfile::TempDir;
use voom::git::{GitPruneOptions, Status, StepKind, prune, prune_during_sweep, prune_repositories};
fn git_available() -> bool {
Command::new("git")
.arg("--version")
.output()
.is_ok_and(|output| output.status.success())
}
fn git(dir: &Path, args: &[&str]) -> String {
let output = Command::new("git")
.current_dir(dir)
.args(args)
.output()
.unwrap_or_else(|error| panic!("running `git {}` in {}: {error}", args.join(" "), dir.display()));
assert!(
output.status.success(),
"`git {}` failed in {}: {}",
args.join(" "),
dir.display(),
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).into_owned()
}
fn repository(root: &Path, name: &str) -> PathBuf {
let path = root.join(name);
std::fs::create_dir_all(&path).expect("the repository directory");
git(&path, &["init", "--quiet"]);
git(&path, &["config", "user.name", "voom tests"]);
git(&path, &["config", "user.email", "tests@voom.invalid"]);
git(&path, &["config", "commit.gpgsign", "false"]);
git(&path, &["symbolic-ref", "HEAD", "refs/heads/main"]);
std::fs::write(path.join("README.md"), b"fixture").expect("a file to commit");
git(&path, &["add", "-A"]);
git(&path, &["commit", "--quiet", "-m", "initial"]);
path
}
fn sweep_options(root: &Path) -> GitPruneOptions {
GitPruneOptions {
roots: vec![root.to_path_buf()],
jobs: Some(2),
..GitPruneOptions::default()
}
}
fn subcommand_options(root: &Path) -> GitPruneOptions {
GitPruneOptions {
count_objects: true,
..sweep_options(root)
}
}
fn canonical(path: &Path) -> PathBuf {
path.canonicalize().expect("the path exists")
}
#[test]
#[cfg(unix)]
fn should_prune_a_stale_worktree_and_leave_a_live_one() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(&repository, &["worktree", "add", "--quiet", "../live", "-b", "live"]);
git(&repository, &["worktree", "add", "--quiet", "../stale", "-b", "stale"]);
std::fs::remove_dir_all(root.path().join("stale")).expect("the stale checkout goes away");
age_worktree(&repository, "stale");
let result = prune(&subcommand_options(root.path())).expect("git is available");
let stale = repository.join(".git/worktrees/stale");
let live = repository.join(".git/worktrees/live");
let reported = result.repositories[0]
.steps
.iter()
.find(|step| step.kind == StepKind::WorktreePrune)
.and_then(|step| step.summary().map(ToOwned::to_owned))
.unwrap_or_default();
assert_eq!(
reported,
"1 stale worktree pruned",
"the report must say what was removed from {}",
repository.display()
);
assert!(
!stale.exists(),
"the stale worktree's administrative files remain: {}",
stale.display()
);
assert!(live.exists(), "a live worktree must not be pruned: {}", live.display());
assert!(
root.path().join("live").exists(),
"and its checkout must still be there: {}",
root.path().join("live").display()
);
}
#[test]
fn should_skip_a_repository_mid_rebase_and_name_it() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "halfway");
std::fs::create_dir_all(repository.join(".git/rebase-merge")).expect("the rebase state");
std::fs::write(repository.join(".git/rebase-merge/head-name"), b"refs/heads/main").expect("its head-name");
let result = prune(&subcommand_options(root.path())).expect("git is available");
let entry = result
.repositories
.iter()
.find(|found| found.path == canonical(&repository))
.unwrap_or_else(|| panic!("{} was not reported at all", repository.display()));
assert_eq!(
entry.status(),
Status::Skipped,
"{} was not skipped: {:?}",
repository.display(),
entry.steps
);
assert!(entry.steps.is_empty(), "nothing may run in a repository mid-rebase");
let reason = entry.skipped.as_ref().map(ToString::to_string).unwrap_or_default();
assert!(
reason.contains("rebase-merge"),
"the reason must name the marker, not just say `skipped`: {reason}"
);
}
#[test]
fn should_prune_a_repository_with_linked_worktrees_exactly_once() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(&repository, &["worktree", "add", "--quiet", "../one", "-b", "one"]);
git(&repository, &["worktree", "add", "--quiet", "../two", "-b", "two"]);
let result = prune(&subcommand_options(root.path())).expect("git is available");
let paths: Vec<&Path> = result.repositories.iter().map(|found| found.path.as_path()).collect();
assert_eq!(
paths,
vec![canonical(&repository).as_path()],
"a linked worktree is not a repository of its own; got {paths:?}"
);
}
#[test]
#[cfg(unix)]
fn should_change_nothing_under_dry_run_and_report_what_a_real_run_then_does() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(&repository, &["worktree", "add", "--quiet", "../stale", "-b", "stale"]);
std::fs::remove_dir_all(root.path().join("stale")).expect("the stale checkout goes away");
age_worktree(&repository, "stale");
let before = snapshot(root.path());
let dry = prune(&GitPruneOptions {
dry_run: true,
..subcommand_options(root.path())
})
.expect("git is available");
assert_eq!(snapshot(root.path()), before, "a dry run left the tree different");
let predicted = dry.repositories[0]
.steps
.iter()
.find(|step| step.kind == StepKind::WorktreePrune)
.and_then(|step| step.summary().map(ToOwned::to_owned))
.unwrap_or_default();
assert_eq!(
predicted, "1 stale worktree pruned",
"the dry run did not say what it would do"
);
let real = prune(&subcommand_options(root.path())).expect("git is available");
let done = real.repositories[0]
.steps
.iter()
.find(|step| step.kind == StepKind::WorktreePrune)
.and_then(|step| step.summary().map(ToOwned::to_owned))
.unwrap_or_default();
assert_eq!(
done, predicted,
"the real run did something the dry run did not predict"
);
assert!(
!repository.join(".git/worktrees/stale").exists(),
"and the real run really removed it"
);
}
#[test]
fn should_ignore_a_directory_that_is_not_a_repository() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
std::fs::create_dir_all(root.path().join("just/a/tree")).expect("a plain tree");
std::fs::write(root.path().join("just/a/tree/notes.txt"), b"nothing to see").expect("a file");
let before = snapshot(root.path());
let result = prune(&subcommand_options(root.path())).expect("git is available");
assert!(
result.repositories.is_empty(),
"a plain directory was taken for a repository: {:?}",
result.repositories
);
assert_eq!(snapshot(root.path()), before);
}
#[test]
fn should_report_a_failing_repository_without_aborting_the_run() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let broken = repository(root.path(), "broken");
let healthy = repository(root.path(), "healthy");
std::fs::write(broken.join(".git/config"), b"[core\n").expect("a broken config");
let result = prune(&subcommand_options(root.path())).expect("git is available");
let status = |path: &Path| {
result
.repositories
.iter()
.find(|found| found.path == canonical(path))
.unwrap_or_else(|| panic!("{} was not reported", path.display()))
.status()
};
assert_eq!(
status(&broken),
Status::Failed,
"{} should have failed",
broken.display()
);
assert_eq!(
status(&healthy),
Status::Pruned,
"{} must be pruned despite its neighbour failing",
healthy.display()
);
assert_eq!(result.totals().failed, 1);
assert_eq!(result.exit_code(), 1, "a failed repository is a non-zero exit");
}
#[test]
fn should_run_no_networked_command_during_a_default_sweep() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(
&repository,
&["remote", "add", "origin", "https://example.invalid/api.git"],
);
let result = prune_during_sweep(std::slice::from_ref(&repository), &sweep_options(root.path())).expect("a result");
let commands: Vec<&[String]> = result.repositories[0]
.steps
.iter()
.map(|step| subcommand(&step.command))
.collect();
assert!(
!commands.iter().any(|command| command.iter().any(|arg| arg == "remote")),
"a sweep built a command that would contact the remote: {commands:?}"
);
assert_eq!(
commands,
vec![
&["worktree", "prune", "--expire", "3.months.ago", "-v"].map(ToOwned::to_owned)[..],
&["gc", "--auto"].map(ToOwned::to_owned)[..],
]
);
}
#[test]
fn should_repack_only_on_gits_own_thresholds() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
let result = prune_repositories(&[repository], &sweep_options(root.path())).expect("git is available");
let repack = result.repositories[0]
.steps
.iter()
.find(|step| step.kind == StepKind::Gc)
.expect("a repack step");
assert_eq!(subcommand(&repack.command), ["gc", "--auto"], "never a bare `gc`");
assert!(repack.succeeded(), "{:?}", repack.outcome);
assert_eq!(
repack.summary(),
None,
"a repository below git's thresholds has nothing to report, which is what keeps a sweep silent"
);
}
#[test]
fn should_have_nothing_noteworthy_to_say_about_a_quiet_repository() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
let result = prune_during_sweep(&[repository], &sweep_options(root.path())).expect("a result");
assert!(
result.noteworthy().is_empty(),
"a quiet repository would have produced output: {:?}",
result.repositories
);
}
#[test]
fn should_say_nothing_when_a_sweep_walked_past_no_repositories() {
let root = TempDir::new().expect("a temporary directory");
assert!(prune_during_sweep(&[], &sweep_options(root.path())).is_none());
}
#[test]
#[cfg(unix)]
fn should_prune_a_repository_a_sweep_walked_past() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(&repository, &["worktree", "add", "--quiet", "../stale", "-b", "stale"]);
std::fs::remove_dir_all(root.path().join("stale")).expect("the stale checkout goes away");
age_worktree(&repository, "stale");
let result = prune_during_sweep(std::slice::from_ref(&repository), &sweep_options(root.path())).expect("a result");
assert!(
!repository.join(".git/worktrees/stale").exists(),
"the sweep did not prune the stale worktree it walked past"
);
assert_eq!(result.noteworthy().len(), 1, "and it is worth a line in the report");
}
#[cfg(unix)]
fn tree_with_a_stale_worktree(root: &Path) -> PathBuf {
let repository = repository(root, "project");
git(&repository, &["worktree", "add", "--quiet", "../stale", "-b", "stale"]);
std::fs::remove_dir_all(root.join("stale")).expect("removing the checkout makes it stale");
age_worktree(&repository, "stale");
std::fs::create_dir_all(repository.join("target")).expect("an artifact directory");
std::fs::write(repository.join("Cargo.toml"), b"[package]").expect("a marker");
std::fs::write(repository.join("target/app"), vec![0u8; 4096]).expect("build output");
repository.join(".git/worktrees/stale")
}
#[cfg(unix)]
fn age_worktree(repository: &Path, name: &str) {
let private = repository.join(".git/worktrees").join(name).join("index");
let output = Command::new("touch")
.args(["-t", "202001010000"])
.arg(&private)
.output()
.unwrap_or_else(|error| panic!("backdating {}: {error}", private.display()));
assert!(output.status.success(), "backdating {} failed", private.display());
}
fn subcommand(command: &[String]) -> &[String] {
let start = command
.iter()
.position(|arg| arg.starts_with("--git-dir="))
.map_or(1, |index| index + 1);
&command[start..]
}
#[cfg(unix)]
fn voom() -> assert_cmd::Command {
assert_cmd::Command::cargo_bin("voom").expect("the binary builds")
}
#[test]
#[cfg(unix)]
fn should_prune_a_repository_a_sweep_walked_past_without_being_asked() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let stale = tree_with_a_stale_worktree(root.path());
voom()
.arg(root.path())
.assert()
.success()
.stdout(predicates::str::contains("1 stale worktree pruned"));
assert!(
!stale.exists(),
"an ordinary sweep prunes it: {} is still there",
stale.display()
);
assert!(
!root.path().join("project/target").exists(),
"and it still removed the build artifact it was actually run for"
);
}
#[test]
#[cfg(unix)]
fn should_not_touch_a_repository_when_git_housekeeping_is_turned_off() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let stale = tree_with_a_stale_worktree(root.path());
voom()
.args(["--no-git", "--color", "never"])
.arg(root.path())
.assert()
.success()
.stdout(predicates::str::contains("worktree").not())
.stdout(predicates::str::contains(" · git ").not());
assert!(
stale.exists(),
"--no-git leaves the repository alone: {} was pruned anyway",
stale.display()
);
}
#[test]
#[cfg(unix)]
fn should_honour_a_config_file_turning_git_housekeeping_off() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let stale = tree_with_a_stale_worktree(root.path());
std::fs::write(root.path().join("voom.toml"), b"[git]\nenabled = false\n").expect("a config file");
voom().arg(root.path()).assert().success();
assert!(
stale.exists(),
"`[git] enabled = false` leaves the repository alone: {} was pruned anyway",
stale.display()
);
}
#[test]
#[cfg(unix)]
fn should_predict_git_housekeeping_in_a_dry_run_without_performing_it() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let stale = tree_with_a_stale_worktree(root.path());
let before = snapshot(root.path());
voom()
.args(["--dry-run", "--color", "never"])
.arg(root.path())
.assert()
.success()
.stdout(predicates::str::contains("would prune"))
.stdout(predicates::str::contains("1 stale worktree pruned"));
assert!(stale.exists(), "a dry run prunes nothing: {}", stale.display());
assert_eq!(snapshot(root.path()), before, "and leaves the tree byte-identical");
}
#[test]
#[cfg(unix)]
fn should_say_nothing_about_a_repository_that_needed_nothing() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "project");
std::fs::create_dir_all(repository.join("target")).expect("an artifact directory");
std::fs::write(repository.join("Cargo.toml"), b"[package]").expect("a marker");
std::fs::write(repository.join("target/app"), vec![0u8; 4096]).expect("build output");
voom()
.args(["--color", "never"])
.arg(root.path())
.assert()
.success()
.stdout(predicates::str::contains("pruned").not());
}
#[test]
#[cfg(unix)]
fn should_let_the_flag_beat_a_config_file_that_turns_git_housekeeping_on() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let stale = tree_with_a_stale_worktree(root.path());
std::fs::write(root.path().join("voom.toml"), b"[git]\nenabled = true\n").expect("a config file");
voom().args(["--no-git"]).arg(root.path()).assert().success();
assert!(
stale.exists(),
"--no-git wins over `[git] enabled = true`: {} was pruned anyway",
stale.display()
);
}
#[test]
fn should_not_prune_a_worktree_that_only_just_went_missing() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(
&repository,
&["worktree", "add", "--quiet", "../unmounted", "-b", "elsewhere"],
);
std::fs::remove_dir_all(root.path().join("unmounted")).expect("as if the disk were unmounted");
prune(&subcommand_options(root.path())).expect("git is available");
assert!(
repository.join(".git/worktrees/unmounted").exists(),
"an absent checkout is not by itself grounds for discarding its reflog"
);
}
#[test]
#[cfg(unix)]
fn should_never_let_a_repository_choose_what_voom_runs() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let repository = repository(root.path(), "api");
git(&repository, &["config", "gc.auto", "1"]);
let hooks = repository.join(".git/hooks");
std::fs::create_dir_all(&hooks).expect("a hooks directory");
let evidence = root.path().join("hook-ran");
std::fs::write(
hooks.join("pre-auto-gc"),
format!("#!/bin/sh\ntouch {}\n", evidence.display()),
)
.expect("a hook");
let mut mode = std::fs::metadata(hooks.join("pre-auto-gc"))
.expect("the hook")
.permissions();
std::os::unix::fs::PermissionsExt::set_mode(&mut mode, 0o755);
std::fs::set_permissions(hooks.join("pre-auto-gc"), mode).expect("an executable hook");
let result = prune(&GitPruneOptions {
dry_run: false,
..subcommand_options(root.path())
})
.expect("git is available");
assert!(
!evidence.exists(),
"voom ran the repository's pre-auto-gc hook: {}",
evidence.display()
);
for step in &result.repositories[0].steps {
assert!(
step.command.contains(&"core.hooksPath=".to_owned()),
"every command disables hooks, got {:?}",
step.command
);
assert!(
step.command.iter().any(|arg| arg.starts_with("--git-dir=")),
"every command pins the store, got {:?}",
step.command
);
}
}
#[test]
fn should_refuse_a_repository_whose_object_store_escapes_the_scan_root() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
let scanned = root.path().join("scanned");
let store = root.path().join("outside/store");
std::fs::create_dir_all(&scanned).expect("the scan root");
std::fs::create_dir_all(root.path().join("outside")).expect("somewhere else");
let output = Command::new("git")
.args(["init", "--quiet", "--separate-git-dir"])
.arg(&store)
.arg(scanned.join("project"))
.output()
.expect("running git init");
if !output.status.success() {
return;
}
let result = prune(&subcommand_options(&scanned)).expect("git is available");
assert_eq!(
result
.repositories
.iter()
.map(|repository| (repository.status(), repository.steps.len()))
.collect::<Vec<_>>(),
vec![(Status::Skipped, 0)],
"a store outside the scan root is refused before any command is built"
);
}
#[test]
fn should_not_report_an_uninitialised_git_directory_as_a_failure() {
if !git_available() {
return;
}
let root = TempDir::new().expect("a temporary directory");
std::fs::create_dir_all(root.path().join("hollow/.git/hooks")).expect("the shell of a repository");
let result = prune(&subcommand_options(root.path())).expect("git is available");
assert!(
result.repositories.is_empty(),
"an uninitialised .git is not a repository at all, got {:?}",
result
.repositories
.iter()
.map(|r| (&r.path, r.status()))
.collect::<Vec<_>>()
);
}