summaryrefslogtreecommitdiffstats
path: root/src/modules/directory.rs
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-04-15 20:54:52 -0400
committerGitHub <noreply@github.com>2019-04-15 20:54:52 -0400
commit794ae7b2ad3d79009162fa7f2d4fe24997f7c2d1 (patch)
tree8a60c56f3092c1c21844bd5d842b4b328b67c6ea /src/modules/directory.rs
parentab5490bea691d6c5fbe525a54799c17d4bd76dca (diff)
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
Diffstat (limited to 'src/modules/directory.rs')
-rw-r--r--src/modules/directory.rs52
1 files changed, 4 insertions, 48 deletions
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<Segment> {
+pub fn segment(current_dir: &Path, _args: &ArgMatches) -> Option<Segment> {
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(&current_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(&current_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(&current_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");