Skip to content

Commit

Permalink
Avoid prompting on terminals during publish tests (#10496)
Browse files Browse the repository at this point in the history
## Summary

Closes #10493.

## Test Plan

Run `cargo test --profile fast-build --no-fail-fast -p uv
username_password_sources` from a terminal.
  • Loading branch information
charliermarsh authored Jan 11, 2025
1 parent 54b3a43 commit e57acc5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions crates/uv/src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub(crate) async fn publish(
keyring_provider,
&oidc_client,
check_url.as_ref(),
Prompt::Enabled,
printer,
)
.await?;
Expand Down Expand Up @@ -150,6 +151,14 @@ pub(crate) async fn publish(
Ok(ExitStatus::Success)
}

/// Whether to allow prompting for username and password.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Prompt {
Enabled,
#[allow(dead_code)]
Disabled,
}

/// Unify the different possible source for username and password information.
///
/// Returns the publish URL, the username and the password.
Expand All @@ -161,6 +170,7 @@ async fn gather_credentials(
keyring_provider: KeyringProviderType,
oidc_client: &BaseClient,
check_url: Option<&IndexUrl>,
prompt: Prompt,
printer: Printer,
) -> Result<(Url, Option<String>, Option<String>)> {
// Support reading username and password from the URL, for symmetry with the index API.
Expand Down Expand Up @@ -200,7 +210,10 @@ async fn gather_credentials(
(Some("__token__".to_string()), Some(password.to_string()))
} else {
if username.is_none() && password.is_none() {
prompt_username_and_password()?
match prompt {
Prompt::Enabled => prompt_username_and_password()?,
Prompt::Disabled => (None, None),
}
} else {
(username, password)
}
Expand Down Expand Up @@ -289,8 +302,10 @@ fn prompt_username_and_password() -> Result<(Option<String>, Option<String>)> {
#[cfg(test)]
mod tests {
use super::*;
use insta::assert_snapshot;

use std::str::FromStr;

use insta::assert_snapshot;
use url::Url;

async fn credentials(
Expand All @@ -307,6 +322,7 @@ mod tests {
KeyringProviderType::Disabled,
&client,
None,
Prompt::Disabled,
Printer::Quiet,
)
.await
Expand Down

0 comments on commit e57acc5

Please sign in to comment.