diff --git a/backend/src/config.rs b/backend/src/config.rs index a62743b..1fe7a81 100644 --- a/backend/src/config.rs +++ b/backend/src/config.rs @@ -630,6 +630,18 @@ mod tests { // (we set/unset within each guard region). static ENV_GUARD: Mutex<()> = Mutex::new(()); + /// Ensure `DATABASE_URL` is set for `Config::from_env()` WITHOUT clobbering + /// or removing an existing value. The `#[sqlx::test]` lib tests in other + /// modules read `DATABASE_URL` from the same process env and run in + /// parallel with these `ENV_GUARD`-serialised tests; overwriting it (bad + /// host) or removing it ("NotPresent") flaked them. CI already exports the + /// real URL, so this is a no-op there; locally it provides a placeholder. + fn ensure_database_url() { + if std::env::var_os("DATABASE_URL").is_none() { + std::env::set_var("DATABASE_URL", "postgres://test"); + } + } + #[test] fn crawler_limit_env_populates_manga_limit() { let _g = ENV_GUARD.lock().unwrap_or_else(|p| p.into_inner()); @@ -757,10 +769,13 @@ mod tests { fn private_mode_env_parses_true() { let _g = ENV_GUARD.lock().unwrap_or_else(|p| p.into_inner()); std::env::set_var("PRIVATE_MODE", "true"); - std::env::set_var("DATABASE_URL", "postgres://test"); + // Don't clobber/unset DATABASE_URL: parallel #[sqlx::test] lib tests + // read it from the process env and would flake ("NotPresent") on the + // removal. Ensure it's present (CI sets it) without overwriting a real + // one, and never remove it. + ensure_database_url(); let cfg = Config::from_env().expect("from_env"); std::env::remove_var("PRIVATE_MODE"); - std::env::remove_var("DATABASE_URL"); assert!(cfg.auth.private_mode); } @@ -768,10 +783,9 @@ mod tests { fn private_mode_env_parses_false() { let _g = ENV_GUARD.lock().unwrap_or_else(|p| p.into_inner()); std::env::set_var("PRIVATE_MODE", "false"); - std::env::set_var("DATABASE_URL", "postgres://test"); + ensure_database_url(); let cfg = Config::from_env().expect("from_env"); std::env::remove_var("PRIVATE_MODE"); - std::env::remove_var("DATABASE_URL"); assert!(!cfg.auth.private_mode); } @@ -779,9 +793,8 @@ mod tests { fn private_mode_defaults_to_false() { let _g = ENV_GUARD.lock().unwrap_or_else(|p| p.into_inner()); std::env::remove_var("PRIVATE_MODE"); - std::env::set_var("DATABASE_URL", "postgres://test"); + ensure_database_url(); let cfg = Config::from_env().expect("from_env"); - std::env::remove_var("DATABASE_URL"); assert!(!cfg.auth.private_mode); } }