Skip to content

Commit

Permalink
Remove env vars in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 21, 2024
1 parent 138c545 commit 129483d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
64 changes: 27 additions & 37 deletions crates/uv-settings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsStr;
use std::ops::Deref;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -210,20 +211,24 @@ fn locate_system_config_xdg(value: Option<&str>) -> Option<PathBuf> {
None
}

#[cfg(windows)]
fn locate_system_config_windows(system_drive: &OsStr) -> Option<PathBuf> {
// On Windows, use `%SYSTEMDRIVE%\ProgramData\uv\uv.toml` (e.g., `C:\ProgramData`).
let candidate = PathBuf::from(system_drive).join("ProgramData\\uv\\uv.toml");
candidate.as_path().is_file().then_some(candidate)
}

/// Returns the path to the system configuration file.
///
/// Unix-like systems: This uses the `XDG_CONFIG_DIRS` environment variable in *nix systems.
/// If the var is not present it will check /etc/xdg/uv/uv.toml and then /etc/uv/uv.toml.
/// Windows: "%SYSTEMDRIVE%\ProgramData\uv\uv.toml" is used.
/// On Unix-like systems, uses the `XDG_CONFIG_DIRS` environment variable (falling back to
/// `/etc/xdg/uv/uv.toml` if unset or empty) and then `/etc/uv/uv.toml`
///
/// On Windows, uses `%SYSTEMDRIVE%\ProgramData\uv\uv.toml`.
fn system_config_file() -> Option<PathBuf> {
// On Windows, use, e.g., `C:\ProgramData`.
#[cfg(windows)]
{
if let Ok(system_drive) = env::var(EnvVars::SYSTEMDRIVE) {
let candidate = PathBuf::from(system_drive).join("ProgramData\\uv\\uv.toml");
return candidate.as_path().is_file().then_some(candidate);
}
None
env::var_os(EnvVars::SYSTEMDRIVE)
.and_then(|system_drive| locate_system_config_windows(&system_drive))
}

#[cfg(not(windows))]
Expand All @@ -233,6 +238,7 @@ fn system_config_file() -> Option<PathBuf> {
{
return Some(path);
}

// Fallback to `/etc/uv/uv.toml` if `XDG_CONFIG_DIRS` is not set or no valid
// path is found.
let candidate = Path::new("/etc/uv/uv.toml");
Expand Down Expand Up @@ -262,17 +268,14 @@ pub enum Error {

#[cfg(test)]
mod test {
#[cfg(windows)]
use crate::locate_system_config_windows;
#[cfg(not(windows))]
use crate::locate_system_config_xdg;
#[cfg(windows)]
use crate::system_config_file;
#[cfg(windows)]
use uv_static::EnvVars;

use assert_fs::fixture::FixtureError;
use assert_fs::prelude::*;
use indoc::indoc;
use std::env;

#[test]
#[cfg(not(windows))]
Expand All @@ -286,22 +289,13 @@ mod test {
"#,
})?;

let current_dir = env::current_dir().unwrap();
// None
assert_eq!(locate_system_config_xdg(None), None);

{
env::set_current_dir(context.path()).unwrap();
// Empty string
assert_eq!(locate_system_config_xdg(Some("")), None);

// None
assert_eq!(locate_system_config_xdg(None), None);

// Empty string
assert_eq!(locate_system_config_xdg(Some("")), None);

// Single colon
assert_eq!(locate_system_config_xdg(Some(":")), None);
}

env::set_current_dir(&current_dir).unwrap();
// Single colon
assert_eq!(locate_system_config_xdg(Some(":")), None);

// Assert that the `system_config_file` function returns the correct path.
Expand Down Expand Up @@ -340,14 +334,10 @@ mod test {
index-url = "https://test.pypi.org/simple"
"#})?;

// Previous value of `SYSTEMDRIVE` which should always exist.
let prev_system_drive = env::var(EnvVars::SYSTEMDRIVE).unwrap_or("C:".to_string());

// This is typically only a drive (that is, letter and colon) but we
// allow anything, including a path to the test fixtures...
env::set_var(EnvVars::SYSTEMDRIVE, context.path().as_os_str());
assert_eq!(
system_config_file().unwrap(),
locate_system_config_windows(context.path().as_os_str()).unwrap(),
context
.child("ProgramData")
.child("uv")
Expand All @@ -357,10 +347,10 @@ mod test {

// This does not have a `ProgramData` child, so contains no config.
let context = assert_fs::TempDir::new()?;
env::set_var(EnvVars::SYSTEMDRIVE, context.path().as_os_str());
assert_eq!(system_config_file(), None);

env::set_var(EnvVars::SYSTEMDRIVE, prev_system_drive);
assert_eq!(
locate_system_config_windows(context.path().as_os_str()),
None
);

Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ format, as a `pyproject.toml` is intended to define a Python _project_.

If project-, user-, and system-level configuration files are found, the settings will be merged,
with project-level configuration taking precedence over the user-level configuration, and user-level
configuration taking precedence over the system-level configuration.
configuration taking precedence over the system-level configuration. (If multiple system-level
configuration files are found, e.g., at both `/etc/uv/uv.toml` and `$XDG_CONFIG_DIRS/uv/uv.toml`,
only the first-discovered file will be used, with XDG taking priority.)

For example, if a string, number, or boolean is present in both the project- and user-level
configuration tables, the project-level value will be used, and the user-level value will be
Expand Down

0 comments on commit 129483d

Please sign in to comment.