From 4f992e08102f226dbfa872d2d515704b93371162 Mon Sep 17 00:00:00 2001 From: photowey Date: Fri, 19 Jul 2024 01:14:51 +0800 Subject: [PATCH] feat: Add: 1.StandardEnvironment set/get ut Signed-off-by: photowey --- crates/env/src/env/standard.rs | 5 +- crates/env/src/reader/registry.rs | 7 +-- crates/env/src/tests/env_tests.rs | 76 ++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 9 deletions(-) diff --git a/crates/env/src/env/standard.rs b/crates/env/src/env/standard.rs index d6894c3..fa8ff8e 100644 --- a/crates/env/src/env/standard.rs +++ b/crates/env/src/env/standard.rs @@ -32,7 +32,7 @@ use omigacore::constants::{ use crate::core::error::FileError; use crate::core::{error::ConfigError, merger::merge_tables}; -use crate::env::{is_default_profile, try_load_env_variables, DynamicEnvironment, Environment}; +use crate::env::{is_default_profile, DynamicEnvironment, Environment}; use crate::reader::{ registry::{ConfigReaderRegistry, ReaderRegistry}, toml::TomlConfigReader, @@ -75,7 +75,8 @@ impl StandardEnvironment { fn new_opt(table_opt: Option, registry: Box) -> Self { let mut merged_table = Table::new(); - let env_table = try_load_env_variables(); + //let env_table = try_load_env_variables(); + let env_table = Table::new(); merged_table = merge_tables(merged_table, env_table); if let Some(table) = table_opt { diff --git a/crates/env/src/reader/registry.rs b/crates/env/src/reader/registry.rs index 0f31ad8..9764136 100644 --- a/crates/env/src/reader/registry.rs +++ b/crates/env/src/reader/registry.rs @@ -33,7 +33,7 @@ pub trait ReaderRegistry { // ------------------------------------------------------------ pub struct ConfigReaderRegistry { - readers: HashMap>, + readers: HashMap>, } // ---------------------------------------------------------------- @@ -66,9 +66,6 @@ impl ReaderRegistry for ConfigReaderRegistry { } fn try_acquires(&self) -> Vec<&dyn ConfigReader> { - self.readers - .values() - .map(|r| r.as_ref() as &dyn ConfigReader) - .collect() + self.readers.values().map(|r| r.as_ref()).collect() } } diff --git a/crates/env/src/tests/env_tests.rs b/crates/env/src/tests/env_tests.rs index c75f7de..e5d9c55 100644 --- a/crates/env/src/tests/env_tests.rs +++ b/crates/env/src/tests/env_tests.rs @@ -18,9 +18,10 @@ // ---------------------------------------------------------------- -use omigacore::collection::table::Table; +use omigacore::collection::table::{Table, Value}; use crate::env::standard::StandardEnvironment; +use crate::env::Environment; use crate::reader::registry::ConfigReaderRegistry; use crate::reader::toml::TomlConfigReader; @@ -28,7 +29,7 @@ use crate::reader::toml::TomlConfigReader; #[test] #[cfg(windows)] -fn test_standard_environment_builder() { +fn test_standard_environment_builder_os_windows() { let rvt = StandardEnvironment::builder() .with_table(Table::new()) .with_registry(Box::new(ConfigReaderRegistry::default())) @@ -71,3 +72,74 @@ fn test_standard_environment_builder_os_unix() { assert!(rvt.is_ok()); } + +// ---------------------------------------------------------------- + +#[test] +#[cfg(windows)] +fn test_standard_environment_os_windows() { + let rvt = StandardEnvironment::builder() + .with_table(Table::new()) + .with_registry(Box::new(ConfigReaderRegistry::default())) + .with_reader(Box::new(TomlConfigReader::default())) + .with_path(".\\testdata".to_string()) + .with_config("omiga".to_string()) + .with_profile("dev".to_string()) + .with_format("toml".to_string()) + .with_search_path("C:\\rust\\app\\configs".to_string()) + .build(); + + let mut environment = rvt.unwrap(); + environment + .set("io.github.photowey", Value::String("omiga".to_string())) + .expect("Set failed"); + + assert_eq!( + environment.get("io.github.photowey"), + Ok(&Value::String("omiga".to_string())) + ); +} + +#[test] +#[cfg(unix)] +fn test_standard_environment_os_unix() { + let rvt = StandardEnvironment::builder() + .with_table(Table::new()) + .with_registry(Box::new(ConfigReaderRegistry::default())) + .with_reader(Box::new(TomlConfigReader::default())) + .with_path("./testdata".to_string()) + .with_config("omiga".to_string()) + .with_profile("dev".to_string()) + .with_format("toml".to_string()) + .with_search_path("/opt/app/configs".to_string()) + .build(); + + let environment = rvt.unwrap(); + + assert!(environment.get("JAVA_HOME").is_ok()); +} + +#[test] +#[cfg(windows)] +fn test_standard_environment_os_unix() { + let rvt = StandardEnvironment::builder() + .with_table(Table::new()) + .with_registry(Box::new(ConfigReaderRegistry::default())) + .with_reader(Box::new(TomlConfigReader::default())) + .with_path("./testdata".to_string()) + .with_config("omiga".to_string()) + .with_profile("dev".to_string()) + .with_format("toml".to_string()) + .with_search_path("/opt/app/configs".to_string()) + .build(); + + let mut environment = rvt.unwrap(); + environment + .set("io.github.photowey", Value::String("omiga".to_string())) + .expect("Set failed"); + + assert_eq!( + environment.get("io.github.photowey"), + Ok(&Value::String("omiga".to_string())) + ); +}