diff options
author | Matan Kushner <hello@matchai.dev> | 2020-01-26 17:37:18 -0500 |
---|---|---|
committer | Kevin Song <chipbuster@users.noreply.github.com> | 2020-01-26 16:37:18 -0600 |
commit | 3365beae09af2f12b6323a52f88633ed97b39c51 (patch) | |
tree | fb36ec6c06439edd29775fc4a7b1a06041619b69 /src/module.rs | |
parent | 5342dcc658110f8884a77ef1b3f83e867322ffb5 (diff) |
test(nodejs): Port nodejs module tests from E2E to integraton (#867)
Replaces the existing nodejs module end-to-end tests with integration tests that don't require preinstalled environmental dependencies.
- Moved the tests to the same file as the module they test
- Created a render_module utility function for rendering modules within tests
- Removed Node.js installation during CI setup
- Add Shell to Context to allow for tests to not run shell-specific code
Diffstat (limited to 'src/module.rs')
-rw-r--r-- | src/module.rs | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/module.rs b/src/module.rs index 34f3a6520..f09c6e5df 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,4 +1,5 @@ use crate::config::SegmentConfig; +use crate::context::Shell; use crate::segment::Segment; use ansi_term::Style; use ansi_term::{ANSIString, ANSIStrings}; @@ -134,10 +135,10 @@ impl<'a> Module<'a> { /// Returns a vector of colored ANSIString elements to be later used with /// `ANSIStrings()` to optimize ANSI codes pub fn ansi_strings(&self) -> Vec<ANSIString> { - self.ansi_strings_for_prompt(true) + self.ansi_strings_for_shell(Shell::Unknown) } - pub fn ansi_strings_for_prompt(&self, is_prompt: bool) -> Vec<ANSIString> { + pub fn ansi_strings_for_shell(&self, shell: Shell) -> Vec<ANSIString> { let mut ansi_strings = self .segments .iter() @@ -147,20 +148,17 @@ impl<'a> Module<'a> { ansi_strings.insert(0, self.prefix.ansi_string()); ansi_strings.push(self.suffix.ansi_string()); - if is_prompt { - let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); - ansi_strings = match shell.as_str() { - "bash" => ansi_strings_modified(ansi_strings, shell), - "zsh" => ansi_strings_modified(ansi_strings, shell), - _ => ansi_strings, - }; - } + ansi_strings = match shell { + Shell::Bash => ansi_strings_modified(ansi_strings, shell), + Shell::Zsh => ansi_strings_modified(ansi_strings, shell), + _ => ansi_strings, + }; ansi_strings } - pub fn to_string_without_prefix(&self) -> String { - ANSIStrings(&self.ansi_strings()[1..]).to_string() + pub fn to_string_without_prefix(&self, shell: Shell) -> String { + ANSIStrings(&self.ansi_strings_for_shell(shell)[1..]).to_string() } } @@ -174,7 +172,7 @@ impl<'a> fmt::Display for Module<'a> { /// Many shells cannot deal with raw unprintable characters (like ANSI escape sequences) and /// miscompute the cursor position as a result, leading to strange visual bugs. Here, we wrap these /// characters in shell-specific escape codes to indicate to the shell that they are zero-length. -fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: String) -> Vec<ANSIString> { +fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: Shell) -> Vec<ANSIString> { const ESCAPE_BEGIN: char = '\u{1b}'; const MAYBE_ESCAPE_END: char = 'm'; ansi_strings @@ -187,18 +185,18 @@ fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: String) -> Vec<AN .map(|x| match x { ESCAPE_BEGIN => { escaped = true; - match shell.as_str() { - "bash" => String::from("\u{5c}\u{5b}\u{1b}"), // => \[ESC - "zsh" => String::from("\u{25}\u{7b}\u{1b}"), // => %{ESC + match shell { + Shell::Bash => String::from("\u{5c}\u{5b}\u{1b}"), // => \[ESC + Shell::Zsh => String::from("\u{25}\u{7b}\u{1b}"), // => %{ESC _ => x.to_string(), } } MAYBE_ESCAPE_END => { if escaped { escaped = false; - match shell.as_str() { - "bash" => String::from("m\u{5c}\u{5d}"), // => m\] - "zsh" => String::from("m\u{25}\u{7d}"), // => m%} + match shell { + Shell::Bash => String::from("m\u{5c}\u{5d}"), // => m\] + Shell::Zsh => String::from("m\u{25}\u{7d}"), // => m%} _ => x.to_string(), } } else { |