diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 5f2174bd..0aed255e 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -61,6 +61,9 @@ fn get_repo_root(repo: &Repository) -> &Path { } /// Contract the root component of a path +/// +/// Replaces the `top_level_path` in a given `full_path` with the provided +/// `top_level_replacement`. fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: &str) -> String { if !full_path.starts_with(top_level_path) { return full_path.to_str().unwrap().to_string(); @@ -83,6 +86,9 @@ fn contract_path(full_path: &Path, top_level_path: &Path, top_level_replacement: } /// Truncate a path to only have a set number of path components +/// +/// Will truncate a path to only show the last `length` components in a path. +/// If a length of `0` is provided, the path will not be truncated. fn truncate(dir_string: String, length: usize) -> String { if length == 0 { return dir_string; @@ -143,6 +149,24 @@ mod tests { // assert_eq!(segment.output(), "/var"); // } + #[test] + fn contract_home_directory() { + let full_path = Path::new("/Users/astronaut/schematics/rocket"); + let home = Path::new("/Users/astronaut"); + + let output = contract_path(full_path, home, "~"); + assert_eq!(output, "~/schematics/rocket"); + } + + #[test] + fn contract_repo_directory() { + let full_path = Path::new("/Users/astronaut/dev/rocket-controls/src"); + let repo_root = Path::new("/Users/astronaut/dev/rocket-controls"); + + let output = contract_path(full_path, repo_root, "rocket-controls"); + assert_eq!(output, "rocket-controls/src"); + } + #[test] fn truncate_smaller_path_than_provided_length() { let path = "~/starship"; @@ -158,7 +182,7 @@ mod tests { } #[test] - fn truncate_slightly_larger_path_then_provided_length() { + fn truncate_slightly_larger_path_than_provided_length() { let path = "~/starship/engines/booster"; let output = truncate(path.to_string(), 3); assert_eq!(output, "starship/engines/booster")