Skip to content

Commit

Permalink
feat: Add: 1.StandardEnvironment set/get ut
Browse files Browse the repository at this point in the history
Signed-off-by: photowey <[email protected]>
  • Loading branch information
photowey committed Jul 18, 2024
1 parent 73c0dc5 commit 4f992e0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
5 changes: 3 additions & 2 deletions crates/env/src/env/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -75,7 +75,8 @@ impl StandardEnvironment {
fn new_opt(table_opt: Option<Table>, registry: Box<dyn ReaderRegistry>) -> 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 {
Expand Down
7 changes: 2 additions & 5 deletions crates/env/src/reader/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait ReaderRegistry {
// ------------------------------------------------------------

pub struct ConfigReaderRegistry {
readers: HashMap</*suffix*/ String, Box<dyn ConfigReader>>,
readers: HashMap</*format|suffix*/ String, Box<dyn ConfigReader>>,
}

// ----------------------------------------------------------------
Expand Down Expand Up @@ -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()
}
}
76 changes: 74 additions & 2 deletions crates/env/src/tests/env_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

// ----------------------------------------------------------------

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;

// ----------------------------------------------------------------

#[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()))
Expand Down Expand Up @@ -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()))
);
}

0 comments on commit 4f992e0

Please sign in to comment.