summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Knaack <davidkna@users.noreply.github.com>2021-02-13 19:32:35 +0100
committerGitHub <noreply@github.com>2021-02-13 19:32:35 +0100
commitcdb999447a80788a7e24f58958055e8471f39421 (patch)
tree93a3d13b79530bdec5281a2b90e6614839c7473f /src
parent5ee09aa4dd41fecccb4205f1faf8c00e2231dd78 (diff)
feat(test): allow dynamic mocking of commands (#2307)
Diffstat (limited to 'src')
-rw-r--r--src/context.rs45
-rw-r--r--src/modules/java.rs28
-rw-r--r--src/test/mod.rs8
-rw-r--r--src/utils.rs2
4 files changed, 70 insertions, 13 deletions
diff --git a/src/context.rs b/src/context.rs
index 2ce55258a..251e5e337 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -43,8 +43,13 @@ pub struct Context<'a> {
pub shell: Shell,
/// A HashMap of environment variable mocks
+ #[cfg(test)]
pub env: HashMap<&'a str, String>,
+ /// A HashMap of command mocks
+ #[cfg(test)]
+ pub cmd: HashMap<&'a str, Option<CommandOutput>>,
+
/// Timeout for the execution of commands
cmd_timeout: Duration,
}
@@ -114,7 +119,10 @@ impl<'a> Context<'a> {
dir_contents: OnceCell::new(),
repo: OnceCell::new(),
shell,
+ #[cfg(test)]
env: HashMap::new(),
+ #[cfg(test)]
+ cmd: HashMap::new(),
cmd_timeout,
}
}
@@ -129,21 +137,27 @@ impl<'a> Context<'a> {
}
// Retrives a environment variable from the os or from a table if in testing mode
+ #[cfg(test)]
pub fn get_env<K: AsRef<str>>(&self, key: K) -> Option<String> {
- if cfg!(test) {
- self.env.get(key.as_ref()).map(|val| val.to_string())
- } else {
- env::var(key.as_ref()).ok()
- }
+ self.env.get(key.as_ref()).map(|val| val.to_string())
+ }
+
+ #[cfg(not(test))]
+ #[inline]
+ pub fn get_env<K: AsRef<str>>(&self, key: K) -> Option<String> {
+ env::var(key.as_ref()).ok()
}
// Retrives a environment variable from the os or from a table if in testing mode (os version)
+ #[cfg(test)]
pub fn get_env_os<K: AsRef<str>>(&self, key: K) -> Option<OsString> {
- if cfg!(test) {
- self.env.get(key.as_ref()).map(OsString::from)
- } else {
- env::var_os(key.as_ref())
- }
+ self.env.get(key.as_ref()).map(OsString::from)
+ }
+
+ #[cfg(not(test))]
+ #[inline]
+ pub fn get_env_os<K: AsRef<str>>(&self, key: K) -> Option<OsString> {
+ env::var_os(key.as_ref())
}
/// Convert a `~` in a path to the home directory
@@ -246,7 +260,18 @@ impl<'a> Context<'a> {
}
/// Execute a command and return the output on stdout and stderr if successful
+ #[inline]
pub fn exec_cmd(&self, cmd: &str, args: &[&str]) -> Option<CommandOutput> {
+ #[cfg(test)]
+ {
+ let command = match args.len() {
+ 0 => cmd.to_owned(),
+ _ => format!("{} {}", cmd, args.join(" ")),
+ };
+ if let Some(output) = self.cmd.get(command.as_str()) {
+ return output.clone();
+ }
+ }
exec_cmd(cmd, args, self.cmd_timeout)
}
}
diff --git a/src/modules/java.rs b/src/modules/java.rs
index 9fa1789b2..26745d7d5 100644
--- a/src/modules/java.rs
+++ b/src/modules/java.rs
@@ -91,7 +91,7 @@ fn parse_java_version(java_version: &str) -> Option<String> {
#[cfg(test)]
mod tests {
use super::*;
- use crate::test::ModuleRenderer;
+ use crate::{test::ModuleRenderer, utils::CommandOutput};
use ansi_term::Color;
use std::fs::File;
use std::io;
@@ -180,6 +180,32 @@ mod tests {
}
#[test]
+ fn folder_with_java_file_preview() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("Main.java"))?.sync_all()?;
+ let actual = ModuleRenderer::new("java").cmd("java -Xinternalversion", Some(CommandOutput {
+ stdout: "OpenJDK 64-Bit Server VM (16+14) for bsd-aarch64 JRE (16+14), built on Jan 17 2021 07:19:47 by \"brew\" with clang Apple LLVM 12.0.0 (clang-1200.0.32.28)\n".to_owned(),
+ stderr: "".to_owned()
+ })).path(dir.path()).collect();
+ let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ v16 ")));
+ assert_eq!(expected, actual);
+ dir.close()
+ }
+
+ #[test]
+ fn folder_with_java_file_no_java_installed() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("Main.java"))?.sync_all()?;
+ let actual = ModuleRenderer::new("java")
+ .cmd("java -Xinternalversion", None)
+ .path(dir.path())
+ .collect();
+ let expected = Some(format!("via {}", Color::Red.dimmed().paint("☕ ")));
+ assert_eq!(expected, actual);
+ dir.close()
+ }
+
+ #[test]
fn folder_with_class_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("Main.class"))?.sync_all()?;
diff --git a/src/test/mod.rs b/src/test/mod.rs
index acdc56f43..0cfab591d 100644
--- a/src/test/mod.rs
+++ b/src/test/mod.rs
@@ -1,6 +1,6 @@
-use crate::config::StarshipConfig;
use crate::context::{Context, Shell};
use crate::logger::StarshipLogger;
+use crate::{config::StarshipConfig, utils::CommandOutput};
use log::{Level, LevelFilter};
use once_cell::sync::Lazy;
use std::io;
@@ -83,6 +83,12 @@ impl<'a> ModuleRenderer<'a> {
self
}
+ /// Adds the command to the commandv_mocks of the underlying context
+ pub fn cmd(mut self, key: &'a str, val: Option<CommandOutput>) -> Self {
+ self.context.cmd.insert(key, val);
+ self
+ }
+
pub fn shell(mut self, shell: Shell) -> Self {
self.context.shell = shell;
self
diff --git a/src/utils.rs b/src/utils.rs
index 7dfe1406e..c9d076ae7 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -16,7 +16,7 @@ pub fn read_file<P: AsRef<Path>>(file_name: P) -> Result<String> {
Ok(data)
}
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct CommandOutput {
pub stdout: String,
pub stderr: String,