use std::env;
use std::sync::Once;
static INIT: Once = Once::new();
const ENV_ENHANCED_OUTPUT: &str = "FLUENT_TEST_ENHANCED_OUTPUT";
pub struct Config {
pub(crate) use_colors: bool,
pub(crate) use_unicode_symbols: bool,
pub(crate) show_success_details: bool,
pub(crate) enhanced_output: bool,
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
impl Clone for Config {
fn clone(&self) -> Self {
Self {
use_colors: self.use_colors,
use_unicode_symbols: self.use_unicode_symbols,
show_success_details: self.show_success_details,
enhanced_output: self.enhanced_output,
}
}
}
impl Config {
pub fn new() -> Self {
let enhanced_from_env = match env::var(ENV_ENHANCED_OUTPUT) {
Ok(val) => {
let lowercase_val = val.to_lowercase();
lowercase_val == "true" || lowercase_val == "1" || lowercase_val == "yes"
}
Err(_) => false, };
Self { use_colors: true, use_unicode_symbols: true, show_success_details: true, enhanced_output: enhanced_from_env }
}
pub fn use_colors(mut self, enable: bool) -> Self {
self.use_colors = enable;
self
}
pub fn use_unicode_symbols(mut self, enable: bool) -> Self {
self.use_unicode_symbols = enable;
self
}
pub fn show_success_details(mut self, enable: bool) -> Self {
self.show_success_details = enable;
self
}
pub fn enhanced_output(mut self, enable: bool) -> Self {
self.enhanced_output = enable;
self
}
pub fn apply(self) {
use crate::reporter::GLOBAL_CONFIG;
let config = self.clone();
*GLOBAL_CONFIG.write().unwrap() = self;
if config.enhanced_output {
crate::initialize();
}
}
}
pub fn initialize() {
INIT.call_once(|| {
let config = crate::reporter::GLOBAL_CONFIG.read().unwrap();
if config.enhanced_output {
crate::events::EventEmitter::init();
crate::Reporter::init();
}
});
}
pub fn is_enhanced_output_enabled() -> bool {
let config = crate::reporter::GLOBAL_CONFIG.read().unwrap();
return config.enhanced_output;
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
fn reset_env_var() {
unsafe {
env::remove_var(ENV_ENHANCED_OUTPUT);
}
}
#[test]
fn test_config_default() {
reset_env_var();
let config = Config::new();
assert_eq!(config.use_colors, true);
assert_eq!(config.use_unicode_symbols, true);
assert_eq!(config.show_success_details, true);
assert_eq!(config.enhanced_output, false); }
#[test]
fn test_config_env_var_true() {
reset_env_var();
unsafe {
env::set_var(ENV_ENHANCED_OUTPUT, "true");
}
let config = Config::new();
assert_eq!(config.enhanced_output, true);
reset_env_var();
}
#[test]
fn test_config_env_var_false() {
reset_env_var();
unsafe {
env::set_var(ENV_ENHANCED_OUTPUT, "false");
}
let config = Config::new();
assert_eq!(config.enhanced_output, false);
reset_env_var();
}
#[test]
#[ignore] fn test_config_env_var_alternative_values() {
reset_env_var();
unsafe {
env::set_var(ENV_ENHANCED_OUTPUT, "1");
}
let config = Config::new();
assert_eq!(config.enhanced_output, true);
reset_env_var();
unsafe {
env::set_var(ENV_ENHANCED_OUTPUT, "yes");
}
let config = Config::new();
assert_eq!(config.enhanced_output, true);
reset_env_var();
unsafe {
env::set_var(ENV_ENHANCED_OUTPUT, "TRUE");
}
let config = Config::new();
assert_eq!(config.enhanced_output, true);
reset_env_var();
}
#[test]
fn test_config_builder_methods() {
let config = Config::new().use_colors(false).use_unicode_symbols(false).show_success_details(false).enhanced_output(true);
assert_eq!(config.use_colors, false);
assert_eq!(config.use_unicode_symbols, false);
assert_eq!(config.show_success_details, false);
assert_eq!(config.enhanced_output, true);
}
#[test]
fn test_config_clone() {
let config1 = Config::new().use_colors(false).enhanced_output(true);
let config2 = config1.clone();
assert_eq!(config1.use_colors, config2.use_colors);
assert_eq!(config1.use_unicode_symbols, config2.use_unicode_symbols);
assert_eq!(config1.show_success_details, config2.show_success_details);
assert_eq!(config1.enhanced_output, config2.enhanced_output);
}
}