-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Prefer updating existing .zshenv over creating a new one in tool update-shell
#16866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd use |
||
| (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()), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd use |
||
| (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), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd use |
||
| (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()), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd use |
||
| (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()), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd use |
||
| (HOME_DIR_ENV_VAR, tmp_home_dir.path().to_str()), | ||
| ], | ||
| || { | ||
| assert_eq!( | ||
| Shell::Zsh.configuration_files(), | ||
| vec![tmp_zdotdir.path().join(".zshenv")] | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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::USERPROFILEandEnvVars::HOMEhere.