1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 13:47:38 +00:00

Better test and document the dir segment

This commit is contained in:
Matan Kushner 2019-04-15 12:04:53 -04:00
parent bca4a7079f
commit d2cda32c87
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4

View File

@ -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")