code complexity & repetition analysis tool
1use std::path::{Path, PathBuf};
2
3pub fn normalize_path(path: &str, repo_root: Option<&Path>) -> String {
4 let path = PathBuf::from(path);
5
6 let normalized = if let Some(root) = repo_root {
7 if let Ok(stripped) = path.strip_prefix(root) {
8 if stripped.as_os_str().is_empty() { PathBuf::from(".") } else { stripped.to_path_buf() }
9 } else {
10 path
11 }
12 } else {
13 path
14 };
15
16 normalized.display().to_string()
17}
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22
23 #[test]
24 fn test_normalize_path_no_root() {
25 let path = "/absolute/path/to/file.rs";
26 let normalized = normalize_path(path, None);
27 assert_eq!(normalized, path);
28 }
29
30 #[test]
31 fn test_normalize_path_with_root() {
32 let path = "/repo/src/lib.rs";
33 let root = Path::new("/repo");
34 let normalized = normalize_path(path, Some(root));
35 assert_eq!(normalized, "src/lib.rs");
36 }
37
38 #[test]
39 fn test_normalize_path_no_match() {
40 let path = "/other/path/file.rs";
41 let root = Path::new("/repo");
42 let normalized = normalize_path(path, Some(root));
43 assert_eq!(normalized, path);
44 }
45
46 #[test]
47 fn test_normalize_path_relative() {
48 let path = "src/lib.rs";
49 let normalized = normalize_path(path, None);
50 assert_eq!(normalized, path);
51 }
52
53 #[test]
54 fn test_normalize_path_exact_match() {
55 let path = "/repo/src/lib.rs";
56 let root = Path::new("/repo/src/lib.rs");
57 let normalized = normalize_path(path, Some(root));
58 assert_eq!(normalized, ".");
59 }
60}