Skip to content

Commit

Permalink
Add toolchain override support
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniCheni committed Oct 9, 2019
1 parent b9e968b commit 48b5837
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub fn main() -> Result<()> {
let quiet = matches.is_present("quiet");
let cfg = &common::set_globals(verbose, quiet)?;

if matches.is_present("+toolchain") {
override_by_toolchain(cfg, &matches)?;
}

if maybe_upgrade_data(cfg, &matches)? {
return Ok(());
}
Expand Down Expand Up @@ -131,10 +135,8 @@ pub fn cli() -> App<'static, 'static> {
.long("quiet"),
)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
Arg::with_name("+toolchain")
.help("release channel (e.g. +stable) or custom toolchain to set override"),
)
.subcommand(
SubCommand::with_name("dump-testament")
Expand Down Expand Up @@ -824,8 +826,7 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {

fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let binary = m.value_of("command").expect("");
let toolchain_provided = m.is_present("toolchain");
let binary_path = if toolchain_provided {
let binary_path = if m.is_present("toolchain") {
let toolchain = m.value_of("toolchain").expect("");
cfg.which_binary_by_toolchain(toolchain, binary)?
.expect("binary not found")
Expand Down Expand Up @@ -1153,6 +1154,29 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
Ok(())
}

fn override_by_toolchain(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let toolchain = m.value_of("+toolchain").expect("");
let toolchain = cfg.get_toolchain(&toolchain[1..], false)?;

let status = if !toolchain.is_custom() {
Some(toolchain.install_from_dist_if_not_installed()?)
} else if !toolchain.exists() {
return Err(ErrorKind::ToolchainNotInstalled(toolchain.name().to_string()).into());
} else {
None
};

let path = utils::current_dir()?;
toolchain.make_override(&path)?;

if let Some(status) = status {
println!();
common::show_channel_update(cfg, toolchain.name(), Ok(status))?;
}

Ok(())
}

fn override_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let toolchain = m.value_of("toolchain").expect("");
let toolchain = cfg.get_toolchain(toolchain, false)?;
Expand Down
71 changes: 71 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,74 @@ fn which() {
);
});
}

#[test]
fn override_by_toolchain_with_which() {
setup(&|config| {
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"\\toolchains\\stable-x86_64-",
);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"\\bin\\rustc",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"/toolchains/stable-x86_64-",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"/bin/rustc",
);
expect_ok(config, &["rustup", "default", "nightly"]);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "+nightly", "which", "rustc"],
"\\toolchains\\nightly-x86_64-",
);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "+nightly", "which", "rustc"],
"\\bin\\rustc",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "+nightly", "which", "rustc"],
"/toolchains/nightly-x86_64-",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "+nightly", "which", "rustc"],
"/bin/rustc",
);
expect_stderr_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"set to 'stable-x86_64-",
);
expect_err(
config,
&["rustup", "+foo", "which", "rustc"],
"toolchain 'foo' is not installed",
);
expect_stderr_ok(
config,
&["rustup", "+stable", "set", "profile", "minimal"],
"profile set to 'minimal'",
);
expect_stdout_ok(config, &["rustup", "default"], "nightly-x86_64-");
});
}

0 comments on commit 48b5837

Please sign in to comment.