Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ windows-registry = { workspace = true }

[dev-dependencies]
fs-err = { workspace = true }
temp-env = { workspace = true }
tempfile = { workspace = true }
109 changes: 103 additions & 6 deletions crates/uv-shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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" };
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.

nit: I'd use EnvVars::USERPROFILE and EnvVars::HOME here.


#[test]
fn configuration_files_zsh_no_existing_zshenv() {
let tmp_home_dir = tempdir().unwrap();
let tmp_zdotdir = tempdir().unwrap();

with_vars(
[
("ZDOTDIR", None),
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.

nit: I'd use EnvVars::ZDOTDIR here.

(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()),
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.

nit: I'd use EnvVars::ZDOTDIR here.

(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),
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.

nit: I'd use EnvVars::ZDOTDIR here.

(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()),
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.

nit: I'd use EnvVars::ZDOTDIR here.

(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()),
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.

nit: I'd use EnvVars::ZDOTDIR here.

(HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()),
],
|| {
assert_eq!(
Shell::Zsh.configuration_files(),
vec![tmp_zdotdir.path().join(".zshenv")]
);
},
);
}
}
Loading