Skip to content

Commit

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

if matches.is_present("keyword") {
override_by_keyword(cfg, &matches)?;
}

if maybe_upgrade_data(cfg, &matches)? {
return Ok(());
}
Expand Down Expand Up @@ -130,10 +134,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("keyword")
.help("keyword for release channel (e.g. +stable) or custom toolchain to set override"),
)
.subcommand(
SubCommand::with_name("dump-testament")
Expand Down Expand Up @@ -819,8 +821,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 @@ -1148,6 +1149,29 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
Ok(())
}

fn override_by_keyword(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let keyword = m.value_of("keyword").expect("");
let toolchain = cfg.get_toolchain(&keyword[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
73 changes: 69 additions & 4 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ fn add_remove_component() {
#[test]
fn which() {
setup(&|config| {
let path_1 = config.customdir.join("custom-1");
let path_1 = path_1.to_string_lossy();
let _path_1 = config.customdir.join("custom-1");
let path_1 = _path_1.to_string_lossy();
expect_ok(
config,
&["rustup", "toolchain", "link", "custom-1", &path_1],
Expand All @@ -885,8 +885,8 @@ fn which() {
&["rustup", "which", "rustc"],
"/toolchains/custom-1/bin/rustc",
);
let path_2 = config.customdir.join("custom-2");
let path_2 = path_2.to_string_lossy();
let _path_2 = config.customdir.join("custom-2");
let path_2 = _path_2.to_string_lossy();
expect_ok(
config,
&["rustup", "toolchain", "link", "custom-2", &path_2],
Expand All @@ -905,3 +905,68 @@ fn which() {
);
});
}

#[test]
fn override_by_keyword_arg_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",
);
});
}

0 comments on commit 94f639e

Please sign in to comment.