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:
parent
faadb05eb3
commit
5d49b57b9c
16 changed files with 2058 additions and 4 deletions
174
crates/vfs/tests/watch.rs
Normal file
174
crates/vfs/tests/watch.rs
Normal 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)
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue