From cdb999447a80788a7e24f58958055e8471f39421 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Sat, 13 Feb 2021 19:32:35 +0100 Subject: [PATCH] feat(test): allow dynamic mocking of commands (#2307) --- src/context.rs | 45 +++++++++++++++++++++++++++++++++++---------- src/modules/java.rs | 28 +++++++++++++++++++++++++++- src/test/mod.rs | 8 +++++++- src/utils.rs | 2 +- 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/src/context.rs b/src/context.rs index 2ce55258..251e5e33 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>, + /// 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>(&self, key: K) -> Option { - 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>(&self, key: K) -> Option { + 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>(&self, key: K) -> Option { - 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>(&self, key: K) -> Option { + 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 { + #[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 9fa1789b..26745d7d 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -91,7 +91,7 @@ fn parse_java_version(java_version: &str) -> Option { #[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; @@ -179,6 +179,32 @@ mod tests { dir.close() } + #[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()?; diff --git a/src/test/mod.rs b/src/test/mod.rs index acdc56f4..0cfab591 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) -> 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 7dfe1406..c9d076ae 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,7 +16,7 @@ pub fn read_file>(file_name: P) -> Result { Ok(data) } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct CommandOutput { pub stdout: String, pub stderr: String,