diff --git a/Cargo.lock b/Cargo.lock index 87425314c..92e52e467 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6770,6 +6770,7 @@ dependencies = [ "fs-err", "nix", "same-file", + "temp-env", "tempfile", "tracing", "uv-fs", diff --git a/crates/uv-shell/Cargo.toml b/crates/uv-shell/Cargo.toml index e9260a900..4ed1bf4cf 100644 --- a/crates/uv-shell/Cargo.toml +++ b/crates/uv-shell/Cargo.toml @@ -33,4 +33,5 @@ windows-registry = { workspace = true } [dev-dependencies] fs-err = { workspace = true } +temp-env = { workspace = true } tempfile = { workspace = true } diff --git a/crates/uv-shell/src/lib.rs b/crates/uv-shell/src/lib.rs index 828cc96cc..757e262ae 100644 --- a/crates/uv-shell/src/lib.rs +++ b/crates/uv-shell/src/lib.rs @@ -188,12 +188,11 @@ impl Shell { if zshenv.is_file() { return vec![zshenv]; } - } else { - // If `ZDOTDIR` is _not_ set, and `~/.zshenv` exists, then we update that file. - let zshenv = home_dir.join(".zshenv"); - if zshenv.is_file() { - return vec![zshenv]; - } + } + // Whether `ZDOTDIR` is set or not, if `~/.zshenv` exists then we update that file. + let zshenv = home_dir.join(".zshenv"); + if zshenv.is_file() { + return vec![zshenv]; } if let Some(zsh_dot_dir) = zsh_dot_dir.as_ref() { @@ -340,3 +339,101 @@ fn backtick_escape(s: &str) -> String { } escaped } + +#[cfg(test)] +mod tests { + use super::*; + use fs_err::File; + use temp_env::with_vars; + use tempfile::tempdir; + + // First option used by std::env::home_dir. + const HOME_DIR_ENV_VAR: &str = if cfg!(windows) { "USERPROFILE" } else { "HOME" }; + + #[test] + fn configuration_files_zsh_no_existing_zshenv() { + let tmp_home_dir = tempdir().unwrap(); + let tmp_zdotdir = tempdir().unwrap(); + + with_vars( + [ + ("ZDOTDIR", None), + (HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()), + ], + || { + assert_eq!( + Shell::Zsh.configuration_files(), + vec![tmp_home_dir.path().join(".zshenv")] + ); + }, + ); + + with_vars( + [ + ("ZDOTDIR", tmp_zdotdir.path().to_str()), + (HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()), + ], + || { + assert_eq!( + Shell::Zsh.configuration_files(), + vec![tmp_zdotdir.path().join(".zshenv")] + ); + }, + ); + } + + #[test] + fn configuration_files_zsh_existing_home_zshenv() { + let tmp_home_dir = tempdir().unwrap(); + File::create(tmp_home_dir.path().join(".zshenv")).unwrap(); + + let tmp_zdotdir = tempdir().unwrap(); + + with_vars( + [ + ("ZDOTDIR", None), + (HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()), + ], + || { + assert_eq!( + Shell::Zsh.configuration_files(), + vec![tmp_home_dir.path().join(".zshenv")] + ); + }, + ); + + with_vars( + [ + ("ZDOTDIR", tmp_zdotdir.path().to_str()), + (HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()), + ], + || { + assert_eq!( + Shell::Zsh.configuration_files(), + vec![tmp_home_dir.path().join(".zshenv")] + ); + }, + ); + } + + #[test] + fn configuration_files_zsh_existing_zdotdir_zshenv() { + let tmp_home_dir = tempdir().unwrap(); + + let tmp_zdotdir = tempdir().unwrap(); + File::create(tmp_zdotdir.path().join(".zshenv")).unwrap(); + + with_vars( + [ + ("ZDOTDIR", tmp_zdotdir.path().to_str()), + (HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()), + ], + || { + assert_eq!( + Shell::Zsh.configuration_files(), + vec![tmp_zdotdir.path().join(".zshenv")] + ); + }, + ); + } +}