Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --toolchain option to rustup which #2030

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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)?;

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_toolchain(
&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
43 changes: 43 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
}