|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use glob::glob; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +/// Characters that indicate a path contains glob pattern metacharacters. |
| 8 | +const GLOB_METACHARACTERS: &[char] = &['*', '?', '[', ']']; |
| 9 | + |
| 10 | +/// Checks whether a path string contains glob metacharacters. |
| 11 | +/// |
| 12 | +/// # Examples |
| 13 | +/// - `"/home/user/*"` → `true` |
| 14 | +/// - `"/home/user/envs"` → `false` |
| 15 | +/// - `"**/*.py"` → `true` |
| 16 | +/// - `"/home/user/[abc]"` → `true` |
| 17 | +pub fn is_glob_pattern(path: &str) -> bool { |
| 18 | + path.contains(GLOB_METACHARACTERS) |
| 19 | +} |
| 20 | + |
| 21 | +/// Expands a single glob pattern to matching paths. |
| 22 | +/// |
| 23 | +/// If the path does not contain glob metacharacters, returns it unchanged (if it exists) |
| 24 | +/// or as-is (to let downstream code handle non-existent paths). |
| 25 | +/// |
| 26 | +/// If the path is a glob pattern, expands it and returns all matching paths. |
| 27 | +/// Pattern errors and unreadable paths are logged and skipped. |
| 28 | +/// |
| 29 | +/// # Examples |
| 30 | +/// - `"/home/user/envs"` → `["/home/user/envs"]` |
| 31 | +/// - `"/home/user/*/venv"` → `["/home/user/project1/venv", "/home/user/project2/venv"]` |
| 32 | +/// - `"**/.venv"` → All `.venv` directories recursively |
| 33 | +pub fn expand_glob_pattern(pattern: &str) -> Vec<PathBuf> { |
| 34 | + if !is_glob_pattern(pattern) { |
| 35 | + // Not a glob pattern, return as-is |
| 36 | + return vec![PathBuf::from(pattern)]; |
| 37 | + } |
| 38 | + |
| 39 | + match glob(pattern) { |
| 40 | + Ok(paths) => { |
| 41 | + let mut result = Vec::new(); |
| 42 | + for entry in paths { |
| 43 | + match entry { |
| 44 | + Ok(path) => result.push(path), |
| 45 | + Err(e) => { |
| 46 | + log::debug!("Failed to read glob entry: {}", e); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + if result.is_empty() { |
| 51 | + log::debug!("Glob pattern '{}' matched no paths", pattern); |
| 52 | + } |
| 53 | + result |
| 54 | + } |
| 55 | + Err(e) => { |
| 56 | + log::warn!("Invalid glob pattern '{}': {}", pattern, e); |
| 57 | + Vec::new() |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Expands a list of paths, where each path may be a glob pattern. |
| 63 | +/// |
| 64 | +/// Non-glob paths are passed through as-is. |
| 65 | +/// Glob patterns are expanded to all matching paths. |
| 66 | +/// Duplicate paths are preserved (caller should deduplicate if needed). |
| 67 | +/// |
| 68 | +/// # Examples |
| 69 | +/// ```ignore |
| 70 | +/// let paths = vec![ |
| 71 | +/// PathBuf::from("/home/user/project"), |
| 72 | +/// PathBuf::from("/home/user/*/venv"), |
| 73 | +/// ]; |
| 74 | +/// let expanded = expand_glob_patterns(&paths); |
| 75 | +/// // expanded contains "/home/user/project" plus all matching venv dirs |
| 76 | +/// ``` |
| 77 | +pub fn expand_glob_patterns(paths: &[PathBuf]) -> Vec<PathBuf> { |
| 78 | + let mut result = Vec::new(); |
| 79 | + for path in paths { |
| 80 | + let path_str = path.to_string_lossy(); |
| 81 | + let expanded = expand_glob_pattern(&path_str); |
| 82 | + result.extend(expanded); |
| 83 | + } |
| 84 | + result |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use super::*; |
| 90 | + use std::fs; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_is_glob_pattern_with_asterisk() { |
| 94 | + assert!(is_glob_pattern("/home/user/*")); |
| 95 | + assert!(is_glob_pattern("**/*.py")); |
| 96 | + assert!(is_glob_pattern("*.txt")); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_is_glob_pattern_with_question_mark() { |
| 101 | + assert!(is_glob_pattern("/home/user/file?.txt")); |
| 102 | + assert!(is_glob_pattern("test?")); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_is_glob_pattern_with_brackets() { |
| 107 | + assert!(is_glob_pattern("/home/user/[abc]")); |
| 108 | + assert!(is_glob_pattern("file[0-9].txt")); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_is_glob_pattern_no_metacharacters() { |
| 113 | + assert!(!is_glob_pattern("/home/user/envs")); |
| 114 | + assert!(!is_glob_pattern("simple_path")); |
| 115 | + assert!(!is_glob_pattern("/usr/local/bin/python3")); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_expand_non_glob_path() { |
| 120 | + let path = "/some/literal/path"; |
| 121 | + let result = expand_glob_pattern(path); |
| 122 | + assert_eq!(result.len(), 1); |
| 123 | + assert_eq!(result[0], PathBuf::from(path)); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_expand_glob_pattern_no_matches() { |
| 128 | + let pattern = "/this/path/definitely/does/not/exist/*"; |
| 129 | + let result = expand_glob_pattern(pattern); |
| 130 | + assert!(result.is_empty()); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_expand_glob_pattern_with_matches() { |
| 135 | + // Create temp directories for testing |
| 136 | + let temp_dir = std::env::temp_dir().join("pet_glob_test"); |
| 137 | + let _ = fs::remove_dir_all(&temp_dir); |
| 138 | + fs::create_dir_all(temp_dir.join("project1")).unwrap(); |
| 139 | + fs::create_dir_all(temp_dir.join("project2")).unwrap(); |
| 140 | + fs::create_dir_all(temp_dir.join("other")).unwrap(); |
| 141 | + |
| 142 | + let pattern = format!("{}/project*", temp_dir.to_string_lossy()); |
| 143 | + let result = expand_glob_pattern(&pattern); |
| 144 | + |
| 145 | + assert_eq!(result.len(), 2); |
| 146 | + assert!(result.iter().any(|p| p.ends_with("project1"))); |
| 147 | + assert!(result.iter().any(|p| p.ends_with("project2"))); |
| 148 | + assert!(!result.iter().any(|p| p.ends_with("other"))); |
| 149 | + |
| 150 | + // Cleanup |
| 151 | + let _ = fs::remove_dir_all(&temp_dir); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_expand_glob_patterns_mixed() { |
| 156 | + let temp_dir = std::env::temp_dir().join("pet_glob_test_mixed"); |
| 157 | + let _ = fs::remove_dir_all(&temp_dir); |
| 158 | + fs::create_dir_all(temp_dir.join("dir1")).unwrap(); |
| 159 | + fs::create_dir_all(temp_dir.join("dir2")).unwrap(); |
| 160 | + |
| 161 | + let paths = vec![ |
| 162 | + PathBuf::from("/literal/path"), |
| 163 | + PathBuf::from(format!("{}/dir*", temp_dir.to_string_lossy())), |
| 164 | + ]; |
| 165 | + |
| 166 | + let result = expand_glob_patterns(&paths); |
| 167 | + |
| 168 | + // Should have literal path + 2 expanded directories |
| 169 | + assert_eq!(result.len(), 3); |
| 170 | + assert!(result.contains(&PathBuf::from("/literal/path"))); |
| 171 | + |
| 172 | + // Cleanup |
| 173 | + let _ = fs::remove_dir_all(&temp_dir); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn test_expand_glob_pattern_recursive() { |
| 178 | + // Create nested temp directories for testing ** |
| 179 | + let temp_dir = std::env::temp_dir().join("pet_glob_test_recursive"); |
| 180 | + let _ = fs::remove_dir_all(&temp_dir); |
| 181 | + fs::create_dir_all(temp_dir.join("a/b/.venv")).unwrap(); |
| 182 | + fs::create_dir_all(temp_dir.join("c/.venv")).unwrap(); |
| 183 | + fs::create_dir_all(temp_dir.join(".venv")).unwrap(); |
| 184 | + |
| 185 | + let pattern = format!("{}/**/.venv", temp_dir.to_string_lossy()); |
| 186 | + let result = expand_glob_pattern(&pattern); |
| 187 | + |
| 188 | + // Should find .venv at multiple levels (behavior depends on glob crate version) |
| 189 | + assert!(!result.is_empty()); |
| 190 | + assert!(result.iter().all(|p| p.ends_with(".venv"))); |
| 191 | + |
| 192 | + // Cleanup |
| 193 | + let _ = fs::remove_dir_all(&temp_dir); |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn test_expand_glob_pattern_filename_patterns() { |
| 198 | + // Create temp files for testing filename patterns like python_* and python.* |
| 199 | + let temp_dir = std::env::temp_dir().join("pet_glob_test_filenames"); |
| 200 | + let _ = fs::remove_dir_all(&temp_dir); |
| 201 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 202 | + |
| 203 | + // Create files matching python_* pattern |
| 204 | + fs::write(temp_dir.join("python_foo"), "").unwrap(); |
| 205 | + fs::write(temp_dir.join("python_bar"), "").unwrap(); |
| 206 | + fs::write(temp_dir.join("python_3.12"), "").unwrap(); |
| 207 | + fs::write(temp_dir.join("other_file"), "").unwrap(); |
| 208 | + |
| 209 | + // Test python_* pattern |
| 210 | + let pattern = format!("{}/python_*", temp_dir.to_string_lossy()); |
| 211 | + let result = expand_glob_pattern(&pattern); |
| 212 | + |
| 213 | + assert_eq!(result.len(), 3); |
| 214 | + assert!(result.iter().any(|p| p.ends_with("python_foo"))); |
| 215 | + assert!(result.iter().any(|p| p.ends_with("python_bar"))); |
| 216 | + assert!(result.iter().any(|p| p.ends_with("python_3.12"))); |
| 217 | + assert!(!result.iter().any(|p| p.ends_with("other_file"))); |
| 218 | + |
| 219 | + // Create files matching python.* pattern |
| 220 | + fs::write(temp_dir.join("python.exe"), "").unwrap(); |
| 221 | + fs::write(temp_dir.join("python.sh"), "").unwrap(); |
| 222 | + fs::write(temp_dir.join("pythonrc"), "").unwrap(); |
| 223 | + |
| 224 | + // Test python.* pattern |
| 225 | + let pattern = format!("{}/python.*", temp_dir.to_string_lossy()); |
| 226 | + let result = expand_glob_pattern(&pattern); |
| 227 | + |
| 228 | + assert_eq!(result.len(), 2); |
| 229 | + assert!(result.iter().any(|p| p.ends_with("python.exe"))); |
| 230 | + assert!(result.iter().any(|p| p.ends_with("python.sh"))); |
| 231 | + assert!(!result.iter().any(|p| p.ends_with("pythonrc"))); |
| 232 | + |
| 233 | + // Cleanup |
| 234 | + let _ = fs::remove_dir_all(&temp_dir); |
| 235 | + } |
| 236 | +} |
0 commit comments