Skip to content
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

do not get cwd if not env set #1379

Merged
merged 3 commits into from
May 27, 2018
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
3 changes: 3 additions & 0 deletions src/rustup-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ error_chain! {
description("failed to set permissions")
display("failed to set permissions for '{}'", path.display())
}
GettingCwd {
description("couldn't get current working directory")
}
CargoHome {
description("couldn't find value of CARGO_HOME")
}
Expand Down
35 changes: 31 additions & 4 deletions src/rustup-utils/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,15 @@ pub fn cargo_home() -> Result<PathBuf> {
None
};

let cwd = env::current_dir().chain_err(|| ErrorKind::CargoHome)?;
let cargo_home = env_var.clone().map(|home| cwd.join(home));
let cargo_home = if env_var.is_some() {
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::GettingCwd));
env_var.clone().map(|home| {
cwd.join(home)
})
} else {
None
};

let user_home = home_dir().map(|p| p.join(".cargo"));
cargo_home.or(user_home).ok_or(ErrorKind::CargoHome.into())
}
Expand Down Expand Up @@ -716,9 +723,17 @@ pub fn rustup_home_in_user_dir() -> Result<PathBuf> {

pub fn rustup_home() -> Result<PathBuf> {
let use_rustup_dir = do_rustup_home_upgrade();
let rustup_home_env = env::var_os("RUSTUP_HOME");

let rustup_home = if rustup_home_env.is_some() {
let cwd = try!(env::current_dir().chain_err(|| ErrorKind::GettingCwd));
rustup_home_env.clone().map(|home| {
cwd.join(home)
})
} else {
None
};

let cwd = env::current_dir().chain_err(|| ErrorKind::RustupHome)?;
let rustup_home = env::var_os("RUSTUP_HOME").map(|home| cwd.join(home));
let user_home = if use_rustup_dir {
dot_dir(".rustup")
} else {
Expand Down Expand Up @@ -822,6 +837,18 @@ fn rename(name: &'static str, src: &Path, dest: &Path) -> Result<()> {
mod tests {
use super::*;

#[test]
fn test_cargo_home() {
// CARGO_HOME unset, we'll get the default ending in /.cargo
let cargo_home1 = cargo_home();
let ch = format!("{}", cargo_home1.unwrap().display());
assert!(ch.contains("/.cargo"));

env::set_var("CARGO_HOME", "/test");
let cargo_home2 = cargo_home();
assert_eq!("/test", format!("{}", cargo_home2.unwrap().display()));
}

#[test]
fn test_toochain_sort() {
let expected = vec![
Expand Down