summaryrefslogtreecommitdiffstats
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
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
-rw-r--r--Cargo.lock33
-rw-r--r--Cargo.toml3
-rw-r--r--src/modules/character.rs29
-rw-r--r--src/modules/directory.rs52
-rw-r--r--src/modules/line_break.rs3
-rw-r--r--src/modules/mod.rs14
-rw-r--r--src/modules/nodejs.rs7
-rw-r--r--src/print.rs6
-rw-r--r--tests/character.rs29
-rw-r--r--tests/common.rs18
-rw-r--r--tests/directory.rs149
11 files changed, 254 insertions, 89 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bc747f9a2..683ea6978 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -291,6 +291,18 @@ dependencies = [
]
[[package]]
+name = "rand"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -349,6 +361,14 @@ dependencies = [
]
[[package]]
+name = "remove_dir_all"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "rustc-demangle"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -392,6 +412,7 @@ dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -421,6 +442,15 @@ dependencies = [
]
[[package]]
+name = "tempdir"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -538,6 +568,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
+"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0"
"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
@@ -545,6 +576,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum redox_syscall 0.1.52 (registry+https://github.com/rust-lang/crates.io-index)" = "d32b3053e5ced86e4bc0411fec997389532bf56b000e66cb4884eeeb41413d69"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum redox_users 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3fe5204c3a17e97dde73f285d49be585df59ed84b50a872baf416e73b62c3828"
+"checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5"
"checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
@@ -554,6 +586,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum syn 0.15.30 (registry+https://github.com/rust-lang/crates.io-index)" = "66c8865bf5a7cbb662d8b011950060b3c8743dca141b054bf7195b20d314d8e2"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
+"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
diff --git a/Cargo.toml b/Cargo.toml
index 69dadcbf3..678d927c8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,3 +9,6 @@ clap = "2.32.0"
ansi_term = "0.11.0"
dirs = "1.0.5"
git2 = "0.8.0"
+
+[dev-dependencies]
+tempdir = "0.3.7"
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(&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");
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(&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);
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, &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
diff --git a/tests/character.rs b/tests/character.rs
new file mode 100644
index 000000000..c9b27b1e9
--- /dev/null
+++ b/tests/character.rs
@@ -0,0 +1,29 @@
+use ansi_term::Color;
+use starship::segment::Segment;
+use std::path::Path;
+
+mod common;
+
+#[test]
+fn char_section_success_status() {
+ let dir = Path::new("~");
+ let expected = Segment::new("char")
+ .set_value("➜")
+ .set_style(Color::Green)
+ .set_prefix(None)
+ .output();
+ let actual = common::render_segment_with_status("char", &dir, "0");
+ assert_eq!(expected, actual);
+}
+
+#[test]
+fn char_section_failure_status() {
+ let dir = Path::new("~");
+ let expected = Segment::new("char")
+ .set_value("➜")
+ .set_style(Color::Red)
+ .set_prefix(None)
+ .output();
+ let actual = common::render_segment_with_status("char", &dir, "1");
+ assert_eq!(expected, actual);
+}
diff --git a/tests/common.rs b/tests/common.rs
new file mode 100644
index 000000000..4bc62a5fa
--- /dev/null
+++ b/tests/common.rs
@@ -0,0 +1,18 @@
+use clap::{App, Arg};
+use starship::modules;
+use std::path::Path;
+
+pub fn render_segment(module: &str, path: &Path) -> String {
+ render_segment_with_status(module, path, "0")
+}
+
+pub fn render_segment_with_status(module: &str, path: &Path, status: &str) -> String {
+ // Create an `Arg` with status_code of "0"
+ let args = App::new("starship")
+ .arg(Arg::with_name("status_code"))
+ .get_matches_from(vec!["starship", status]);
+
+ let segment = modules::handle(module, path, &args);
+
+ segment.unwrap().output()
+}
diff --git a/tests/directory.rs b/tests/directory.rs
new file mode 100644
index 000000000..ae8d93736
--- /dev/null
+++ b/tests/directory.rs
@@ -0,0 +1,149 @@
+use ansi_term::Color;
+use git2::Repository;
+use starship::segment::Segment;
+use std::fs;
+use std::io;
+use std::path::Path;
+use tempdir::TempDir;
+
+mod common;
+
+#[test]
+fn home_directory() -> io::Result<()> {
+ let dir = Path::new("~");
+
+ let expected = Segment::new("dir")
+ .set_value("~")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn directory_in_home() -> io::Result<()> {
+ let dir = Path::new("~/starship/engine");
+
+ let expected = Segment::new("dir")
+ .set_value("~/starship/engine")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn truncated_directory_in_home() -> io::Result<()> {
+ let dir = Path::new("~/starship/engine/schematics");
+
+ let expected = Segment::new("dir")
+ .set_value("starship/engine/schematics")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn root_directory() -> io::Result<()> {
+ let dir = Path::new("/");
+
+ let expected = Segment::new("dir")
+ .set_value("/")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn directory_in_root() -> io::Result<()> {
+ let dir = Path::new("/private");
+
+ let expected = Segment::new("dir")
+ .set_value("/private")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn truncated_directory_in_root() -> io::Result<()> {
+ let dir = Path::new("/private/var/folders/3s");
+
+ let expected = Segment::new("dir")
+ .set_value("var/folders/3s")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn git_repo_root() -> io::Result<()> {
+ let temp_dir = TempDir::new("starship")?;
+ let repo_dir = temp_dir.path().join("rocket-controls");
+ fs::create_dir(&repo_dir)?;
+
+ Repository::init(&repo_dir).unwrap();
+
+ let expected = Segment::new("dir")
+ .set_value("rocket-controls")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &repo_dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn directory_in_git_repo() -> io::Result<()> {
+ let temp_dir = TempDir::new("starship")?;
+ let repo_dir = temp_dir.path().join("rocket-controls");
+ let dir = repo_dir.join("src");
+ fs::create_dir_all(&dir)?;
+
+ Repository::init(&repo_dir).unwrap();
+
+ let expected = Segment::new("dir")
+ .set_value("rocket-controls/src")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}
+
+#[test]
+fn truncated_directory_in_git_repo() -> io::Result<()> {
+ let temp_dir = TempDir::new("starship")?;
+ let repo_dir = temp_dir.path().join("rocket-controls");
+ let dir = repo_dir.join("src/meters/fuel-gauge");
+ fs::create_dir_all(&dir)?;
+
+ Repository::init(&repo_dir).unwrap();
+
+ let expected = Segment::new("dir")
+ .set_value("src/meters/fuel-gauge")
+ .set_style(Color::Cyan.bold())
+ .output();
+ let actual = common::render_segment("dir", &dir);
+ assert_eq!(expected, actual);
+
+ Ok(())
+}