use std::{fmt, fs::FileType, path::Path, sync::Arc};
use walkdir::WalkDir;
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum EntryKind {
Dir,
File,
Unknown,
}
impl From<FileType> for EntryKind {
fn from(file_type: FileType) -> Self {
if file_type.is_dir() {
EntryKind::Dir
} else {
EntryKind::File
}
}
}
type Ignored = dyn Fn(&Path, EntryKind) -> bool + Send + Sync;
#[derive(Clone, Default)]
pub struct IgnoreFilter(Option<Arc<Ignored>>);
impl IgnoreFilter {
pub fn new(ignored: impl Fn(&Path, EntryKind) -> bool + Send + Sync + 'static) -> Self {
Self(Some(Arc::new(ignored)))
}
#[must_use]
pub fn is_ignored(&self, path: &Path, kind: EntryKind) -> bool {
self.0.as_ref().is_some_and(|ignored| ignored(path, kind))
}
#[must_use]
pub fn is_path_ignored(&self, path: &Path) -> bool {
self.0.is_some() && {
let kind = path
.metadata()
.map_or(EntryKind::Unknown, |metadata| metadata.file_type().into());
self.is_ignored(path, kind)
}
}
pub(crate) fn walk(
&self,
walk_dir: WalkDir,
) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>> + use<> {
let filter = self.clone();
walk_dir.into_iter().filter_entry(move |entry| {
entry.depth() == 0 || !filter.is_ignored(entry.path(), entry.file_type().into())
})
}
}
impl fmt::Debug for IgnoreFilter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self.0 {
Some(_) => "IgnoreFilter(Some(..))",
None => "IgnoreFilter(None)",
})
}
}
#[cfg(all(test, not(target_family = "wasm")))]
mod tests {
use super::*;
use crate::test::testdir;
use std::{
fs,
path::PathBuf,
sync::{Arc, Mutex},
};
#[test]
fn default_filter_ignores_nothing() {
let filter = IgnoreFilter::default();
assert!(!filter.is_ignored(Path::new("/p/node_modules"), EntryKind::Dir));
assert!(!filter.is_path_ignored(Path::new("/p/node_modules/a.js")));
}
#[test]
fn is_path_ignored_looks_up_the_kind() {
let tmpdir = testdir();
let root = tmpdir.path();
fs::create_dir(root.join("dir")).expect("create dir");
fs::write(root.join("file"), "").expect("write");
let asked = Arc::new(Mutex::new(Vec::new()));
let filter = IgnoreFilter::new({
let asked = Arc::clone(&asked);
move |path, kind| {
asked.lock().unwrap().push((path.to_path_buf(), kind));
true
}
});
assert!(filter.is_path_ignored(&root.join("dir")));
assert!(filter.is_path_ignored(&root.join("file")));
assert!(filter.is_path_ignored(&root.join("missing")));
assert_eq!(
*asked.lock().unwrap(),
[
(root.join("dir"), EntryKind::Dir),
(root.join("file"), EntryKind::File),
(root.join("missing"), EntryKind::Unknown),
]
);
}
#[test]
fn walk_does_not_descend_into_ignored_directories() {
let tmpdir = testdir();
let root = tmpdir.path();
fs::create_dir_all(root.join("node_modules/pkg")).expect("create dir");
fs::write(root.join("node_modules/pkg/index.js"), "").expect("write");
fs::create_dir(root.join("src")).expect("create dir");
fs::write(root.join("src/index.js"), "").expect("write");
let asked = Arc::new(Mutex::new(Vec::new()));
let filter = IgnoreFilter::new({
let asked = Arc::clone(&asked);
move |path, _| {
asked.lock().unwrap().push(path.to_path_buf());
path.file_name().is_some_and(|name| name == "node_modules")
}
});
let mut walked: Vec<PathBuf> = filter
.walk(WalkDir::new(root))
.map(|entry| entry.expect("walk").into_path())
.collect();
walked.sort();
assert_eq!(
walked,
[
root.to_path_buf(),
root.join("src"),
root.join("src/index.js")
]
);
let mut asked = std::mem::take(&mut *asked.lock().unwrap());
asked.sort();
assert_eq!(
asked,
[
root.join("node_modules"),
root.join("src"),
root.join("src/index.js")
]
);
}
}
#[cfg(all(test, not(target_family = "wasm")))]
mod watcher_tests {
use crate::{RecommendedWatcher, RecursiveMode, TargetMode, WatchMode, Watcher, test::*};
use std::{fs, path::Path};
fn watcher() -> (TestWatcher<RecommendedWatcher>, Receiver) {
ignoring_channel()
}
fn event_paths(rx: &mut Receiver, root: &Path) -> Vec<std::path::PathBuf> {
let paths: Vec<_> = rx.iter().flat_map(|event| event.paths).collect();
for path in &paths {
let below_root = path.strip_prefix(root).unwrap_or(path);
assert!(
!is_ignored(below_root),
"ignored path was reported: {}",
path.display()
);
}
paths
}
#[test]
fn ignored_paths_are_not_reported() {
let tmpdir = testdir();
let root = tmpdir.path();
fs::create_dir_all(root.join("node_modules/pkg")).expect("create dir");
fs::create_dir(root.join("src")).expect("create dir");
fs::create_dir(root.join("lib")).expect("create dir");
let (mut watcher, mut rx) = watcher();
watcher.watch_recursively(root);
fs::write(root.join("node_modules/pkg/index.js"), "").expect("write");
fs::create_dir(root.join("src/node_modules")).expect("create dir");
fs::write(root.join("src/node_modules/index.js"), "").expect("write");
fs::write(root.join("src/debug.log"), "").expect("write");
fs::remove_file(root.join("src/debug.log")).expect("remove");
fs::remove_dir_all(root.join("src/node_modules")).expect("remove dir");
fs::write(root.join("lib/index.js"), "").expect("write");
let paths = event_paths(&mut rx, root);
assert!(paths.contains(&root.join("lib/index.js")), "{paths:#?}");
}
#[test]
fn rename_to_an_ignored_path_only_reports_the_other_path() {
let tmpdir = testdir();
let root = tmpdir.path();
fs::write(root.join("a.js"), "").expect("write");
fs::write(root.join("b.log"), "").expect("write");
let (mut watcher, mut rx) = watcher();
watcher.watch_recursively(root);
fs::rename(root.join("a.js"), root.join("a.log")).expect("rename");
fs::rename(root.join("b.log"), root.join("b.js")).expect("rename");
let paths = event_paths(&mut rx, root);
assert!(paths.contains(&root.join("a.js")), "{paths:#?}");
assert!(paths.contains(&root.join("b.js")), "{paths:#?}");
}
#[test]
fn watching_an_ignored_path_does_nothing() {
let tmpdir = testdir();
let root = tmpdir.path();
let pkg = root.join("node_modules/pkg");
fs::create_dir_all(&pkg).expect("create dir");
fs::write(root.join("debug.log"), "").expect("write");
let (mut watcher, mut rx) = watcher();
watcher.watch_recursively(root);
watcher.watch_recursively(root.join("node_modules"));
watcher.watch_recursively(&pkg);
watcher.watch_nonrecursively(root.join("debug.log"));
let no_track = WatchMode {
recursive_mode: RecursiveMode::NonRecursive,
target_mode: TargetMode::NoTrack,
};
watcher.watch(root.join("node_modules/missing"), no_track);
fs::write(pkg.join("index.js"), "").expect("write");
fs::write(root.join("debug.log"), "123").expect("write");
fs::write(root.join("index.js"), "").expect("write");
let paths = event_paths(&mut rx, root);
assert!(paths.contains(&root.join("index.js")), "{paths:#?}");
watcher.watcher.unwatch(&pkg).expect("unwatch");
watcher
.watcher
.unwatch(&root.join("debug.log"))
.expect("unwatch");
}
}