use crate::config::DiscoveryConfig;
use glob::Pattern;
use log::warn; use std::path::Path;
pub fn check_process_last(relative_path: &Path, config: &DiscoveryConfig) -> (bool, Option<usize>) {
if let Some(ref last_patterns) = config.process_last {
for (index, pattern_str) in last_patterns.iter().enumerate() {
match Pattern::new(pattern_str) {
Ok(pattern) => {
if pattern.matches_path(relative_path) {
return (true, Some(index));
}
}
Err(e) => {
warn!(
"Invalid glob pattern in --last argument: '{}'. Error: {}",
pattern_str, e
);
}
}
}
}
(false, None)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::DiscoveryConfig;
use std::path::Path;
fn create_test_config(process_last: Option<Vec<&str>>) -> DiscoveryConfig {
let mut config = DiscoveryConfig::default_for_test();
config.process_last = process_last.map(|v| v.iter().map(|s| s.to_string()).collect());
config
}
#[test]
fn test_no_last_patterns() {
let config = create_test_config(None);
let rel_path = Path::new("src/main.rs");
assert_eq!(check_process_last(rel_path, &config), (false, None));
}
#[test]
fn test_match_exact_filename_glob() {
let config = create_test_config(Some(vec!["other.txt", "main.rs"]));
let rel_path = Path::new("src/main.rs"); assert_eq!(check_process_last(rel_path, &config), (false, None));
let rel_path_root = Path::new("main.rs"); assert_eq!(check_process_last(rel_path_root, &config), (true, Some(1)));
}
#[test]
fn test_match_wildcard_filename_glob() {
let config = create_test_config(Some(vec!["*.txt", "*.rs"]));
let rel_path = Path::new("src/main.rs");
assert_eq!(check_process_last(rel_path, &config), (true, Some(1)));
let rel_path_txt = Path::new("docs/README.txt");
assert_eq!(check_process_last(rel_path_txt, &config), (true, Some(0)));
}
#[test]
fn test_match_relative_path_glob() {
let config = create_test_config(Some(vec!["src/*.rs", "data/**/config.toml"]));
let rel_path = Path::new("src/lib.rs");
assert_eq!(check_process_last(rel_path, &config), (true, Some(0)));
let rel_path_toml = Path::new("data/prod/config.toml");
assert_eq!(check_process_last(rel_path_toml, &config), (true, Some(1)));
let rel_path_toml_deep = Path::new("data/dev/nested/config.toml");
assert_eq!(
check_process_last(rel_path_toml_deep, &config),
(true, Some(1))
);
}
#[test]
fn test_no_match_glob() {
let config = create_test_config(Some(vec!["lib.rs", "*.toml"]));
let rel_path = Path::new("src/main.rs");
assert_eq!(check_process_last(rel_path, &config), (false, None));
}
#[test]
fn test_invalid_glob_pattern() {
let config = create_test_config(Some(vec!["[invalid", "*.rs"])); let rel_path = Path::new("src/main.rs");
assert_eq!(check_process_last(rel_path, &config), (true, Some(1)));
let rel_path_other = Path::new("src/other.txt");
assert_eq!(check_process_last(rel_path_other, &config), (false, None));
}
}