Skip to content

Commit 9105833

Browse files
added new test, changed substitute_env_vars function
1 parent 426fa1e commit 9105833

File tree

1 file changed

+78
-16
lines changed

1 file changed

+78
-16
lines changed

config/src/config.rs

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,33 @@ impl Config {
9191
fn substitute_env_vars(content: String) -> Result<String, String> {
9292
let reg = Regex::new(r"\$\{([A-Z_][A-Z0-9_]*)\}").map_err(|e| e.to_string())?;
9393
let mut missing_vars = Vec::new();
94-
95-
let result = reg.replace_all(&content, |caps: &regex::Captures| {
96-
let var_name = &caps[1];
97-
match env::var(var_name) {
98-
Ok(value) => value,
99-
Err(_) => {
100-
missing_vars.push(var_name.to_string());
101-
format!("${{{}}}", var_name) //keeping the same value there, will returned in error
102-
}
94+
let mut result = String::new();
95+
96+
for line in content.lines() {
97+
if !line.trim_start().starts_with('#') {
98+
let processed_line = reg.replace_all(line, |caps: &regex::Captures| {
99+
let var_name = &caps[1];
100+
match env::var(var_name) {
101+
Ok(value) => value,
102+
Err(_) => {
103+
missing_vars.push(var_name.to_string());
104+
format!("${{{}}}", var_name)
105+
}
106+
}
107+
});
108+
result.push_str(&processed_line);
109+
result.push('\n');
103110
}
104-
});
105-
// returning error if corrosponding env variables are not found
111+
}
112+
106113
if !missing_vars.is_empty() {
107-
return Err(format!("Missing environment variables: {}", missing_vars.join(", ")));
114+
return Err(format!(
115+
"Missing environment variables: {}",
116+
missing_vars.join(", ")
117+
));
108118
}
109-
Ok(result.into_owned())
119+
120+
Ok(result.trim_end().to_string())
110121
}
111122

112123
// custom validation of the values
@@ -393,7 +404,7 @@ mod tests {
393404
.unwrap_err();
394405

395406
let test_value = "http://localhost:8000/testvalue";
396-
std::env::set_var("INDEXER_SERVICE_SUBGRAPHS__NETWORK__QUERY_URL", test_value);
407+
env::set_var("INDEXER_SERVICE_SUBGRAPHS__NETWORK__QUERY_URL", test_value);
397408

398409
let config = Config::parse(
399410
ConfigPrefix::Service,
@@ -428,7 +439,7 @@ mod tests {
428439
// Test to check substitute_env_vars function is substituting env variables
429440
// indexers can use ${ENV_VAR_NAME} to point to the required env variable
430441
#[test]
431-
fn test_substitute_env_vars() {
442+
fn test_substitution_using_regex() {
432443
// Set up environment variables
433444
env::set_var("TEST_VAR1", "changed_value_1");
434445

@@ -459,11 +470,62 @@ mod tests {
459470
let result = Config::substitute_env_vars(input).expect("error substiting env variables");
460471

461472
assert_eq!(
462-
result, expected_output,
473+
result.trim(),
474+
expected_output.trim(),
463475
"Environment variable substitution failed"
464476
);
465477

466478
// Clean up environment variables
467479
env::remove_var("TEST_VAR1");
468480
}
481+
#[sealed_test(files = ["minimal-config-example.toml"])]
482+
fn test_parse_with_env_substitution_and_overrides() {
483+
let mut minimal_config: toml::Value = toml::from_str(
484+
fs::read_to_string("minimal-config-example.toml")
485+
.unwrap()
486+
.as_str(),
487+
)
488+
.unwrap();
489+
// Change the subgraphs query_url to an env variable
490+
minimal_config
491+
.get_mut("subgraphs")
492+
.unwrap()
493+
.get_mut("network")
494+
.unwrap()
495+
.as_table_mut()
496+
.unwrap()
497+
.insert(
498+
String::from("query_url"),
499+
toml::Value::String("${QUERY_URL}".to_string()),
500+
);
501+
502+
// Save the modified minimal config to a named temporary file using tempfile
503+
let temp_minimal_config_path = tempfile::NamedTempFile::new().unwrap();
504+
fs::write(
505+
temp_minimal_config_path.path(),
506+
toml::to_string(&minimal_config).unwrap(),
507+
)
508+
.unwrap();
509+
510+
// This should fail because the QUERY_URL env variable is not setup
511+
Config::parse(
512+
ConfigPrefix::Service,
513+
&PathBuf::from(temp_minimal_config_path.path()),
514+
)
515+
.unwrap_err();
516+
517+
let test_value = "http://localhost:8000/testvalue";
518+
env::set_var("QUERY_URL", test_value);
519+
520+
let config = Config::parse(
521+
ConfigPrefix::Service,
522+
&PathBuf::from(temp_minimal_config_path.path()),
523+
)
524+
.unwrap();
525+
526+
assert_eq!(
527+
config.subgraphs.network.config.query_url.as_str(),
528+
test_value
529+
);
530+
}
469531
}

0 commit comments

Comments
 (0)