Skip to content

Commit b01351e

Browse files
authored
RUST-905 Try reading the default server URI from a local file if $MONGODB_URI is unset (#409)
1 parent 1fc1611 commit b01351e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ approx = "0.4.0"
118118
derive_more = "0.99.13"
119119
function_name = "0.2.0"
120120
futures = "0.3"
121+
home = "0.5"
121122
pretty_assertions = "0.7.1"
122123
serde_json = "1.0.64"
123124
semver = "1.0.0"

src/test/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ pub(crate) use self::{
2727
},
2828
};
2929

30+
use home::home_dir;
3031
use lazy_static::lazy_static;
3132

3233
use self::util::TestLock;
3334
use crate::{
3435
client::options::{ServerApi, ServerApiVersion},
3536
options::ClientOptions,
3637
};
37-
use std::str::FromStr;
38+
use std::{fs::read_to_string, str::FromStr};
3839

3940
const MAX_POOL_SIZE: u32 = 100;
4041

@@ -48,8 +49,7 @@ lazy_static! {
4849
options
4950
};
5051
pub(crate) static ref LOCK: TestLock = TestLock::new();
51-
pub(crate) static ref DEFAULT_URI: String =
52-
std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".to_string());
52+
pub(crate) static ref DEFAULT_URI: String = get_default_uri();
5353
pub(crate) static ref SERVER_API: Option<ServerApi> = match std::env::var("MONGODB_API_VERSION")
5454
{
5555
Ok(server_api_version) if !server_api_version.is_empty() => Some(ServerApi {
@@ -62,3 +62,16 @@ lazy_static! {
6262
pub(crate) static ref SERVERLESS: bool =
6363
matches!(std::env::var("SERVERLESS"), Ok(s) if s == "serverless");
6464
}
65+
66+
fn get_default_uri() -> String {
67+
if let Ok(uri) = std::env::var("MONGODB_URI") {
68+
return uri;
69+
}
70+
if let Some(mut home) = home_dir() {
71+
home.push(".mongodb_uri");
72+
if let Ok(uri) = read_to_string(home) {
73+
return uri;
74+
}
75+
}
76+
return "mongodb://localhost:27017".to_string();
77+
}

0 commit comments

Comments
 (0)