From d0b5b8b22b17e099e9d01251deae1ac907c07f0b Mon Sep 17 00:00:00 2001 From: Benjamin Chen Date: Sun, 29 Sep 2019 22:34:14 -0400 Subject: [PATCH] Support --toolchain option to rustup which Update code format by cargo fmt for cli-misc.rs Test both windows and non-windows which() in cli-misc.rs --- src/cli/rustup_mode.rs | 27 +++++++++++++++++++++----- src/config.rs | 13 +++++++++++++ tests/cli-misc.rs | 43 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 790abbba74..4d395a66b1 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -129,6 +129,12 @@ pub fn cli() -> App<'static, 'static> { .short("q") .long("quiet"), ) + .arg( + Arg::with_name("toolchain") + .help(TOOLCHAIN_ARG_HELP) + .long("toolchain") + .takes_value(true), + ) .subcommand( SubCommand::with_name("dump-testament") .about("Dump information about the build") @@ -451,7 +457,13 @@ pub fn cli() -> App<'static, 'static> { .subcommand( SubCommand::with_name("which") .about("Display which binary will be run for a given command") - .arg(Arg::with_name("command").required(true)), + .arg(Arg::with_name("command").required(true)) + .arg( + Arg::with_name("toolchain") + .help(TOOLCHAIN_ARG_HELP) + .long("toolchain") + .takes_value(true), + ), ) .subcommand( SubCommand::with_name("doc") @@ -783,10 +795,15 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> { fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> { let binary = m.value_of("command").expect(""); - - let binary_path = cfg - .which_binary(&utils::current_dir()?, binary)? - .expect("binary not found"); + let toolchain_provided = m.is_present("toolchain"); + let binary_path = if toolchain_provided { + let toolchain = m.value_of("toolchain").expect(""); + cfg.which_binary_by_toolchain(toolchain, binary)? + .expect("binary not found") + } else { + cfg.which_binary(&utils::current_dir()?, binary)? + .expect("binary not found") + }; utils::assert_is_file(&binary_path)?; diff --git a/src/config.rs b/src/config.rs index bb065f9469..99ccda4980 100644 --- a/src/config.rs +++ b/src/config.rs @@ -183,6 +183,19 @@ impl Cfg { Ok(self.update_hash_dir.join(toolchain)) } + pub fn which_binary_by_toolchain( + &self, + toolchain: &str, + binary: &str, + ) -> Result> { + let toolchain = self.get_toolchain(toolchain, false)?; + if toolchain.exists() { + Ok(Some(toolchain.binary_file(binary))) + } else { + Ok(None) + } + } + pub fn which_binary(&self, path: &Path, binary: &str) -> Result> { if let Some((toolchain, _)) = self.find_override_toolchain_or_default(path)? { Ok(Some(toolchain.binary_file(binary))) diff --git a/tests/cli-misc.rs b/tests/cli-misc.rs index 862eb4f9fc..228403fc19 100644 --- a/tests/cli-misc.rs +++ b/tests/cli-misc.rs @@ -862,3 +862,46 @@ fn add_remove_component() { expect_component_executable(config, "rustc"); }); } + +#[test] +fn which() { + setup(&|config| { + 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], + ); + expect_ok(config, &["rustup", "default", "custom-1"]); + #[cfg(windows)] + expect_stdout_ok( + config, + &["rustup", "which", "rustc"], + "\\toolchains\\custom-1\\bin\\rustc", + ); + #[cfg(not(windows))] + expect_stdout_ok( + config, + &["rustup", "which", "rustc"], + "/toolchains/custom-1/bin/rustc", + ); + 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], + ); + #[cfg(windows)] + expect_stdout_ok( + config, + &["rustup", "which", "--toolchain=custom-2", "rustc"], + "\\toolchains\\custom-2\\bin\\rustc", + ); + #[cfg(not(windows))] + expect_stdout_ok( + config, + &["rustup", "which", "--toolchain=custom-2", "rustc"], + "/toolchains/custom-2/bin/rustc", + ); + }); +}