Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlx-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ num-bigint = { version = "0.4.3", optional = true }
smallvec = { version = "1.13.1" }
stringprep = "0.1.2"
tracing = { version = "0.1.37", features = ["log"] }
whoami = { version = "2.0.2", default-features = false }
whoami = { version = "2.0.2", default-features = false, features = ["std"] }

dotenvy.workspace = true
thiserror.workspace = true
Expand Down
32 changes: 32 additions & 0 deletions sqlx-postgres/src/options/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use sqlx_core::Url;
use std::net::IpAddr;
use std::str::FromStr;

#[cfg(all(test, unix, not(target_arch = "wasm32")))]
use std::sync::Mutex;

impl PgConnectOptions {
pub(crate) fn parse_from_url(url: &Url) -> Result<Self, Error> {
let mut options = Self::new_without_pgpass();
Expand Down Expand Up @@ -340,3 +343,32 @@ fn built_url_can_be_parsed() {

assert!(parsed.is_ok());
}

#[cfg(all(test, unix, not(target_arch = "wasm32")))]
struct PgUserTestGuard(Option<std::ffi::OsString>);

#[cfg(all(test, unix, not(target_arch = "wasm32")))]
impl Drop for PgUserTestGuard {
fn drop(&mut self) {
if let Some(old_pguser) = &self.0 {
std::env::set_var("PGUSER", old_pguser);
} else {
std::env::remove_var("PGUSER");
}
}
}

#[test]
#[cfg(all(unix, not(target_arch = "wasm32")))]
fn it_uses_the_os_username_when_url_omits_user() {
static ENV_LOCK: Mutex<()> = Mutex::new(());

let _guard = ENV_LOCK.lock().unwrap();
let _pguser_guard = PgUserTestGuard(std::env::var_os("PGUSER"));

std::env::remove_var("PGUSER");
Comment on lines +364 to +369
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially racy with other tests that may check similar behavior. Instead, it would probably be better to mark this #[ignore] and add a CI step that tests for this specifically.


let opts = PgConnectOptions::from_str("postgresql:///database").unwrap();

assert_ne!(opts.username, "anonymous");
}
Loading