#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(clippy::pedantic)]
#![cfg_attr(
not(test),
deny(
clippy::unwrap_used,
clippy::expect_used,
clippy::todo,
clippy::unimplemented,
clippy::panic
)
)]
#![allow(
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::missing_errors_doc
)]
#[cfg(feature = "archive")]
pub mod archive;
pub mod codewalker;
pub mod detect;
pub mod error;
pub mod filter;
#[cfg(feature = "gitignore")]
mod gitignore_ctx;
mod iter;
pub mod probe;
pub mod sandbox;
mod sort;
mod walk_common;
mod walker;
mod worker;
pub use codewalker::{CodeWalker, FileContent, FileContentChunks, FileEntry, WalkConfig};
pub use filter::{CompiledFilter, FileFilter};
pub use iter::WalkItemIter;
pub use sort::SortMode;
pub use walker::{WalkedFile, Walker, MAX_WALK_PATH_BYTES};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WalkOp {
Metadata,
ReadDir,
Open,
Gitignore,
}
impl std::fmt::Display for WalkOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
WalkOp::Metadata => "metadata",
WalkOp::ReadDir => "read_dir",
WalkOp::Open => "open",
WalkOp::Gitignore => "gitignore",
})
}
}
#[derive(Debug)]
pub struct WalkError {
pub path: std::path::PathBuf,
pub op: WalkOp,
pub source: std::io::Error,
}
impl WalkError {
#[must_use]
pub fn new(path: std::path::PathBuf, op: WalkOp, source: std::io::Error) -> Self {
Self { path, op, source }
}
fn fix_hint(err: &std::io::Error) -> &'static str {
#[cfg(unix)]
if err.raw_os_error() == Some(libc::ENAMETOOLONG) {
return "Fix: shorten the path; the OS reported ENAMETOOLONG.";
}
match err.kind() {
std::io::ErrorKind::PermissionDenied => {
"Fix: grant read and search (execute on directories) permission, or exclude this path."
}
std::io::ErrorKind::NotFound => {
"Fix: the path disappeared during the walk (concurrent delete/move); retry."
}
std::io::ErrorKind::NotADirectory => {
"Fix: a path component is not a directory; check symlinks and types."
}
std::io::ErrorKind::InvalidInput => {
"Fix: invalid path or walkkit limit (see error message)."
}
std::io::ErrorKind::Interrupted => "Fix: retry; the operation was interrupted.",
_ => "Fix: inspect the underlying OS error and remediate I/O conditions.",
}
}
}
impl std::fmt::Display for WalkError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} on {} during {} - {}",
self.source,
self.path.display(),
self.op,
Self::fix_hint(&self.source)
)
}
}
impl std::error::Error for WalkError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
#[derive(Debug)]
pub enum WalkItem {
File(WalkedFile),
Error(WalkError),
}
impl WalkItem {
#[must_use]
pub fn into_file(self) -> Option<WalkedFile> {
match self {
Self::File(f) => Some(f),
Self::Error(_) => None,
}
}
#[must_use]
pub fn as_file(&self) -> Option<&WalkedFile> {
match self {
Self::File(f) => Some(f),
Self::Error(_) => None,
}
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("Fix: check the glob pattern for syntax errors. Invalid glob pattern: {source}")]
InvalidGlob {
#[from]
source: globset::Error,
},
#[error(
"Fix: remove empty patterns and null bytes from filter configuration. Invalid filter pattern: {message}"
)]
InvalidFilterPattern {
message: String,
},
}
#[cfg(test)]
#[allow(
clippy::all,
clippy::pedantic,
clippy::nursery,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic
)]
mod walkkit_tests {
use super::*;
use tempfile::tempdir;
fn files_only(items: Vec<WalkItem>) -> Vec<WalkedFile> {
items.into_iter().filter_map(WalkItem::into_file).collect()
}
#[test]
fn walker_empty_directory() {
let dir = tempdir().unwrap();
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert!(files.is_empty(), "empty dir should produce 0 files");
}
#[test]
fn walker_single_file() {
let dir = tempdir().unwrap();
std::fs::write(dir.path().join("test.txt"), "content").unwrap();
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert_eq!(files.len(), 1);
assert!(files[0].path.ends_with("test.txt"));
}
#[test]
fn walker_multiple_files() {
let dir = tempdir().unwrap();
for i in 0..5 {
std::fs::write(dir.path().join(format!("f{i}.js")), "content").unwrap();
}
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert_eq!(files.len(), 5);
}
#[test]
fn walker_nested_directories() {
let dir = tempdir().unwrap();
let nested = dir.path().join("a/b/c");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("deep.js"), "deep content").unwrap();
std::fs::write(dir.path().join("top.js"), "top content").unwrap();
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert_eq!(files.len(), 2, "should find files in nested dirs");
}
#[test]
fn walker_file_size_reported() {
let dir = tempdir().unwrap();
std::fs::write(dir.path().join("sized.txt"), "12345").unwrap();
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert_eq!(files.len(), 1);
assert_eq!(files[0].size, 5, "file size should be 5 bytes");
}
#[test]
fn walker_hidden_files_included() {
let dir = tempdir().unwrap();
std::fs::write(dir.path().join(".hidden"), "secret").unwrap();
std::fs::write(dir.path().join("visible"), "open").unwrap();
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert!(!files.is_empty(), "at least visible file should be found");
}
#[test]
fn walker_parallelism_setting() {
let dir = tempdir().unwrap();
std::fs::write(dir.path().join("a.txt"), "a").unwrap();
let walker = Walker::new().add_root(dir.path()).with_parallelism(1);
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert_eq!(files.len(), 1);
}
#[test]
fn walker_nonexistent_root() {
let walker = Walker::new().add_root("/tmp/nonexistent_walkkit_test_12345");
let items: Vec<_> = walker.walk().unwrap().collect();
let file_count = items
.iter()
.filter(|i| matches!(i, WalkItem::File(_)))
.count();
assert_eq!(file_count, 0, "nonexistent root should produce 0 files");
assert!(
items.iter().any(|i| matches!(i, WalkItem::Error(_))),
"nonexistent root should surface a traversal error"
);
}
#[test]
fn walker_symlink_not_followed_by_default() {
let dir = tempdir().unwrap();
let target = tempdir().unwrap();
std::fs::write(target.path().join("target.txt"), "data").unwrap();
#[cfg(unix)]
std::os::unix::fs::symlink(target.path(), dir.path().join("link")).ok();
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert!(files.len() <= 1);
}
#[test]
fn walker_many_files_no_oom() {
let dir = tempdir().unwrap();
for i in 0..500 {
std::fs::write(dir.path().join(format!("file{i:04}.txt")), "x").unwrap();
}
let walker = Walker::new().add_root(dir.path());
let files: Vec<_> = files_only(walker.walk().unwrap().collect());
assert_eq!(files.len(), 500, "should find all 500 files");
}
}