summaryrefslogtreecommitdiffstats
path: root/src/modules
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-04-11 20:04:04 -0400
committerMatan Kushner <hello@matchai.me>2019-04-11 20:04:04 -0400
commit7ffadd37bc3feaae3b636f64169176c041f6684a (patch)
treeb9f1d731f8cceb21dee8220cf98773c9faaac819 /src/modules
parentc1f5a733c991abc8575832a8280ef683127e2e5c (diff)
Remove duplicate tests
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/nodejs.rs48
1 files changed, 14 insertions, 34 deletions
diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs
index 3c362f5d2..87a1e4a25 100644
--- a/src/modules/nodejs.rs
+++ b/src/modules/nodejs.rs
@@ -6,16 +6,21 @@ use std::fs::{self, DirEntry};
use std::process::Command;
/// Creates a segment with the current Node.js version
+///
+/// Will display the Node.js version if any of the following criteria are met:
+/// - Current directory contains a `.js` file
+/// - Current directory contains a `node_modules` directory
+/// - Current directory contains a `package.json` file
pub fn segment(_: &ArgMatches) -> Segment {
- const NODE_CHAR: &str = "⬢ ";
+ const NODE_CHAR: &str = "⬢";
const SECTION_COLOR: Color = Color::Green;
let current_path = env::current_dir().expect("Unable to identify current directory");
let files = fs::read_dir(&current_path).unwrap();
+ // Early return if there are no JS project files
let is_js_project = files.filter_map(Result::ok).any(has_js_files);
-
- if is_js_project {
+ if !is_js_project {
return Segment::default();
}
@@ -27,47 +32,22 @@ pub fn segment(_: &ArgMatches) -> Segment {
};
Segment {
- value: format!("{}{}", NODE_CHAR, version),
+ value: format!("{} {}", NODE_CHAR, version),
style: Style::from(SECTION_COLOR),
..Default::default()
}
}
fn has_js_files(dir_entry: DirEntry) -> bool {
- let is_js_file =
- |d: &DirEntry| d.path().is_file() && d.path().extension().unwrap_or_default() == "js";
- let is_node_modules = |d: &DirEntry| {
+ let is_js_file = |d: &DirEntry| -> bool {
+ d.path().is_file() && d.path().extension().unwrap_or_default() == "js"
+ };
+ let is_node_modules = |d: &DirEntry| -> bool {
d.path().is_dir() && d.path().file_name().unwrap_or_default() == "node_modules"
};
- let is_package_json = |d: &DirEntry| {
+ let is_package_json = |d: &DirEntry| -> bool {
d.path().is_file() && d.path().file_name().unwrap_or_default() == "package.json"
};
is_js_file(&dir_entry) || is_node_modules(&dir_entry) || is_package_json(&dir_entry)
}
-
-#[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));
- }
-}