From 794ae7b2ad3d79009162fa7f2d4fe24997f7c2d1 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Mon, 15 Apr 2019 20:54:52 -0400 Subject: Add integration tests (#6) ### Changed - Added current_dir param to segments to make them more testable - Moved all existing integration tests to a `tests/` dir ### Added - A whole bunch of new integration tests --- src/modules/directory.rs | 52 ++++-------------------------------------------- 1 file changed, 4 insertions(+), 48 deletions(-) (limited to 'src/modules/directory.rs') diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 2b5e52722..8048a3620 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -3,7 +3,6 @@ use ansi_term::Color; use clap::ArgMatches; use dirs; use git2::Repository; -use std::env; use std::path::Path; /// Creates a segment with the current directory @@ -15,28 +14,25 @@ use std::path::Path; /// /// **Truncation** /// Paths will be limited in length to `3` path components by default. -pub fn segment(_: &ArgMatches) -> Option { +pub fn segment(current_dir: &Path, _args: &ArgMatches) -> Option { const HOME_SYMBOL: &str = "~"; const DIR_TRUNCATION_LENGTH: usize = 3; const SECTION_COLOR: Color = Color::Cyan; let mut segment = Segment::new("dir"); - // TODO: Currently gets the physical directory. Get the logical directory. - let current_path = env::current_dir().expect("Unable to identify current directory"); - let dir_string; - if let Ok(repo) = git2::Repository::discover(¤t_path) { + if let Ok(repo) = git2::Repository::discover(current_dir) { // Contract the path to the git repo root let repo_root = get_repo_root(&repo); let repo_folder_name = repo_root.file_name().unwrap().to_str().unwrap(); - dir_string = contract_path(¤t_path, repo_root, repo_folder_name); + dir_string = contract_path(current_dir, repo_root, repo_folder_name); } else { // Contract the path to the home directory let home_dir = dirs::home_dir().unwrap(); - dir_string = contract_path(¤t_path, &home_dir, HOME_SYMBOL); + dir_string = contract_path(current_dir, &home_dir, HOME_SYMBOL); } // Truncate the dir string to the maximum number of path components @@ -107,48 +103,8 @@ fn truncate(dir_string: String, length: usize) -> String { #[cfg(test)] mod tests { - // TODO: Look into stubbing `env` so that tests can be run in parallel use super::*; - // #[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 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"]); - - // 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("/var"); - // env::set_current_dir(&root_dir).unwrap(); - - // let segment = segment(&args).unwrap(); - // assert_eq!(segment.output(), "/var"); - // } - #[test] fn contract_home_directory() { let full_path = Path::new("/Users/astronaut/schematics/rocket"); -- cgit v1.2.3