use super::*;
#[test]
fn lists_entries_with_names_and_paths() {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::create_dir(tmp.path().join("alpha")).expect("mkdir");
std::fs::write(tmp.path().join("beta.txt"), "x").expect("write");
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list must succeed");
let names: Vec<&str> = listing.entries.iter().map(|e| e.name.as_str()).collect();
assert_eq!(names, vec!["alpha", "beta.txt"], "dirs sort before files");
assert!(listing.entries[0].is_dir);
assert!(!listing.entries[1].is_dir);
assert!(
listing.entries[0].path.ends_with("alpha"),
"entry path must be absolute: {}",
listing.entries[0].path
);
}
#[test]
fn entries_sort_dirs_first_then_case_insensitively() {
let tmp = tempfile::tempdir().expect("tempdir");
for d in ["Zebra", "apple"] {
std::fs::create_dir(tmp.path().join(d)).expect("mkdir");
}
std::fs::write(tmp.path().join("aaa.txt"), "x").expect("write");
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list must succeed");
let names: Vec<&str> = listing.entries.iter().map(|e| e.name.as_str()).collect();
assert_eq!(names, vec!["apple", "Zebra", "aaa.txt"]);
}
#[test]
fn hidden_entries_are_gated_by_include_hidden() {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::create_dir(tmp.path().join(".hidden")).expect("mkdir");
std::fs::create_dir(tmp.path().join("visible")).expect("mkdir");
let default = list_dir(&tmp.path().display().to_string(), false).expect("list");
assert_eq!(default.entries.len(), 1);
assert_eq!(default.entries[0].name, "visible");
let with_hidden = list_dir(&tmp.path().display().to_string(), true).expect("list");
assert_eq!(with_hidden.entries.len(), 2);
}
#[test]
fn git_dir_is_detected() {
let tmp = tempfile::tempdir().expect("tempdir");
let repo = tmp.path().join("acme-api");
std::fs::create_dir(&repo).expect("mkdir");
std::fs::create_dir(repo.join(".git")).expect("mkdir .git");
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list");
assert!(listing.entries[0].is_git_repo);
}
#[test]
fn linked_worktree_gitfile_is_detected() {
let tmp = tempfile::tempdir().expect("tempdir");
let worktree = tmp.path().join("feature-wt");
std::fs::create_dir(&worktree).expect("mkdir");
std::fs::write(
worktree.join(".git"),
"gitdir: /Users/dev/main/.git/worktrees/feature-wt\n",
)
.expect("write .git file");
assert!(
is_git_repo(&worktree),
"a linked worktree's `.git` FILE must count as a repo"
);
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list");
assert!(
listing.entries[0].is_git_repo,
"the worktree row must carry the git badge"
);
}
#[test]
fn real_linked_worktree_is_detected() {
let tmp = tempfile::tempdir().expect("tempdir");
let main = tmp.path().join("main");
std::fs::create_dir(&main).expect("mkdir");
let git = |args: &[&str], cwd: &std::path::Path| {
std::process::Command::new("git")
.arg("-C")
.arg(cwd)
.args(args)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
};
for args in [
&["init", "-q"][..],
&["config", "user.email", "t@example.com"][..],
&["config", "user.name", "t"][..],
&["commit", "-q", "--allow-empty", "-m", "init"][..],
] {
if !git(args, &main) {
return;
}
}
let wt = tmp.path().join("wt");
if !git(
&["worktree", "add", "-q", &wt.display().to_string(), "-d"],
&main,
) {
return;
}
assert!(
wt.join(".git").is_file(),
"precondition: git writes `.git` as a FILE in a linked worktree"
);
assert!(
is_git_repo(&wt),
"a real linked worktree must be detected as a repo"
);
}
#[test]
fn bogus_dot_git_file_is_not_a_repo() {
let tmp = tempfile::tempdir().expect("tempdir");
let dir = tmp.path().join("not-a-repo");
std::fs::create_dir(&dir).expect("mkdir");
std::fs::write(dir.join(".git"), "just some notes\n").expect("write");
assert!(!is_git_repo(&dir));
}
#[test]
fn large_dot_git_file_is_not_a_repo() {
let tmp = tempfile::tempdir().expect("tempdir");
let dir = tmp.path().join("not-a-repo");
std::fs::create_dir(&dir).expect("mkdir");
std::fs::write(dir.join(".git"), "x".repeat(10_000)).expect("write");
assert!(!is_git_repo(&dir));
}
#[test]
fn gitdir_pointer_at_exactly_the_read_bound() {
let tmp = tempfile::tempdir().expect("tempdir");
let dir = tmp.path().join("worktree-at-bound");
std::fs::create_dir(&dir).expect("mkdir");
let long_path = format!("/very/long/path/to/main/.git/worktrees/{}", "x".repeat(40));
std::fs::write(dir.join(".git"), format!("gitdir: {long_path}\n")).expect("write");
assert!(
is_git_repo(&dir),
"a gitdir: pointer must be detected even when the full line exceeds the read bound"
);
}
#[test]
fn non_git_dir_is_reported_as_non_git() {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::create_dir(tmp.path().join("scratch")).expect("mkdir");
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list must succeed");
assert_eq!(listing.entries[0].name, "scratch");
assert!(!listing.entries[0].is_git_repo);
}
#[test]
fn file_entries_are_never_git_repos() {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::write(tmp.path().join("readme.md"), "x").expect("write");
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list");
assert!(!listing.entries[0].is_dir);
assert!(!listing.entries[0].is_git_repo);
}
#[test]
fn listing_shape_supports_breadcrumb_and_badges() {
let tmp = tempfile::tempdir().expect("tempdir");
let repo = tmp.path().join("acme-api");
std::fs::create_dir(&repo).expect("mkdir");
std::fs::create_dir(repo.join(".git")).expect("mkdir");
std::fs::create_dir(tmp.path().join("scratch")).expect("mkdir");
let listing = list_dir(&tmp.path().display().to_string(), false).expect("list");
assert!(!listing.display_path.is_empty());
assert!(listing.display_path.contains('/'));
assert!(listing.parent.is_some());
let by_name = |n: &str| listing.entries.iter().find(|e| e.name == n).expect("row");
assert!(by_name("acme-api").is_git_repo);
assert!(!by_name("scratch").is_git_repo);
}
#[test]
fn parent_is_none_at_filesystem_root() {
let listing = list_dir("/", false).expect("root must list");
assert_eq!(listing.path, "/");
assert!(listing.parent.is_none());
}
#[test]
fn parent_round_trips_as_a_list_target() {
let tmp = tempfile::tempdir().expect("tempdir");
let child = tmp.path().join("child");
std::fs::create_dir(&child).expect("mkdir");
let listing = list_dir(&child.display().to_string(), false).expect("list child");
let parent = listing.parent.expect("child has a parent");
let up = list_dir(&parent, false).expect("parent must list");
assert!(up.entries.iter().any(|e| e.name == "child"));
}
#[test]
fn dotdot_segments_are_resolved() {
let tmp = tempfile::tempdir().expect("tempdir");
let child = tmp.path().join("child");
std::fs::create_dir(&child).expect("mkdir");
let listing = list_dir(&format!("{}/..", child.display()), false).expect("list");
let expected = std::fs::canonicalize(tmp.path()).expect("canonicalize");
assert_eq!(listing.path, expected.display().to_string());
}
#[test]
fn tilde_expands_to_home() {
let Some(home) = dirs::home_dir() else {
return;
};
assert_eq!(expand_tilde("~/code").expect("expand"), home.join("code"));
}
#[test]
fn tilde_bare_expands_to_home() {
let Some(home) = dirs::home_dir() else {
return;
};
assert_eq!(expand_tilde("~").expect("expand"), home);
}
#[test]
fn absolute_path_is_left_alone() {
assert_eq!(
expand_tilde("/abs/path").expect("expand"),
PathBuf::from("/abs/path")
);
assert_eq!(
expand_tilde("/tmp/we~ird").expect("expand"),
PathBuf::from("/tmp/we~ird")
);
}
#[test]
fn display_path_collapses_home_to_tilde() {
let Some(home) = dirs::home_dir() else {
return;
};
assert_eq!(collapse_home(&home), "~");
assert_eq!(collapse_home(&home.join("code")), "~/code");
assert_eq!(collapse_home(Path::new("/etc")), "/etc");
}
#[test]
fn tilde_path_lists_home() {
if dirs::home_dir().is_none() {
return;
}
let listing = list_dir("~", false).expect("home must list");
assert_eq!(listing.display_path, "~");
}
#[test]
fn nonexistent_path_is_not_found() {
let tmp = tempfile::tempdir().expect("tempdir");
let missing = tmp.path().join("nope");
let err = list_dir(&missing.display().to_string(), false).expect_err("must fail");
assert!(
matches!(err, ListDirError::NotFound(_)),
"expected NotFound, got {err:?}"
);
}
#[test]
fn file_path_is_not_a_directory() {
let tmp = tempfile::tempdir().expect("tempdir");
let file = tmp.path().join("a.txt");
std::fs::write(&file, "x").expect("write");
let err = list_dir(&file.display().to_string(), false).expect_err("must fail");
assert!(
matches!(err, ListDirError::NotADirectory(_)),
"expected NotADirectory, got {err:?}"
);
}
#[cfg(unix)]
#[test]
fn unreadable_dir_is_permission_denied() {
use std::os::unix::fs::PermissionsExt;
if unsafe { libc::geteuid() } == 0 {
return;
}
let tmp = tempfile::tempdir().expect("tempdir");
let locked = tmp.path().join("locked");
std::fs::create_dir(&locked).expect("mkdir");
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).expect("chmod 000");
let result = list_dir(&locked.display().to_string(), false);
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).expect("chmod 755");
let err = result.expect_err("an unreadable dir must fail");
assert!(
matches!(err, ListDirError::PermissionDenied(_)),
"expected PermissionDenied, got {err:?}"
);
}