Skip to content

Commit 01b8809

Browse files
committed
test_config_loading_from_multiple_sources fixed
1 parent 896ed4b commit 01b8809

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

src/config.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ mod test {
184184
use std::io::Write;
185185
use tempfile::Builder;
186186

187+
use std::sync::Mutex;
188+
189+
static ENV_LOCK: Mutex<()> = Mutex::new(());
190+
187191
const CONFIG_STR1: &str = "
188192
datasource:
189193
url: 'https:/a.b'
@@ -275,6 +279,7 @@ mod test {
275279
/// Test merging config with env vars
276280
#[test]
277281
fn test_merge_env() {
282+
let _lock = ENV_LOCK.lock().unwrap();
278283
// Create a file inside of `std::env::temp_dir()`.
279284
let mut config_file = Builder::new().suffix(".yaml").tempfile().unwrap();
280285

@@ -283,7 +288,7 @@ mod test {
283288
env::set_var("MP_STATUS_DASHBOARD__SECRET", "val");
284289
let _config = config::Config::new(config_file.path().to_str().unwrap()).unwrap();
285290
assert_eq!(_config.status_dashboard.unwrap().secret.unwrap(), "val");
286-
291+
287292
// Clean up to avoid affecting other tests
288293
env::remove_var("MP_STATUS_DASHBOARD__SECRET");
289294
}
@@ -359,13 +364,13 @@ mod test {
359364
health_metrics: {}
360365
";
361366
let config = config::Config::from_config_str(minimal_config);
362-
367+
363368
// Verify default server address
364369
assert_eq!("0.0.0.0", config.server.address);
365-
370+
366371
// Verify default server port
367372
assert_eq!(3000, config.server.port);
368-
373+
369374
// Verify default datasource timeout
370375
assert_eq!(10, config.datasource.timeout);
371376
}
@@ -385,7 +390,7 @@ mod test {
385390
health_metrics: {}
386391
";
387392
let config = config::Config::from_config_str(config_str);
388-
393+
389394
let socket_addr = config.get_socket_addr();
390395
assert_eq!("127.0.0.1:8080", socket_addr.to_string());
391396
}
@@ -395,15 +400,16 @@ mod test {
395400
/// but we add an explicit comprehensive test
396401
#[test]
397402
fn test_config_loading_from_multiple_sources() {
403+
let _lock = ENV_LOCK.lock().unwrap();
398404
// Create temporary directory structure
399405
let dir = Builder::new().tempdir().unwrap();
400406
let main_config_path = dir.path().join("config.yaml");
401407
let mut main_config = File::create(&main_config_path).unwrap();
402-
408+
403409
// Create conf.d directory
404410
let confd_path = dir.path().join("conf.d");
405411
create_dir(&confd_path).expect("Cannot create conf.d");
406-
412+
407413
// Write main config with all required fields
408414
let main_config_content = "
409415
datasource:
@@ -421,7 +427,10 @@ mod test {
421427
- name: prod
422428
health_metrics: {}
423429
";
424-
main_config.write_all(main_config_content.as_bytes()).unwrap();
430+
main_config
431+
.write_all(main_config_content.as_bytes())
432+
.unwrap();
433+
main_config.sync_all().unwrap();
425434
// Ensure file is flushed and closed before reading
426435
drop(main_config);
427436

@@ -436,33 +445,36 @@ mod test {
436445
- name: prod
437446
";
438447
let mut flags_config = File::create(confd_path.join("flags.yaml")).unwrap();
439-
flags_config.write_all(flags_config_content.as_bytes()).unwrap();
448+
flags_config
449+
.write_all(flags_config_content.as_bytes())
450+
.unwrap();
451+
flags_config.sync_all().unwrap();
440452
// Ensure file is flushed and closed before reading
441453
drop(flags_config);
442454

443455
// Set environment variables to ensure required fields are present
444456
// This makes the test independent of any pre-existing env vars
445457
env::set_var("MP_DATASOURCE__URL", "https://graphite.example.com");
446458
env::set_var("MP_SERVER__PORT", "8080");
447-
459+
448460
// Load config from all sources
449461
let config = config::Config::new(main_config_path.to_str().unwrap()).unwrap();
450-
462+
451463
// Verify datasource config (env var ensures this is set)
452464
assert_eq!("https://graphite.example.com", config.datasource.url);
453465
assert_eq!(10, config.datasource.timeout);
454-
466+
455467
// Verify conf.d part merged
456468
assert_eq!(1, config.flag_metrics.len());
457469
assert_eq!("test-metric", config.flag_metrics[0].name);
458-
470+
459471
// Verify environment variable merged (overrides main config)
460472
assert_eq!(8080, config.server.port);
461-
473+
462474
// Clean up environment variables
463475
env::remove_var("MP_DATASOURCE__URL");
464476
env::remove_var("MP_SERVER__PORT");
465-
477+
466478
// Cleanup
467479
dir.close().unwrap();
468480
}

0 commit comments

Comments
 (0)