diff --git a/Cargo.lock b/Cargo.lock index 9a87f6a75..c631e6ad1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4206,6 +4206,7 @@ dependencies = [ "url", "urlencoding", "uv-once-map", + "uv-static", "wiremock", ] diff --git a/crates/uv-auth/Cargo.toml b/crates/uv-auth/Cargo.toml index ea63f5125..24d75582b 100644 --- a/crates/uv-auth/Cargo.toml +++ b/crates/uv-auth/Cargo.toml @@ -26,6 +26,8 @@ tracing = { workspace = true } url = { workspace = true } urlencoding = { workspace = true } +uv-static = { workspace = true } + [dev-dependencies] tempfile = { workspace = true } tokio = { workspace = true } diff --git a/crates/uv-auth/src/credentials.rs b/crates/uv-auth/src/credentials.rs index 144b8c64e..91aa48103 100644 --- a/crates/uv-auth/src/credentials.rs +++ b/crates/uv-auth/src/credentials.rs @@ -9,6 +9,8 @@ use std::io::Read; use std::io::Write; use url::Url; +use uv_static::EnvVars; + #[derive(Clone, Debug, PartialEq)] pub struct Credentials { /// The name of the user for authentication. @@ -145,8 +147,8 @@ impl Credentials { /// `UV_HTTP_BASIC_PYTORCH_PASSWORD`. pub fn from_env(name: &str) -> Option { let name = name.to_uppercase(); - let username = std::env::var(format!("UV_HTTP_BASIC_{name}_USERNAME")).ok(); - let password = std::env::var(format!("UV_HTTP_BASIC_{name}_PASSWORD")).ok(); + let username = std::env::var(EnvVars::http_basic_username(&name)).ok(); + let password = std::env::var(EnvVars::http_basic_password(&name)).ok(); if username.is_none() && password.is_none() { None } else { diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 8343e0270..d51686d28 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -3880,7 +3880,7 @@ pub struct IndexArgs { /// All indexes provided via this flag take priority over the index specified by /// `--default-index` (which defaults to PyPI). When multiple `--index` flags are /// provided, earlier values take priority. - #[arg(long, env = "UV_INDEX", value_delimiter = ' ', value_parser = parse_index, help_heading = "Index options")] + #[arg(long, env = EnvVars::UV_INDEX, value_delimiter = ' ', value_parser = parse_index, help_heading = "Index options")] pub index: Option>>, /// The URL of the default package index (by default: ). @@ -3890,7 +3890,7 @@ pub struct IndexArgs { /// /// The index given by this flag is given lower priority than all other indexes specified via /// the `--index` flag. - #[arg(long, env = "UV_DEFAULT_INDEX", value_parser = parse_default_index, help_heading = "Index options")] + #[arg(long, env = EnvVars::UV_DEFAULT_INDEX, value_parser = parse_default_index, help_heading = "Index options")] pub default_index: Option>, /// (Deprecated: use `--default-index` instead) The URL of the Python package index (by default: ). diff --git a/crates/uv-static/src/env_vars.rs b/crates/uv-static/src/env_vars.rs index 2e2511e51..64fa737c0 100644 --- a/crates/uv-static/src/env_vars.rs +++ b/crates/uv-static/src/env_vars.rs @@ -2,10 +2,20 @@ pub struct EnvVars; impl EnvVars { + /// Equivalent to the `--default-index` argument. Base index URL for searching packages. + pub const UV_DEFAULT_INDEX: &'static str = "UV_DEFAULT_INDEX"; + + /// Equivalent to the `--index` argument. Additional indexes for searching packages. + pub const UV_INDEX: &'static str = "UV_INDEX"; + /// Equivalent to the `--index-url` argument. Base index URL for searching packages. + /// + /// Deprecated: use `UV_DEFAULT_INDEX` instead. pub const UV_INDEX_URL: &'static str = "UV_INDEX_URL"; /// Equivalent to the `--extra-index-url` argument. Additional indexes for searching packages. + /// + /// Deprecated: use `UV_INDEX` instead. pub const UV_EXTRA_INDEX_URL: &'static str = "UV_EXTRA_INDEX_URL"; /// Equivalent to the `--find-links` argument. Additional package search locations. @@ -149,6 +159,16 @@ impl EnvVars { /// Use to control the stack size used by uv. Typically more relevant for Windows in debug mode. pub const UV_STACK_SIZE: &'static str = "UV_STACK_SIZE"; + /// Generates the environment variable key for the HTTP Basic authentication username. + pub fn http_basic_username(name: &str) -> String { + format!("UV_HTTP_BASIC_{name}_USERNAME") + } + + /// Generates the environment variable key for the HTTP Basic authentication password. + pub fn http_basic_password(name: &str) -> String { + format!("UV_HTTP_BASIC_{name}_PASSWORD") + } + /// Used to set the uv commit hash at build time via `build.rs`. pub const UV_COMMIT_HASH: &'static str = "UV_COMMIT_HASH"; @@ -164,6 +184,12 @@ impl EnvVars { /// Used to set the uv tag distance from head at build time via `build.rs`. pub const UV_LAST_TAG_DISTANCE: &'static str = "UV_LAST_TAG_DISTANCE"; + /// Used in tests for testing providing credentials via environment variables. + pub const UV_HTTP_BASIC_PROXY_USERNAME: &'static str = "UV_HTTP_BASIC_PROXY_USERNAME"; + + /// Used in tests for testing providing credentials via environment variables. + pub const UV_HTTP_BASIC_PROXY_PASSWORD: &'static str = "UV_HTTP_BASIC_PROXY_PASSWORD"; + /// Used to set the spawning/parent interpreter when using --system in the test suite. pub const UV_INTERNAL__PARENT_INTERPRETER: &'static str = "UV_INTERNAL__PARENT_INTERPRETER"; diff --git a/crates/uv/tests/it/lock.rs b/crates/uv/tests/it/lock.rs index 059e62781..c47de0151 100644 --- a/crates/uv/tests/it/lock.rs +++ b/crates/uv/tests/it/lock.rs @@ -6475,8 +6475,8 @@ fn lock_env_credentials() -> Result<()> { // Provide credentials via environment variables. uv_snapshot!(context.filters(), context.lock() - .env("UV_HTTP_BASIC_PROXY_USERNAME", "public") - .env("UV_HTTP_BASIC_PROXY_PASSWORD", "heron"), @r###" + .env(EnvVars::UV_HTTP_BASIC_PROXY_USERNAME, "public") + .env(EnvVars::UV_HTTP_BASIC_PROXY_PASSWORD, "heron"), @r###" success: true exit_code: 0 ----- stdout ----- diff --git a/crates/uv/tests/it/pip_install.rs b/crates/uv/tests/it/pip_install.rs index 454c36a34..7cd2a771c 100644 --- a/crates/uv/tests/it/pip_install.rs +++ b/crates/uv/tests/it/pip_install.rs @@ -2327,7 +2327,7 @@ fn no_prerelease_hint_source_builds() -> Result<()> { build-backend = "setuptools.build_meta" "#})?; - uv_snapshot!(context.filters(), context.pip_install().arg(".").env("UV_EXCLUDE_NEWER", "2018-10-08"), @r###" + uv_snapshot!(context.filters(), context.pip_install().arg(".").env(EnvVars::UV_EXCLUDE_NEWER, "2018-10-08"), @r###" success: false exit_code: 2 ----- stdout -----