Skip to content

Commit

Permalink
Support --toolchain option to rustup which
Browse files Browse the repository at this point in the history
Update code format by cargo fmt for cli-misc.rs
  • Loading branch information
BeniCheni committed Sep 30, 2019
1 parent 1844a92 commit b2f8140
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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_explicit_toolchain_path(toolchain, binary)?
.expect("binary not found")
} else {
cfg.which_binary(&utils::current_dir()?, binary)?
.expect("binary not found")
};

utils::assert_is_file(&binary_path)?;

Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ impl Cfg {
Ok(self.update_hash_dir.join(toolchain))
}

pub fn which_binary_by_explicit_toolchain_path(
&self,
toolchain: &str,
binary: &str,
) -> Result<Option<PathBuf>> {
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<Option<PathBuf>> {
if let Some((toolchain, _)) = self.find_override_toolchain_or_default(path)? {
Ok(Some(toolchain.binary_file(binary)))
Expand Down
29 changes: 29 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,3 +862,32 @@ 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"]);
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],
);
expect_stdout_ok(
config,
&["rustup", "which", "--toolchain=custom-2", "rustc"],
"/toolchains/custom-2/bin/rustc",
);
});
}

0 comments on commit b2f8140

Please sign in to comment.