use std::collections::BTreeSet;
use std::path::PathBuf;
const MARKER: &str = "nested_worktree_probe_746";
pub fn fixture() -> (tempfile::TempDir, crate::git_probe::NestedWorktreeFixture) {
let dir = tempfile::tempdir().unwrap();
let fixture = crate::git_probe::init_repo_with_nested_worktree_for_test(dir.path(), MARKER);
(dir, fixture)
}
pub fn relative_set(
root: &std::path::Path,
paths: impl IntoIterator<Item = PathBuf>,
) -> BTreeSet<String> {
paths
.into_iter()
.map(|path| {
path.strip_prefix(root)
.unwrap_or_else(|_| panic!("{} is outside {}", path.display(), root.display()))
.to_string_lossy()
.replace('\\', "/")
})
.collect()
}
#[test]
fn boundary_marks_only_this_repositories_linked_worktrees() {
let (_dir, fixture) = fixture();
let root = &fixture.root;
let boundary = crate::project_content::NestedWorktreeBoundary::for_root(root);
assert!(
boundary.is_nested_worktree_root(&fixture.worktree),
"linked worktree not detected"
);
assert!(boundary.is_nested_worktree_root(&fixture.worktree));
assert!(
!boundary.is_nested_worktree_root(root),
"project root is never a worktree"
);
assert!(
!boundary.is_nested_worktree_root(&fixture.independent),
"nested independent repository must still be walked"
);
assert!(
!boundary.is_nested_worktree_root(&fixture.submodule),
"submodule-style gitdir must still be walked"
);
assert!(!boundary.is_nested_worktree_root(&root.join("src")));
assert!(boundary.is_under_nested_worktree(&fixture.worktree.join("src/main.rs")));
assert!(boundary.is_under_nested_worktree(&fixture.worktree));
assert!(!boundary.is_under_nested_worktree(&root.join("src/main.rs")));
assert!(!boundary.is_under_nested_worktree(root));
assert!(!boundary.is_under_nested_worktree(&fixture.independent.join("inner.rs")));
assert!(!boundary.is_under_nested_worktree(&fixture.submodule.join("code.rs")));
}
#[test]
fn boundary_ignores_a_worktree_of_another_repository() {
let (_dir, fixture) = fixture();
let root = &fixture.root;
let foreign = root.join("foreign");
let foreign_admin = root.join("elsewhere.git").join("worktrees").join("foreign");
std::fs::create_dir_all(&foreign_admin).unwrap();
std::fs::create_dir_all(&foreign).unwrap();
std::fs::write(
foreign.join(".git"),
format!("gitdir: {}\n", foreign_admin.display()),
)
.unwrap();
assert!(
!crate::project_content::NestedWorktreeBoundary::for_root(root)
.is_nested_worktree_root(&foreign),
"foreign worktrees entry must still be walked"
);
}