Lutra/crates/vfs/tests/project.rs
danny 5d49b57b9c M1.2: vfs with ignore rules, polling watcher and safe file IO
- Project::open canonicalises the root (no \?\ prefix on Windows where
  safe); typed errors for missing and non-directory paths.
- Lazy children(dir): dirs first, case-insensitive; kind, ignored flag
  and role (vendor/ library, node_modules/ excluded, .git hidden).
- Ignore rules via the ignore crate: .gitignore, .ignore,
  .git/info/exclude, global git excludes, with git precedence.
- Watcher: std-only polling (100 ms, watched dirs only) with
  create/modify/remove/rename events and a Failed event on root loss.
  notify is CC0-1.0 and fails cargo deny; owner decision pending.
- read_text with a 32 MiB cap, UTF-8 + BOM, typed InvalidUtf8 and
  UnsupportedEncoding; write_atomic via temp file + rename, keeping
  permissions and refusing read-only files.
- 28 tests on temp dirs (30 on Unix: permission and symlink write tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 19:24:02 +02:00

228 lines
7.6 KiB
Rust

//! Opening projects, listing order and ignore rules.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
mod common;
use std::fs;
use std::path::{Path, PathBuf};
use common::{open, tree};
use lutra_vfs::{Entry, EntryKind, EntryRole, GlobalExcludes, Project, ProjectOptions, VfsError};
fn names(entries: &[Entry]) -> Vec<&str> {
entries.iter().map(|e| e.name.as_str()).collect()
}
fn entry<'a>(entries: &'a [Entry], name: &str) -> &'a Entry {
entries.iter().find(|e| e.name == name).unwrap()
}
#[test]
fn open_canonicalises_the_root() {
let dir = tree(&[("src/", "")]);
let project = open(&dir.path().join("src").join(".."));
assert!(project.root().is_absolute());
assert_eq!(project.root(), open(dir.path()).root());
assert!(!project.root().to_string_lossy().starts_with(r"\\?\"));
}
#[test]
fn open_rejects_missing_and_non_directory_paths() {
let dir = tree(&[("file.txt", "x")]);
let missing = Project::open(dir.path().join("nope"));
assert!(matches!(missing, Err(VfsError::NotFound { .. })));
let file = Project::open(dir.path().join("file.txt"));
assert!(matches!(file, Err(VfsError::NotADirectory { .. })));
}
#[test]
fn children_sorts_directories_first_then_case_insensitive() {
let dir = tree(&[
("b.php", ""),
("A.php", ""),
("c.php", ""),
("zeta/", ""),
("Alpha/", ""),
("beta/", ""),
]);
let listing = open(dir.path()).children("").unwrap();
assert_eq!(
names(&listing.entries),
["Alpha", "beta", "zeta", "A.php", "b.php", "c.php"]
);
assert!(!listing.truncated);
let alpha = entry(&listing.entries, "Alpha");
assert_eq!(alpha.kind, EntryKind::Directory);
assert_eq!(alpha.path, PathBuf::from("Alpha"));
assert_eq!(entry(&listing.entries, "b.php").kind, EntryKind::File);
}
#[test]
fn children_of_a_subdirectory_have_relative_paths() {
let dir = tree(&[("src/Controller/Home.php", "")]);
let listing = open(dir.path()).children("src").unwrap();
assert_eq!(names(&listing.entries), ["Controller"]);
assert_eq!(listing.entries[0].path, Path::new("src").join("Controller"));
let inner = open(dir.path()).children("src/Controller").unwrap();
assert_eq!(
inner.entries[0].path,
Path::new("src").join("Controller").join("Home.php")
);
}
#[test]
fn children_rejects_bad_paths() {
let dir = tree(&[("a.php", "")]);
let project = open(dir.path());
assert!(matches!(
project.children("../"),
Err(VfsError::OutsideProject { .. })
));
assert!(matches!(
project.children("missing"),
Err(VfsError::NotFound { .. })
));
assert!(matches!(
project.children("a.php"),
Err(VfsError::NotADirectory { .. })
));
}
#[test]
fn gitignore_marks_entries_and_their_descendants() {
let dir = tree(&[
(".gitignore", "*.log\n/build/\n!keep.log\n"),
("app.log", ""),
("keep.log", ""),
("index.php", ""),
("build/out/a.txt", ""),
("src/build/b.txt", ""),
]);
let project = open(dir.path());
let root = project.children("").unwrap().entries;
assert!(entry(&root, "app.log").ignored);
assert!(!entry(&root, "keep.log").ignored);
assert!(!entry(&root, "index.php").ignored);
assert!(entry(&root, "build").ignored);
assert!(!entry(&root, ".gitignore").ignored);
let build = project.children("build").unwrap().entries;
assert!(entry(&build, "out").ignored);
let deep = project.children("build/out").unwrap().entries;
assert!(entry(&deep, "a.txt").ignored);
let src = project.children("src").unwrap().entries;
assert!(
!entry(&src, "build").ignored,
"anchored pattern only at root"
);
}
#[test]
fn nested_gitignore_and_dot_ignore_take_precedence() {
let dir = tree(&[
(".gitignore", "*.cache\n"),
("sub/.gitignore", "!local.cache\n"),
("sub/local.cache", ""),
("sub/other.cache", ""),
(".ignore", "notes.md\n"),
("notes.md", ""),
("tool/.gitignore", "gen.php\n"),
("tool/.ignore", "!gen.php\n"),
("tool/gen.php", ""),
]);
let project = open(dir.path());
let sub = project.children("sub").unwrap().entries;
assert!(!entry(&sub, "local.cache").ignored);
assert!(entry(&sub, "other.cache").ignored);
let root = project.children("").unwrap().entries;
assert!(entry(&root, "notes.md").ignored);
let tool = project.children("tool").unwrap().entries;
assert!(!entry(&tool, "gen.php").ignored, ".ignore beats .gitignore");
}
#[test]
fn ignore_rule_edits_apply_to_the_next_listing() {
let dir = tree(&[("a.tmp", "")]);
let project = open(dir.path());
assert!(!project.children("").unwrap().entries[0].ignored);
fs::write(dir.path().join(".gitignore"), "*.tmp\n").unwrap();
let root = project.children("").unwrap().entries;
assert!(entry(&root, "a.tmp").ignored);
}
#[test]
fn info_exclude_and_global_excludes_apply() {
let dir = tree(&[
(".git/info/exclude", "secret.txt\n"),
("secret.txt", ""),
("global.bak", ""),
("plain.txt", ""),
]);
let global = tempfile::NamedTempFile::new().unwrap();
fs::write(global.path(), "*.bak\n").unwrap();
let project = Project::open_with(
dir.path(),
ProjectOptions {
global_excludes: GlobalExcludes::File(global.path().to_path_buf()),
},
)
.unwrap();
let root = project.children("").unwrap().entries;
assert!(entry(&root, "secret.txt").ignored);
assert!(entry(&root, "global.bak").ignored);
assert!(!entry(&root, "plain.txt").ignored);
}
#[test]
fn lutra_defaults_hide_git_and_mark_vendor_and_node_modules() {
let dir = tree(&[
(".git/HEAD", "ref"),
("vendor/acme/lib/A.php", ""),
("vendor/acme/node_modules/x.js", ""),
("node_modules/pkg/index.js", ""),
("web/node_modules/y.js", ""),
("src/vendor/", ""),
("src/sub/.git", "gitdir: elsewhere"),
]);
let project = open(dir.path());
let root = project.children("").unwrap().entries;
assert_eq!(names(&root), ["node_modules", "src", "vendor", "web"]);
assert_eq!(entry(&root, "vendor").role, EntryRole::Library);
assert!(!entry(&root, "vendor").ignored);
assert_eq!(entry(&root, "node_modules").role, EntryRole::Excluded);
assert_eq!(entry(&root, "src").role, EntryRole::Normal);
let acme = project.children("vendor/acme").unwrap().entries;
assert_eq!(entry(&acme, "lib").role, EntryRole::Library);
assert_eq!(entry(&acme, "node_modules").role, EntryRole::Excluded);
let pkg = project.children("node_modules/pkg").unwrap().entries;
assert_eq!(entry(&pkg, "index.js").role, EntryRole::Excluded);
let web = project.children("web").unwrap().entries;
assert_eq!(entry(&web, "node_modules").role, EntryRole::Excluded);
let src = project.children("src").unwrap().entries;
assert_eq!(entry(&src, "vendor").role, EntryRole::Normal);
assert!(project.children("src/sub").unwrap().entries.is_empty());
assert!(project.status(".git/HEAD").unwrap().hidden);
}
#[test]
fn resolve_and_relative_round_trip() {
let dir = tree(&[("src/a.php", "")]);
let project = open(dir.path());
let abs = project.resolve("src/a.php").unwrap();
assert!(abs.is_file());
assert_eq!(
project.relative(&abs).unwrap(),
Path::new("src").join("a.php")
);
assert!(
project
.relative(std::env::temp_dir().join("elsewhere"))
.is_none()
);
assert!(project.resolve("../x").is_err());
}