diff --git a/Cargo.toml b/Cargo.toml index bdba14823..5b8b583d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,6 +118,7 @@ approx = "0.4.0" derive_more = "0.99.13" function_name = "0.2.0" futures = "0.3" +home = "0.5" pretty_assertions = "0.7.1" serde_json = "1.0.64" semver = "1.0.0" diff --git a/src/test/mod.rs b/src/test/mod.rs index 2a23fa8aa..9189c6335 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -27,6 +27,7 @@ pub(crate) use self::{ }, }; +use home::home_dir; use lazy_static::lazy_static; use self::util::TestLock; @@ -34,7 +35,7 @@ use crate::{ client::options::{ServerApi, ServerApiVersion}, options::ClientOptions, }; -use std::str::FromStr; +use std::{fs::read_to_string, str::FromStr}; const MAX_POOL_SIZE: u32 = 100; @@ -48,8 +49,7 @@ lazy_static! { options }; pub(crate) static ref LOCK: TestLock = TestLock::new(); - pub(crate) static ref DEFAULT_URI: String = - std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".to_string()); + pub(crate) static ref DEFAULT_URI: String = get_default_uri(); pub(crate) static ref SERVER_API: Option = match std::env::var("MONGODB_API_VERSION") { Ok(server_api_version) if !server_api_version.is_empty() => Some(ServerApi { @@ -62,3 +62,16 @@ lazy_static! { pub(crate) static ref SERVERLESS: bool = matches!(std::env::var("SERVERLESS"), Ok(s) if s == "serverless"); } + +fn get_default_uri() -> String { + if let Ok(uri) = std::env::var("MONGODB_URI") { + return uri; + } + if let Some(mut home) = home_dir() { + home.push(".mongodb_uri"); + if let Ok(uri) = read_to_string(home) { + return uri; + } + } + return "mongodb://localhost:27017".to_string(); +}