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
This commit is contained in:
Matan Kushner
2019-04-15 20:54:52 -04:00
committed by GitHub
parent ab5490bea6
commit 794ae7b2ad
11 changed files with 254 additions and 89 deletions
+2 -27
View File
@@ -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));
}
}
+4 -48
View File
@@ -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");
+2 -1
View File
@@ -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");
+6 -8
View File
@@ -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),
}
+3 -4
View File
@@ -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(&current_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);
+5 -1
View File
@@ -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, &current_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