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>
This commit is contained in:
danny 2026-10-14 19:23:54 +02:00
parent faadb05eb3
commit 5d49b57b9c
16 changed files with 2058 additions and 4 deletions

View file

@ -0,0 +1,37 @@
//! Helpers shared by the integration tests.
#![allow(dead_code, clippy::unwrap_used)]
use std::fs;
use std::path::Path;
use lutra_vfs::{GlobalExcludes, Project, ProjectOptions};
use tempfile::TempDir;
/// Creates a temporary directory with the given files; a path ending in `/`
/// creates a directory.
pub fn tree(paths: &[(&str, &str)]) -> TempDir {
let dir = tempfile::tempdir().unwrap();
for (path, contents) in paths {
let full = dir.path().join(path);
if path.ends_with('/') {
fs::create_dir_all(&full).unwrap();
} else {
fs::create_dir_all(full.parent().unwrap()).unwrap();
fs::write(&full, contents).unwrap();
}
}
dir
}
/// Opens `dir` without the machine's global git excludes, so tests don't
/// depend on the developer's configuration.
pub fn open(dir: &Path) -> Project {
Project::open_with(
dir,
ProjectOptions {
global_excludes: GlobalExcludes::None,
},
)
.unwrap()
}

193
crates/vfs/tests/io.rs Normal file
View file

@ -0,0 +1,193 @@
//! Reading with limits and decoding; atomic writes.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
mod common;
use std::fs;
use common::tree;
use lutra_vfs::{VfsError, read_bytes, read_text, read_text_with_limit, write_atomic, write_text};
#[test]
fn reads_utf8_with_and_without_bom() {
let dir = tree(&[("plain.php", "<?php echo 'é';")]);
let plain = read_text(&dir.path().join("plain.php")).unwrap();
assert_eq!(plain.text, "<?php echo 'é';");
assert!(!plain.bom);
let bom_path = dir.path().join("bom.php");
fs::write(&bom_path, b"\xEF\xBB\xBF<?php").unwrap();
let bom = read_text(&bom_path).unwrap();
assert_eq!(bom.text, "<?php");
assert!(bom.bom);
}
#[test]
fn invalid_utf8_is_a_typed_error() {
let dir = tree(&[]);
let path = dir.path().join("latin1.txt");
fs::write(&path, b"caf\xE9").unwrap();
assert_eq!(
read_text(&path),
Err(VfsError::InvalidUtf8 {
path: path.clone(),
valid_up_to: 3
})
);
fs::write(&path, b"\xEF\xBB\xBFab\xFF").unwrap();
assert!(matches!(
read_text(&path),
Err(VfsError::InvalidUtf8 { valid_up_to: 5, .. })
));
}
#[test]
fn utf16_is_reported_as_unsupported() {
let dir = tree(&[]);
let path = dir.path().join("wide.txt");
fs::write(&path, b"\xFF\xFEa\x00").unwrap();
assert!(matches!(
read_text(&path),
Err(VfsError::UnsupportedEncoding {
encoding: "UTF-16LE",
..
})
));
}
#[test]
fn reads_are_capped() {
let dir = tree(&[("big.txt", "0123456789")]);
let path = dir.path().join("big.txt");
assert_eq!(read_text_with_limit(&path, 10).unwrap().text, "0123456789");
assert_eq!(
read_text_with_limit(&path, 9),
Err(VfsError::TooLarge {
path: path.clone(),
size: 10,
limit: 9
})
);
assert!(matches!(
read_bytes(&dir.path().join("missing"), 10),
Err(VfsError::NotFound { .. })
));
}
#[test]
fn reading_a_directory_fails_cleanly() {
let dir = tree(&[("sub/", "")]);
assert!(read_text(&dir.path().join("sub")).is_err());
}
#[test]
fn atomic_write_replaces_contents_and_leaves_no_temp_files() {
let dir = tree(&[("a.php", "old contents that are longer")]);
let path = dir.path().join("a.php");
write_atomic(&path, b"new").unwrap();
assert_eq!(fs::read(&path).unwrap(), b"new");
write_text(&dir.path().join("created.php"), "x", true).unwrap();
assert_eq!(
fs::read(dir.path().join("created.php")).unwrap(),
b"\xEF\xBB\xBFx"
);
let mut names: Vec<_> = fs::read_dir(dir.path())
.unwrap()
.map(|e| e.unwrap().file_name().into_string().unwrap())
.collect();
names.sort();
assert_eq!(names, ["a.php", "created.php"]);
}
#[test]
fn atomic_write_refuses_read_only_files_and_directories() {
let dir = tree(&[("locked.php", "keep"), ("sub/", "")]);
let path = dir.path().join("locked.php");
let mut perms = fs::metadata(&path).unwrap().permissions();
perms.set_readonly(true);
fs::set_permissions(&path, perms.clone()).unwrap();
assert!(matches!(
write_atomic(&path, b"lost"),
Err(VfsError::ReadOnly { .. })
));
assert_eq!(fs::read(&path).unwrap(), b"keep");
#[allow(clippy::permissions_set_readonly_false)]
perms.set_readonly(false);
fs::set_permissions(&path, perms).unwrap();
assert!(matches!(
write_atomic(&dir.path().join("sub"), b"x"),
Err(VfsError::NotAFile { .. })
));
assert!(matches!(
write_atomic(&dir.path().join("missing").join("a.php"), b"x"),
Err(VfsError::NotFound { .. })
));
}
#[cfg(unix)]
#[test]
fn atomic_write_keeps_permissions() {
use std::os::unix::fs::PermissionsExt;
let dir = tree(&[("run.sh", "#!/bin/sh")]);
let path = dir.path().join("run.sh");
fs::set_permissions(&path, fs::Permissions::from_mode(0o751)).unwrap();
write_atomic(&path, b"#!/bin/sh\necho hi").unwrap();
let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o751);
}
#[cfg(unix)]
#[test]
fn atomic_write_through_a_symlink_keeps_the_link() {
let dir = tree(&[("real.php", "old")]);
let link = dir.path().join("link.php");
std::os::unix::fs::symlink(dir.path().join("real.php"), &link).unwrap();
write_atomic(&link, b"new").unwrap();
assert!(
fs::symlink_metadata(&link)
.unwrap()
.file_type()
.is_symlink()
);
assert_eq!(fs::read(dir.path().join("real.php")).unwrap(), b"new");
}
/// Decoding random bytes never panics, and valid UTF-8 round-trips with and
/// without a BOM. A small deterministic generator stands in for proptest.
#[test]
fn decode_never_panics_on_random_bytes() {
let mut state: u64 = 0x9E37_79B9_7F4A_7C15;
let mut next = move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
let path = std::path::Path::new("random.bin");
for _ in 0..5_000 {
let len = (next() % 64) as usize;
let mut bytes: Vec<u8> = (0..len).map(|_| next() as u8).collect();
if next() % 4 == 0 {
bytes.splice(0..0, [0xEF, 0xBB, 0xBF]);
}
match lutra_vfs::decode(path, bytes.clone()) {
Ok(file) => {
let mut again = if file.bom {
vec![0xEF, 0xBB, 0xBF]
} else {
Vec::new()
};
again.extend_from_slice(file.text.as_bytes());
assert_eq!(again, bytes);
}
Err(VfsError::InvalidUtf8 { valid_up_to, .. }) => assert!(valid_up_to < bytes.len()),
Err(VfsError::UnsupportedEncoding { .. }) => {}
Err(other) => panic!("unexpected error {other:?}"),
}
}
}

228
crates/vfs/tests/project.rs Normal file
View file

@ -0,0 +1,228 @@
//! 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());
}

174
crates/vfs/tests/watch.rs Normal file
View file

@ -0,0 +1,174 @@
//! The polling watcher. Every wait is bounded by a timeout on the channel.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
mod common;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, RecvTimeoutError};
use std::time::{Duration, Instant};
use common::{open, tree};
use lutra_vfs::{EntryKind, VfsEvent, WatchOptions};
const TIMEOUT: Duration = Duration::from_secs(10);
/// Receives events until one satisfies `want`, failing after [`TIMEOUT`].
/// Returns the events seen before it.
fn wait_for(rx: &Receiver<VfsEvent>, want: impl Fn(&VfsEvent) -> bool) -> Vec<VfsEvent> {
let deadline = Instant::now() + TIMEOUT;
let mut seen = Vec::new();
loop {
let left = deadline.saturating_duration_since(Instant::now());
match rx.recv_timeout(left) {
Ok(event) if want(&event) => return seen,
Ok(event) => seen.push(event),
Err(RecvTimeoutError::Timeout) => panic!("timed out; saw {seen:?}"),
Err(RecvTimeoutError::Disconnected) => panic!("watcher stopped; saw {seen:?}"),
}
}
}
fn is_created(path: &Path) -> impl Fn(&VfsEvent) -> bool + '_ {
move |e| matches!(e, VfsEvent::Created { path: p, .. } if p == path)
}
#[test]
fn reports_create_modify_rename_and_delete() {
let dir = tree(&[("keep.php", "")]);
let project = open(dir.path());
let (_watcher, rx) = project.watch().unwrap();
fs::write(dir.path().join("new.php"), "a").unwrap();
wait_for(&rx, is_created(Path::new("new.php")));
fs::write(dir.path().join("new.php"), "a longer body").unwrap();
wait_for(
&rx,
|e| matches!(e, VfsEvent::Modified { path } if path == Path::new("new.php")),
);
fs::rename(dir.path().join("new.php"), dir.path().join("moved.php")).unwrap();
wait_for(&rx, |e| {
*e == VfsEvent::Renamed {
from: PathBuf::from("new.php"),
to: PathBuf::from("moved.php"),
}
});
fs::remove_file(dir.path().join("moved.php")).unwrap();
wait_for(
&rx,
|e| matches!(e, VfsEvent::Removed { path } if path == Path::new("moved.php")),
);
}
#[test]
fn reports_directories_and_watched_subdirectories() {
let dir = tree(&[]);
let project = open(dir.path());
let (watcher, rx) = project.watch().unwrap();
fs::create_dir(dir.path().join("src")).unwrap();
wait_for(&rx, |e| {
*e == VfsEvent::Created {
path: PathBuf::from("src"),
kind: EntryKind::Directory,
}
});
assert!(watcher.watch_dir("src").unwrap());
assert_eq!(
watcher.watched_dirs(),
[PathBuf::new(), PathBuf::from("src")]
);
fs::write(dir.path().join("src").join("A.php"), "").unwrap();
let inner = Path::new("src").join("A.php");
wait_for(&rx, is_created(&inner));
watcher.unwatch_dir("src");
assert_eq!(watcher.watched_dirs(), [PathBuf::new()]);
}
#[test]
fn ignored_hidden_and_excluded_directories_are_not_watched() {
let dir = tree(&[
(".gitignore", "target/\n"),
("target/", ""),
("node_modules/", ""),
(".git/", ""),
("src/", ""),
]);
let project = open(dir.path());
let (watcher, rx) = project.watch().unwrap();
assert!(!watcher.watch_dir("target").unwrap());
assert!(!watcher.watch_dir("node_modules").unwrap());
assert!(!watcher.watch_dir(".git").unwrap());
assert!(watcher.watch_dir("src").unwrap());
fs::write(dir.path().join("target").join("out.bin"), "x").unwrap();
fs::write(dir.path().join("node_modules").join("m.js"), "x").unwrap();
fs::write(dir.path().join(".git").join("index"), "x").unwrap();
fs::write(dir.path().join("src").join("marker.php"), "x").unwrap();
let marker = Path::new("src").join("marker.php");
let before = wait_for(&rx, is_created(&marker));
for event in &before {
let text = format!("{event:?}");
assert!(
!text.contains("target") && !text.contains("node_modules") && !text.contains(".git"),
"unexpected event {event:?}"
);
}
}
#[test]
fn changes_within_one_interval_are_coalesced() {
let dir = tree(&[]);
let project = open(dir.path());
let (_watcher, rx) = project
.watch_with(WatchOptions {
interval: Duration::from_secs(2),
})
.unwrap();
let start = Instant::now();
fs::write(dir.path().join("flash.tmp"), "x").unwrap();
fs::remove_file(dir.path().join("flash.tmp")).unwrap();
fs::write(dir.path().join("marker.php"), "x").unwrap();
let before = wait_for(&rx, is_created(Path::new("marker.php")));
if start.elapsed() < Duration::from_secs(2) {
assert!(before.is_empty(), "flash.tmp leaked: {before:?}");
}
}
#[test]
fn losing_the_root_degrades_to_an_error_event() {
let parent = tree(&[("project/a.php", "")]);
let root = parent.path().join("project");
let project = open(&root);
let (_watcher, rx) = project.watch().unwrap();
fs::remove_dir_all(&root).unwrap();
wait_for(&rx, |e| matches!(e, VfsEvent::Failed(_)));
let deadline = Instant::now() + TIMEOUT;
loop {
match rx.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
Ok(_) => continue,
Err(RecvTimeoutError::Disconnected) => break,
Err(RecvTimeoutError::Timeout) => panic!("watcher kept running"),
}
}
}
#[test]
fn dropping_the_watcher_closes_the_channel() {
let dir = tree(&[]);
let project = open(dir.path());
let (watcher, rx) = project.watch().unwrap();
drop(watcher);
assert_eq!(
rx.recv_timeout(TIMEOUT),
Err(RecvTimeoutError::Disconnected)
);
}