diff options
author | Matan Kushner <hello@matchai.me> | 2019-04-15 20:54:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-15 20:54:52 -0400 |
commit | 794ae7b2ad3d79009162fa7f2d4fe24997f7c2d1 (patch) | |
tree | 8a60c56f3092c1c21844bd5d842b4b328b67c6ea /src | |
parent | ab5490bea691d6c5fbe525a54799c17d4bd76dca (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')
-rw-r--r-- | src/modules/character.rs | 29 | ||||
-rw-r--r-- | src/modules/directory.rs | 52 | ||||
-rw-r--r-- | src/modules/line_break.rs | 3 | ||||
-rw-r--r-- | src/modules/mod.rs | 14 | ||||
-rw-r--r-- | src/modules/nodejs.rs | 7 | ||||
-rw-r--r-- | src/print.rs | 6 |
6 files changed, 22 insertions, 89 deletions
diff --git a/src/modules/character.rs b/src/modules/character.rs index 53a43e886..359ff61fb 100644 --- a/src/modules/character.rs +++ b/src/modules/character.rs @@ -1,6 +1,7 @@ use super::Segment; use ansi_term::Color; use clap::ArgMatches; +use std::path::Path; /// Creates a segment for the prompt character /// @@ -10,7 +11,7 @@ use clap::ArgMatches; /// (green by default) /// - If the exit-code was anything else, the arrow will be formatted with /// `COLOR_FAILURE` (red by default) -pub fn segment(args: &ArgMatches) -> Option<Segment> { +pub fn segment(_current_dir: &Path, args: &ArgMatches) -> Option<Segment> { const PROMPT_CHAR: &str = "➜"; const COLOR_SUCCESS: Color = Color::Green; const COLOR_FAILURE: Color = Color::Red; @@ -27,29 +28,3 @@ pub fn segment(args: &ArgMatches) -> Option<Segment> { Some(segment) } - -#[cfg(test)] -mod tests { - use super::*; - use clap::{App, Arg}; - - #[test] - fn char_section_success_status() { - let args = App::new("starship") - .arg(Arg::with_name("status_code")) - .get_matches_from(vec!["starship", "0"]); - - let segment = segment(&args); - // assert_eq!(segment.style, Style::from(Color::Green)); - } - - #[test] - fn char_section_failure_status() { - let args = App::new("starship") - .arg(Arg::with_name("status_code")) - .get_matches_from(vec!["starship", "1"]); - - let segment = segment(&args); - // assert_eq!(segment.style, Style::from(Color::Red)); - } -} 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(¤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"); diff --git a/src/modules/line_break.rs b/src/modules/line_break.rs index ed8b5023d..a7bda251e 100644 --- a/src/modules/line_break.rs +++ b/src/modules/line_break.rs @@ -1,8 +1,9 @@ use super::Segment; use clap::ArgMatches; +use std::path::Path; /// Creates a segment for the line break -pub fn segment(_: &ArgMatches) -> Option<Segment> { +pub fn segment(_current_dir: &Path, _args: &ArgMatches) -> Option<Segment> { const LINE_ENDING: &str = "\n"; let mut segment = Segment::new("line_break"); diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 6fc14d1bf..9efdad46c 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -5,16 +5,14 @@ mod nodejs; use crate::segment::Segment; use clap::ArgMatches; +use std::path::Path; -// pub static current_dir: PathBuf = env::current_dir().expect("Unable to identify current directory"); -// TODO: Currently gets the physical directory. Get the logical directory. - -pub fn handle(module: &str, args: &ArgMatches) -> Option<Segment> { +pub fn handle(module: &str, current_dir: &Path, args: &ArgMatches) -> Option<Segment> { match module { - "dir" | "directory" => directory::segment(&args), - "char" | "character" => character::segment(&args), - "node" | "nodejs" => nodejs::segment(&args), - "line_break" => line_break::segment(&args), + "dir" | "directory" => directory::segment(current_dir, args), + "char" | "character" => character::segment(current_dir, args), + "node" | "nodejs" => nodejs::segment(current_dir, args), + "line_break" => line_break::segment(current_dir, args), _ => panic!("Unknown module: {}", module), } diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index e7a3e736f..c3ba59f3f 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -1,8 +1,8 @@ use super::Segment; use ansi_term::Color; use clap::ArgMatches; -use std::env; use std::fs::{self, DirEntry}; +use std::path::Path; use std::process::Command; /// Creates a segment with the current Node.js version @@ -11,13 +11,12 @@ use std::process::Command; /// - Current directory contains a `.js` file /// - Current directory contains a `node_modules` directory /// - Current directory contains a `package.json` file -pub fn segment(_: &ArgMatches) -> Option<Segment> { +pub fn segment(current_dir: &Path, _args: &ArgMatches) -> Option<Segment> { const NODE_CHAR: &str = "⬢"; const SECTION_COLOR: Color = Color::Green; let mut segment = Segment::new("node"); - let current_path = env::current_dir().expect("Unable to identify current directory"); - let files = fs::read_dir(¤t_path).unwrap(); + let files = fs::read_dir(current_dir).unwrap(); // Early return if there are no JS project files let is_js_project = files.filter_map(Result::ok).any(has_js_files); diff --git a/src/print.rs b/src/print.rs index a909e58bc..c86083126 100644 --- a/src/print.rs +++ b/src/print.rs @@ -1,4 +1,5 @@ use clap::ArgMatches; +use std::env; use std::io::{self, Write}; use crate::modules; @@ -6,6 +7,9 @@ use crate::modules; pub fn prompt(args: ArgMatches) { let default_prompt = vec!["directory", "nodejs", "line_break", "character"]; + // TODO: Currently gets the physical directory. Get the logical directory. + let current_path = env::current_dir().expect("Unable to identify current directory."); + // TODO: // - List files in directory // - Index binaries in PATH @@ -18,7 +22,7 @@ pub fn prompt(args: ArgMatches) { default_prompt .iter() - .map(|module| modules::handle(module, &args)) // Compute segments + .map(|module| modules::handle(module, ¤t_path, &args)) // Compute segments .flatten() // Remove segments set to `None` .enumerate() // Turn segment into tuple with index .map(|(index, segment)| segment.output_index(index)) // Generate string outputs |