use std::path::Path;
pub fn is_safe_to_delete(path: &Path, targets: &[String]) -> bool {
let Some(base) = path.file_name().and_then(|n| n.to_str()) else {
return false;
};
targets.iter().any(|t| t == base)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_exact_target() {
let targets = vec!["node_modules".to_string()];
assert!(is_safe_to_delete(Path::new("/x/node_modules"), &targets));
}
#[test]
fn rejects_non_target_basename() {
let targets = vec!["node_modules".to_string()];
assert!(!is_safe_to_delete(Path::new("/x/.cache"), &targets));
}
#[test]
fn rejects_empty_path() {
let targets = vec!["node_modules".to_string()];
assert!(!is_safe_to_delete(Path::new(""), &targets));
}
#[test]
fn rejects_empty_targets() {
assert!(!is_safe_to_delete(Path::new("/x/node_modules"), &[]));
}
#[test]
fn matches_one_of_multiple_targets() {
let targets = vec!["venv".to_string(), "node_modules".to_string(), ".gradle".to_string()];
assert!(is_safe_to_delete(Path::new("/x/venv"), &targets));
assert!(is_safe_to_delete(Path::new("/x/.gradle"), &targets));
}
}