diff --git a/src/rustup-cli/self_update.rs b/src/rustup-cli/self_update.rs index 2a6c998ebb..77c1c11118 100644 --- a/src/rustup-cli/self_update.rs +++ b/src/rustup-cli/self_update.rs @@ -981,7 +981,11 @@ fn get_add_path_methods() -> Vec { if let Ok(shell) = env::var("SHELL") { if shell.contains("zsh") { - let zprofile = utils::home_dir().map(|p| p.join(".zprofile")); + let zdotdir = env::var("ZDOTDIR") + .ok() + .map(PathBuf::from) + .or_else(utils::home_dir); + let zprofile = zdotdir.map(|p| p.join(".zprofile")); profiles.push(zprofile); } } diff --git a/src/rustup-mock/src/clitools.rs b/src/rustup-mock/src/clitools.rs index 37ce7f1de2..be4e3bd9ff 100644 --- a/src/rustup-mock/src/clitools.rs +++ b/src/rustup-mock/src/clitools.rs @@ -63,6 +63,7 @@ pub fn setup(s: Scenario, f: &Fn(&Config)) { // Unset env variables that will break our testing env::remove_var("RUSTUP_TOOLCHAIN"); env::remove_var("SHELL"); + env::remove_var("ZDOTDIR"); let exedir = TempDir::new("rustup-exe").unwrap(); let distdir = TempDir::new("rustup-dist").unwrap(); diff --git a/tests/cli-self-upd.rs b/tests/cli-self-upd.rs index c8cde87e33..3860c8672a 100644 --- a/tests/cli-self-upd.rs +++ b/tests/cli-self-upd.rs @@ -323,6 +323,48 @@ fn install_adds_path_to_profile() { install_adds_path_to_rc(".profile"); } +#[test] +#[cfg(unix)] +fn install_with_zsh_adds_path_to_zprofile() { + setup(&|config| { + let my_rc = "foo\nbar\nbaz"; + let ref rc = config.homedir.join(".zprofile"); + raw::write_file(rc, my_rc).unwrap(); + + let mut cmd = clitools::cmd(config, "rustup-init", &["-y"]); + cmd.env("SHELL", "zsh"); + assert!(cmd.output().unwrap().status.success()); + + let new_rc = raw::read_file(rc).unwrap(); + let addition = format!(r#"export PATH="{}/bin:$PATH""#, + config.cargodir.display()); + let expected = format!("{}\n{}\n", my_rc, addition); + assert_eq!(new_rc, expected); + }); +} + +#[test] +#[cfg(unix)] +fn install_with_zsh_adds_path_to_zdotdir_zprofile() { + setup(&|config| { + let zdotdir = TempDir::new("zdotdir").unwrap(); + let my_rc = "foo\nbar\nbaz"; + let ref rc = zdotdir.path().join(".zprofile"); + raw::write_file(rc, my_rc).unwrap(); + + let mut cmd = clitools::cmd(config, "rustup-init", &["-y"]); + cmd.env("SHELL", "zsh"); + cmd.env("ZDOTDIR", zdotdir.path()); + assert!(cmd.output().unwrap().status.success()); + + let new_rc = raw::read_file(rc).unwrap(); + let addition = format!(r#"export PATH="{}/bin:$PATH""#, + config.cargodir.display()); + let expected = format!("{}\n{}\n", my_rc, addition); + assert_eq!(new_rc, expected); + }); +} + #[test] #[cfg(unix)] fn install_adds_path_to_rcfile_just_once() {