Skip to content

Commit

Permalink
Set up ~/.multirust as a temporary symlink to ~/.rustup
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Dec 1, 2016
1 parent 418c512 commit a07ee8c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/rustup-cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ pub fn install(no_prompt: bool, verbose: bool,
if !opts.no_modify_path {
try!(do_add_to_path(&get_add_path_methods()));
}
// Create ~/.rustup and a compatibility ~/.multirust symlink.
// FIXME: Someday we can stop setting up the symlink, and when
// we do that we can stop creating ~/.rustup as well.
try!(utils::create_rustup_home());
try!(maybe_install_rust(&opts.default_toolchain, &opts.default_host_triple, verbose));

if cfg!(unix) {
Expand Down Expand Up @@ -607,6 +611,8 @@ pub fn uninstall(no_prompt: bool) -> Result<()> {

info!("removing rustup home");

try!(utils::delete_legacy_multirust_symlink());

// Delete RUSTUP_HOME
let ref rustup_dir = try!(utils::multirust_home());
if rustup_dir.exists() {
Expand Down
4 changes: 2 additions & 2 deletions src/rustup-mock/src/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ pub fn env(config: &Config, cmd: &mut Command) {
cmd.env("CARGO_HOME", config.cargodir.to_string_lossy().to_string());
cmd.env("RUSTUP_OVERRIDE_HOST_TRIPLE", this_host_triple());

// This is only used for some installation tests on unix where CARGO_HOME
// above is unset
// These are used in some installation tests that unset RUSTUP_HOME/CARGO_HOME
cmd.env("HOME", config.homedir.to_string_lossy().to_string());
cmd.env("USERPROFILE", config.homedir.to_string_lossy().to_string());

// Setting HOME will confuse the sudo check for rustup-init. Override it
cmd.env("RUSTUP_INIT_SKIP_SUDO_CHECK", "yes");
Expand Down
55 changes: 54 additions & 1 deletion src/rustup-utils/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,14 @@ pub fn do_rustup_home_upgrade() -> bool {
old_rustup_dir_removed && if multirust_dir_exists() {
if rustup_dir_exists() {
// There appears to be both a ~/.multirust dir and a valid ~/.rustup
// dir. Weird situation. Pick ~/.rustup.
// dir. Most likely because one is a symlink to the other, as configured
// below.
true
} else {
if rename_multirust_dir_to_rustup().is_ok() {
// Finally, making the hardlink from ~/.multirust back to
// ~/.rustup, for temporary compatibility.
let _ = create_legacy_multirust_symlink();
true
} else {
false
Expand All @@ -612,6 +616,51 @@ pub fn do_rustup_home_upgrade() -> bool {
}
}

// Creates a ~/.rustup folder and a ~/.multirust symlink
pub fn create_rustup_home() -> Result<()> {
// If RUSTUP_HOME is set then don't make any assumptions about where it's
// ok to put ~/.multirust
if env::var_os("RUSTUP_HOME").is_some() { return Ok(()) }

let home = rustup_home_in_user_dir()?;
fs::create_dir_all(&home)
.chain_err(|| "unable to create ~/.rustup")?;

// This is a temporary compatibility symlink
create_legacy_multirust_symlink()?;

Ok(())
}

// Create a symlink from ~/.multirust to ~/.rustup to temporarily
// accomodate old tools that are expecting that directory
fn create_legacy_multirust_symlink() -> Result<()> {
let newhome = rustup_home_in_user_dir()?;
let oldhome = legacy_multirust_home()?;

raw::symlink_dir(&newhome, &oldhome)
.chain_err(|| format!("unable to symlink {} from {}",
newhome.display(), oldhome.display()))?;

Ok(())
}

pub fn delete_legacy_multirust_symlink() -> Result<()> {
let oldhome = legacy_multirust_home()?;

if oldhome.exists() {
let meta = fs::symlink_metadata(&oldhome)
.chain_err(|| "unable to get metadata for ~/.multirust")?;
if meta.file_type().is_symlink() {
// remove_dir handles unlinking symlinks
raw::remove_dir(&oldhome)
.chain_err(|| format!("unable to delete legacy symlink {}", oldhome.display()))?;
}
}

Ok(())
}

fn dot_dir(name: &str) -> Option<PathBuf> {
home_dir().map(|p| p.join(name))
}
Expand All @@ -620,6 +669,10 @@ pub fn legacy_multirust_home() -> Result<PathBuf> {
dot_dir(".multirust").ok_or(ErrorKind::MultirustHome.into())
}

pub fn rustup_home_in_user_dir() -> Result<PathBuf> {
dot_dir(".rustup").ok_or(ErrorKind::MultirustHome.into())
}

pub fn multirust_home() -> Result<PathBuf> {
let use_rustup_dir = do_rustup_home_upgrade();

Expand Down
49 changes: 36 additions & 13 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ fn remove_component() {
// Run without setting RUSTUP_HOME, with setting HOME and USERPROFILE
fn run_no_home(config: &Config, args: &[&str], env: &[(&str, &str)]) -> process::Output {
let home_dir_str = &format!("{}", config.homedir.display());
let mut cmd = clitools::cmd(config, "rustup", args);
let mut cmd = clitools::cmd(config, args[0], &args[1..]);
clitools::env(config, &mut cmd);
cmd.env_remove("RUSTUP_HOME");
cmd.env("HOME", home_dir_str);
Expand All @@ -612,9 +612,9 @@ fn multirust_dir_upgrade_rename_multirust_dir_to_rustup() {
let multirust_dir_str = &format!("{}", multirust_dir.display());

// First write data into ~/.multirust
run_no_home(config, &["default", "stable"],
run_no_home(config, &["rustup", "default", "stable"],
&[("RUSTUP_HOME", multirust_dir_str)]);
let out = run_no_home(config, &["toolchain", "list"],
let out = run_no_home(config, &["rustup", "toolchain", "list"],
&[("RUSTUP_HOME", multirust_dir_str)]);
assert!(String::from_utf8(out.stdout).unwrap().contains("stable"));

Expand All @@ -624,10 +624,11 @@ fn multirust_dir_upgrade_rename_multirust_dir_to_rustup() {
// Next run without RUSTUP_DIR, but with HOME/USERPROFILE set so rustup
// can infer RUSTUP_DIR. It will silently move ~/.multirust to
// ~/.rustup.
let out = run_no_home(config, &["toolchain", "list"], &[]);
let out = run_no_home(config, &["rustup", "toolchain", "list"], &[]);
assert!(String::from_utf8(out.stdout).unwrap().contains("stable"));

assert!(!multirust_dir.exists());
assert!(multirust_dir.exists());
assert!(fs::symlink_metadata(&multirust_dir).unwrap().file_type().is_symlink());
assert!(rustup_dir.exists());
});
}
Expand All @@ -645,9 +646,9 @@ fn multirust_dir_upgrade_old_rustup_exists() {
let new_rustup_sh_version_file = rustup_sh_dir.join("rustup-version");

// First write data into ~/.multirust
run_no_home(config, &["default", "stable"],
run_no_home(config, &["rustup", "default", "stable"],
&[("RUSTUP_HOME", multirust_dir_str)]);
let out = run_no_home(config, &["toolchain", "list"],
let out = run_no_home(config, &["rustup", "toolchain", "list"],
&[("RUSTUP_HOME", multirust_dir_str)]);
assert!(String::from_utf8(out.stdout).unwrap().contains("stable"));

Expand All @@ -660,10 +661,11 @@ fn multirust_dir_upgrade_old_rustup_exists() {
assert!(old_rustup_sh_version_file.exists());

// Now do the upgrade, and ~/.rustup will be moved to ~/.rustup.sh
let out = run_no_home(config, &["toolchain", "list"], &[]);
let out = run_no_home(config, &["rustup", "toolchain", "list"], &[]);
assert!(String::from_utf8(out.stdout).unwrap().contains("stable"));

assert!(!multirust_dir.exists());
assert!(multirust_dir.exists());
assert!(fs::symlink_metadata(&multirust_dir).unwrap().file_type().is_symlink());
assert!(rustup_dir.exists());
assert!(!old_rustup_sh_version_file.exists());
assert!(new_rustup_sh_version_file.exists());
Expand All @@ -684,9 +686,9 @@ fn multirust_dir_upgrade_old_rustup_existsand_new_rustup_sh_exists() {
let new_rustup_sh_version_file = rustup_sh_dir.join("rustup-version");

// First write data into ~/.multirust
run_no_home(config, &["default", "stable"],
run_no_home(config, &["rustup", "default", "stable"],
&[("RUSTUP_HOME", multirust_dir_str)]);
let out = run_no_home(config, &["toolchain", "list"],
let out = run_no_home(config, &["rustup", "toolchain", "list"],
&[("RUSTUP_HOME", multirust_dir_str)]);
assert!(String::from_utf8(out.stdout).unwrap().contains("stable"));

Expand All @@ -708,12 +710,33 @@ fn multirust_dir_upgrade_old_rustup_existsand_new_rustup_sh_exists() {
assert!(new_rustup_sh_version_file.exists());

// Now do the upgrade, and ~/.rustup will be moved to ~/.rustup.sh
let out = run_no_home(config, &["toolchain", "list"], &[]);
let out = run_no_home(config, &["rustup", "toolchain", "list"], &[]);
assert!(String::from_utf8(out.stdout).unwrap().contains("stable"));

assert!(!multirust_dir.exists());
// .multirust is now a symlink to .rustup
assert!(multirust_dir.exists());
assert!(fs::symlink_metadata(&multirust_dir).unwrap().file_type().is_symlink());

assert!(rustup_dir.exists());
assert!(!old_rustup_sh_version_file.exists());
assert!(new_rustup_sh_version_file.exists());
});
}

#[test]
fn multirust_upgrade_works_with_proxy() {
setup(&|config| {
let multirust_dir = config.homedir.join(".multirust");
let rustup_dir = config.homedir.join(".rustup");

// Put data in ~/.multirust
run_no_home(config, &["rustup", "default", "stable"],
&[("RUSTUP_HOME", &format!("{}", multirust_dir.display()))]);

run_no_home(config, &["rustc", "--version"], &[]);

assert!(multirust_dir.exists());
assert!(fs::symlink_metadata(&multirust_dir).unwrap().file_type().is_symlink());
assert!(rustup_dir.exists());
});
}
38 changes: 36 additions & 2 deletions tests/cli-self-upd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,9 @@ fn install_sets_up_stable_unless_there_is_already_a_default() {
#[cfg(unix)]
fn install_deletes_legacy_multirust_bins() {
setup(&|config| {
let ref multirust_bin_dir = config.rustupdir.join("bin");
let ref multirust_bin_dir = config.homedir.join(".multirust/bin");
fs::create_dir_all(multirust_bin_dir).unwrap();
let ref multirust_bin = multirust_bin_dir.join("rustup");
let ref multirust_bin = multirust_bin_dir.join("multirust");
let ref rustc_bin = multirust_bin_dir.join("rustc");
raw::write_file(multirust_bin, "").unwrap();
raw::write_file(rustc_bin, "").unwrap();
Expand Down Expand Up @@ -1076,3 +1076,37 @@ fn legacy_upgrade_removes_multirust_bin() {
assert!(!multirust_bin.exists());
});
}

// Create a ~/.multirust symlink to ~/.rustup
#[test]
fn install_creates_legacy_home_symlink() {
setup(&|config| {
let mut cmd = clitools::cmd(config, "rustup-init", &["-y"]);
// It'll only do this behavior when RUSTUP_HOME isn't set
cmd.env_remove("RUSTUP_HOME");

assert!(cmd.output().unwrap().status.success());

let rustup_dir = config.homedir.join(".rustup");
assert!(rustup_dir.exists());
let multirust_dir = config.homedir.join(".multirust");
assert!(multirust_dir.exists());
assert!(fs::symlink_metadata(&multirust_dir).unwrap().file_type().is_symlink());
});
}

#[test]
fn uninstall_removes_legacy_home_symlink() {
setup(&|config| {
let mut cmd = clitools::cmd(config, "rustup-init", &["-y"]);
// It'll only do this behavior when RUSTUP_HOME isn't set
cmd.env_remove("RUSTUP_HOME");
assert!(cmd.output().unwrap().status.success());

let multirust_dir = config.homedir.join(".multirust");
assert!(multirust_dir.exists());

expect_ok(config, &["rustup", "self", "uninstall", "-y"]);
assert!(!multirust_dir.exists());
});
}

0 comments on commit a07ee8c

Please sign in to comment.