Skip to content

Commit 13b2b87

Browse files
added unit tests for substitute_env_vars function
1 parent 8844093 commit 13b2b87

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

config/src/config.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ impl Config {
100100
Err(_) => format!("${{{}}}", var_name),
101101
}
102102
});
103-
104103
Ok(result.into_owned())
105104
}
106105

@@ -301,9 +300,8 @@ pub struct RavRequestConfig {
301300

302301
#[cfg(test)]
303302
mod tests {
304-
use std::{fs, path::PathBuf};
305-
306303
use sealed_test::prelude::*;
304+
use std::{env, fs, path::PathBuf};
307305
use tracing_test::traced_test;
308306

309307
use crate::{Config, ConfigPrefix};
@@ -420,4 +418,45 @@ mod tests {
420418
test_value
421419
);
422420
}
421+
422+
// Test to check substitute_env_vars function is substituting env variables
423+
// indexers can use ${ENV_VAR_NAME} to point to the required env variable
424+
#[test]
425+
fn test_substitute_env_vars() {
426+
// Set up environment variables
427+
env::set_var("TEST_VAR1", "hello");
428+
env::set_var("TEST_VAR2", "world");
429+
// TEST_VAR3 not setup and should not be replaced by the function
430+
431+
let input = r#"
432+
[section1]
433+
key1 = "${TEST_VAR1}"
434+
key2 = "${TEST_VAR2}"
435+
key3 = "${TEST_VAR3}"
436+
key4 = "prefix_${TEST_VAR1}_${TEST_VAR2}_suffix"
437+
key5 = "a_key_without_substitution"
438+
"#
439+
.to_string();
440+
441+
let expected_output = r#"
442+
[section1]
443+
key1 = "hello"
444+
key2 = "world"
445+
key3 = "${TEST_VAR3}"
446+
key4 = "prefix_hello_world_suffix"
447+
key5 = "a_key_without_substitution"
448+
"#
449+
.to_string();
450+
451+
let result = Config::substitute_env_vars(input).expect("Failed to substitute env vars ");
452+
453+
assert_eq!(
454+
result, expected_output,
455+
"Environment variable substitution failed"
456+
);
457+
458+
// Clean up environment variables
459+
env::remove_var("TEST_VAR1");
460+
env::remove_var("TEST_VAR2");
461+
}
423462
}

0 commit comments

Comments
 (0)