diff options
author | Matan Kushner <hello@matchai.me> | 2019-04-15 11:40:18 -0400 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-04-15 11:40:40 -0400 |
commit | bca4a7079f0bd663b8cc4584dc92388ce6799c1f (patch) | |
tree | 57891c9b1af75e907300535312bb883ac3185c99 /src/modules/directory.rs | |
parent | 71cac5bd1fb09909dde64bbcf3ee4a31fb29be4f (diff) |
Fix bug in path truncation
Paths with 3 components would truncate to 2 despite the truncation length begin set to 3.
Diffstat (limited to 'src/modules/directory.rs')
-rw-r--r-- | src/modules/directory.rs | 88 |
1 files changed, 57 insertions, 31 deletions
diff --git a/src/modules/directory.rs b/src/modules/directory.rs index d62a41f67..5f2174bd3 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -91,11 +91,11 @@ fn truncate(dir_string: String, length: usize) -> String { let components = dir_string .split(std::path::MAIN_SEPARATOR) .collect::<Vec<&str>>(); - if components.len() < length { + if components.len() <= length { return dir_string; } - let truncated_components = &components[..length]; + let truncated_components = &components[components.len() - length..]; truncated_components.join(&std::path::MAIN_SEPARATOR.to_string()) } @@ -103,45 +103,71 @@ fn truncate(dir_string: String, length: usize) -> String { mod tests { // TODO: Look into stubbing `env` so that tests can be run in parallel use super::*; - use clap::{App, Arg}; - use std::path::Path; - #[test] - fn truncate_home_dir() { - let args = App::new("starship") - .arg(Arg::with_name("status_code")) - .get_matches_from(vec!["starship", "0"]); + // #[test] + // fn truncate_home_dir() { + // let args = App::new("starship") + // .arg(Arg::with_name("status_code")) + // .get_matches_from(vec!["starship", "0"]); - let home_dir = dirs::home_dir().unwrap(); - env::set_current_dir(&home_dir).unwrap(); + // let home_dir = dirs::home_dir().unwrap(); + // env::set_current_dir(&home_dir).unwrap(); - let segment = segment(&args); - // assert_eq!(segment.value, "~"); - } + // let segment = segment(&args).unwrap(); + // assert_eq!(segment.output(), "~"); + // } - #[test] - fn dont_truncate_non_home_dir() { - let args = App::new("starship") - .arg(Arg::with_name("status_code")) - .get_matches_from(vec!["starship", "0"]); + // #[test] + // fn dont_truncate_non_home_dir() { + // let args = App::new("starship") + // .arg(Arg::with_name("status_code")) + // .get_matches_from(vec!["starship", "0"]); + + // let root_dir = Path::new("/"); + // env::set_current_dir(&root_dir).unwrap(); + + // let segment = segment(&args).unwrap(); + // assert_eq!(segment.output(), "/"); + // } + + // #[test] + // fn do_not_canonicalize_paths() { + // let args = App::new("starship") + // .arg(Arg::with_name("status_code")) + // .get_matches_from(vec!["starship", "0"]); - let root_dir = Path::new("/"); - env::set_current_dir(&root_dir).unwrap(); + // let root_dir = Path::new("/var"); + // env::set_current_dir(&root_dir).unwrap(); - let segment = segment(&args); - // assert_eq!(segment.value, "/"); + // let segment = segment(&args).unwrap(); + // assert_eq!(segment.output(), "/var"); + // } + + #[test] + fn truncate_smaller_path_than_provided_length() { + let path = "~/starship"; + let output = truncate(path.to_string(), 3); + assert_eq!(output, "~/starship") } #[test] - fn do_not_canonicalize_paths() { - let args = App::new("starship") - .arg(Arg::with_name("status_code")) - .get_matches_from(vec!["starship", "0"]); + fn truncate_same_path_as_provided_length() { + let path = "~/starship/engines"; + let output = truncate(path.to_string(), 3); + assert_eq!(output, "~/starship/engines") + } - let root_dir = Path::new("/var"); - env::set_current_dir(&root_dir).unwrap(); + #[test] + fn truncate_slightly_larger_path_then_provided_length() { + let path = "~/starship/engines/booster"; + let output = truncate(path.to_string(), 3); + assert_eq!(output, "starship/engines/booster") + } - let segment = segment(&args); - // assert_eq!(segment.value, "/var"); + #[test] + fn truncate_larger_path_than_provided_length() { + let path = "~/starship/engines/booster/rocket"; + let output = truncate(path.to_string(), 3); + assert_eq!(output, "engines/booster/rocket") } } |