Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Test same-basename trash staging without depending on drain timing (#4561)

Maximilian Roos1c6eb9da85dd
diff --git a/src/git/remove.rs b/src/git/remove.rs
index e044da6e6..649592a1e 100644
--- a/src/git/remove.rs
+++ b/src/git/remove.rs
@@ -913,6 +913,44 @@ mod tests {
fs::set_permissions(&trash, fs::Permissions::from_mode(0o755)).unwrap();
}
+ /// Same-basename worktrees need distinct staging paths. Test staging
+ /// directly: a prune batch may share one detached drain, so per-branch
+ /// cleanup logs do not prove whether each worktree was staged.
+ #[test]
+ fn stages_worktrees_sharing_a_basename() {
+ let mut test = TestRepo::with_initial_commit();
+ let parent = test.root_path().parent().unwrap().to_path_buf();
+ let worktrees = [
+ ("one", parent.join("aaaa/repo")),
+ ("two", parent.join("bbbb/repo")),
+ ];
+ for (branch, path) in &worktrees {
+ test.add_worktree_at_path(branch, path);
+ }
+ let repo = Repository::at(test.root_path()).unwrap();
+ let mut staged_paths = Vec::new();
+ for (branch, path) in &worktrees {
+ let git_dir = repo.worktree_at(path).git_dir().unwrap();
+ let registration = git_dir.file_name().unwrap().to_string_lossy();
+ let staged = stage_worktree_removal(&repo, path, Some(branch), false)
+ .unwrap()
+ .expect("each worktree must stage successfully");
+ assert!(!path.exists());
+ assert!(staged.exists());
+ // Pin the registration prefix independently of the wall clock.
+ assert!(
+ staged
+ .file_name()
+ .unwrap()
+ .to_string_lossy()
+ .starts_with(&format!("{registration}-"))
+ );
+ staged_paths.push(staged);
+ }
+ assert_ne!(staged_paths[0], staged_paths[1]);
+ assert!(staged_paths.iter().all(|path| path.exists()));
+ }
+
/// A `git worktree lock` must stop the rename even when the caller skipped
/// `prepare_worktree_removal` (merge used to construct a plan by hand).
#[test]
diff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rs
index 6fa5756e6..451969f65 100644
--- a/tests/integration_tests/step_prune.rs
+++ b/tests/integration_tests/step_prune.rs
@@ -79,43 +79,6 @@ fn test_prune_removes_merged(mut repo: TestRepo) {
assert!(!worktree_path.exists(), "Worktree should be fully removed");
}
-/// Worktrees sharing a directory basename (Codex's `<id>/<repo>` layout) each
-/// stage into trash. The suite pins `epoch_now()`, so with basename-derived
-/// staging names the second rename would collide and fall back to a
-/// synchronous `git worktree remove`, which spawns no detached cleanup.
-#[rstest]
-fn test_prune_stages_worktrees_sharing_a_basename(mut repo: TestRepo) {
- repo.commit("initial");
- let parent = repo.root_path().parent().unwrap().to_path_buf();
- let worktrees = [
- ("one", parent.join("aaaa/repo")),
- ("two", parent.join("bbbb/repo")),
- ];
- for (branch, path) in &worktrees {
- repo.add_worktree_at_path(branch, path);
- }
-
- let output = repo
- .wt_command()
- .args(["step", "prune", "--yes", "--min-age=0s"])
- .output()
- .unwrap();
- assert!(
- output.status.success(),
- "{}",
- String::from_utf8_lossy(&output.stderr)
- );
-
- let logs = crate::common::resolve_git_common_dir(repo.root_path()).join("wt/logs");
- for (branch, path) in &worktrees {
- assert!(!path.exists(), "{branch} must be removed");
- assert!(
- logs.join(branch).join("internal/remove.log").exists(),
- "{branch} must be staged into trash with a detached cleanup"
- );
- }
-}
-
/// Prune skips worktrees with unique commits (not merged)
#[rstest]
fn test_prune_skips_unmerged(mut repo: TestRepo) {