Skip to content

Commit d0b5b8b

Browse files
committed
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
1 parent 1844a92 commit d0b5b8b

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

src/cli/rustup_mode.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ pub fn cli() -> App<'static, 'static> {
129129
.short("q")
130130
.long("quiet"),
131131
)
132+
.arg(
133+
Arg::with_name("toolchain")
134+
.help(TOOLCHAIN_ARG_HELP)
135+
.long("toolchain")
136+
.takes_value(true),
137+
)
132138
.subcommand(
133139
SubCommand::with_name("dump-testament")
134140
.about("Dump information about the build")
@@ -451,7 +457,13 @@ pub fn cli() -> App<'static, 'static> {
451457
.subcommand(
452458
SubCommand::with_name("which")
453459
.about("Display which binary will be run for a given command")
454-
.arg(Arg::with_name("command").required(true)),
460+
.arg(Arg::with_name("command").required(true))
461+
.arg(
462+
Arg::with_name("toolchain")
463+
.help(TOOLCHAIN_ARG_HELP)
464+
.long("toolchain")
465+
.takes_value(true),
466+
),
455467
)
456468
.subcommand(
457469
SubCommand::with_name("doc")
@@ -783,10 +795,15 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
783795

784796
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
785797
let binary = m.value_of("command").expect("");
786-
787-
let binary_path = cfg
788-
.which_binary(&utils::current_dir()?, binary)?
789-
.expect("binary not found");
798+
let toolchain_provided = m.is_present("toolchain");
799+
let binary_path = if toolchain_provided {
800+
let toolchain = m.value_of("toolchain").expect("");
801+
cfg.which_binary_by_toolchain(toolchain, binary)?
802+
.expect("binary not found")
803+
} else {
804+
cfg.which_binary(&utils::current_dir()?, binary)?
805+
.expect("binary not found")
806+
};
790807

791808
utils::assert_is_file(&binary_path)?;
792809

src/config.rs

+13
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ impl Cfg {
183183
Ok(self.update_hash_dir.join(toolchain))
184184
}
185185

186+
pub fn which_binary_by_toolchain(
187+
&self,
188+
toolchain: &str,
189+
binary: &str,
190+
) -> Result<Option<PathBuf>> {
191+
let toolchain = self.get_toolchain(toolchain, false)?;
192+
if toolchain.exists() {
193+
Ok(Some(toolchain.binary_file(binary)))
194+
} else {
195+
Ok(None)
196+
}
197+
}
198+
186199
pub fn which_binary(&self, path: &Path, binary: &str) -> Result<Option<PathBuf>> {
187200
if let Some((toolchain, _)) = self.find_override_toolchain_or_default(path)? {
188201
Ok(Some(toolchain.binary_file(binary)))

tests/cli-misc.rs

+43
Original file line numberDiff line numberDiff line change
@@ -862,3 +862,46 @@ fn add_remove_component() {
862862
expect_component_executable(config, "rustc");
863863
});
864864
}
865+
866+
#[test]
867+
fn which() {
868+
setup(&|config| {
869+
let path_1 = config.customdir.join("custom-1");
870+
let path_1 = path_1.to_string_lossy();
871+
expect_ok(
872+
config,
873+
&["rustup", "toolchain", "link", "custom-1", &path_1],
874+
);
875+
expect_ok(config, &["rustup", "default", "custom-1"]);
876+
#[cfg(windows)]
877+
expect_stdout_ok(
878+
config,
879+
&["rustup", "which", "rustc"],
880+
"\\toolchains\\custom-1\\bin\\rustc",
881+
);
882+
#[cfg(not(windows))]
883+
expect_stdout_ok(
884+
config,
885+
&["rustup", "which", "rustc"],
886+
"/toolchains/custom-1/bin/rustc",
887+
);
888+
let path_2 = config.customdir.join("custom-2");
889+
let path_2 = path_2.to_string_lossy();
890+
expect_ok(
891+
config,
892+
&["rustup", "toolchain", "link", "custom-2", &path_2],
893+
);
894+
#[cfg(windows)]
895+
expect_stdout_ok(
896+
config,
897+
&["rustup", "which", "--toolchain=custom-2", "rustc"],
898+
"\\toolchains\\custom-2\\bin\\rustc",
899+
);
900+
#[cfg(not(windows))]
901+
expect_stdout_ok(
902+
config,
903+
&["rustup", "which", "--toolchain=custom-2", "rustc"],
904+
"/toolchains/custom-2/bin/rustc",
905+
);
906+
});
907+
}

0 commit comments

Comments
 (0)