Skip to content

Commit a9d7dad

Browse files
committed
Add release channel & --toolchain support to rustup which
Update "which" test
1 parent 4741579 commit a9d7dad

File tree

2 files changed

+117
-11
lines changed

2 files changed

+117
-11
lines changed

src/cli/rustup_mode.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ pub fn main() -> Result<()> {
3232
let matches = cli().get_matches();
3333
let verbose = matches.is_present("verbose");
3434
let quiet = matches.is_present("quiet");
35+
36+
let toolchain = if let Some(channel) = matches.value_of("toolchain-keyword") {
37+
if matches.is_present("toolchain-option") {
38+
warn_toolchain_keyword_option();
39+
}
40+
&channel[1..]
41+
} else {
42+
matches.value_of("toolchain-option").unwrap_or("")
43+
};
44+
3545
let cfg = &common::set_globals(verbose, quiet)?;
3646

3747
if maybe_upgrade_data(cfg, &matches)? {
@@ -79,7 +89,7 @@ pub fn main() -> Result<()> {
7989
(_, _) => unreachable!(),
8090
},
8191
("run", Some(m)) => run(cfg, m)?,
82-
("which", Some(m)) => which(cfg, m)?,
92+
("which", Some(m)) => which(cfg, m, toolchain)?,
8393
("doc", Some(m)) => doc(cfg, m)?,
8494
("man", Some(m)) => man(cfg, m)?,
8595
("self", Some(c)) => match c.subcommand() {
@@ -130,11 +140,15 @@ pub fn cli() -> App<'static, 'static> {
130140
.long("quiet"),
131141
)
132142
.arg(
133-
Arg::with_name("toolchain")
143+
Arg::with_name("toolchain-option")
134144
.help(TOOLCHAIN_ARG_HELP)
135145
.long("toolchain")
136146
.takes_value(true),
137147
)
148+
.arg(
149+
Arg::with_name("toolchain-keyword")
150+
.help("provide the release channel (e.g. +stable) or custom toolchain"),
151+
)
138152
.subcommand(
139153
SubCommand::with_name("dump-testament")
140154
.about("Dump information about the build")
@@ -475,7 +489,7 @@ pub fn cli() -> App<'static, 'static> {
475489
.about("Display which binary will be run for a given command")
476490
.arg(Arg::with_name("command").required(true))
477491
.arg(
478-
Arg::with_name("toolchain")
492+
Arg::with_name("toolchain-option")
479493
.help(TOOLCHAIN_ARG_HELP)
480494
.long("toolchain")
481495
.takes_value(true),
@@ -817,11 +831,16 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
817831
process::exit(c)
818832
}
819833

820-
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
834+
fn which(cfg: &Cfg, m: &ArgMatches<'_>, toolchain_by_root_cmd: &str) -> Result<()> {
821835
let binary = m.value_of("command").expect("");
822-
let toolchain_provided = m.is_present("toolchain");
823-
let binary_path = if toolchain_provided {
824-
let toolchain = m.value_of("toolchain").expect("");
836+
let binary_path = if !toolchain_by_root_cmd.is_empty() {
837+
if m.is_present("toolchain-option") {
838+
warn_toolchain_keyword_option();
839+
}
840+
cfg.which_binary_by_toolchain(toolchain_by_root_cmd, binary)?
841+
.expect("binary not found")
842+
} else if m.is_present("toolchain-option") {
843+
let toolchain = m.value_of("toolchain-option").expect("");
825844
cfg.which_binary_by_toolchain(toolchain, binary)?
826845
.expect("binary not found")
827846
} else {
@@ -1378,3 +1397,10 @@ fn output_completion_script(shell: Shell, command: CompletionCommand) -> Result<
13781397

13791398
Ok(())
13801399
}
1400+
1401+
fn warn_toolchain_keyword_option() {
1402+
warn!(
1403+
"Both the toolchain keyword (e.g. \"+nightly\" or custom toolchain) and the \"--toolchain\" option are provided.\n\
1404+
The toolchain keyword would take precedence over the \"--toolchain\" option.",
1405+
);
1406+
}

tests/cli-misc.rs

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,8 @@ fn add_remove_component() {
866866
#[test]
867867
fn which() {
868868
setup(&|config| {
869-
let path_1 = config.customdir.join("custom-1");
870-
let path_1 = path_1.to_string_lossy();
869+
let _path_1 = config.customdir.join("custom-1");
870+
let path_1 = _path_1.to_string_lossy();
871871
expect_ok(
872872
config,
873873
&["rustup", "toolchain", "link", "custom-1", &path_1],
@@ -885,8 +885,8 @@ fn which() {
885885
&["rustup", "which", "rustc"],
886886
"/toolchains/custom-1/bin/rustc",
887887
);
888-
let path_2 = config.customdir.join("custom-2");
889-
let path_2 = path_2.to_string_lossy();
888+
let _path_2 = config.customdir.join("custom-2");
889+
let path_2 = _path_2.to_string_lossy();
890890
expect_ok(
891891
config,
892892
&["rustup", "toolchain", "link", "custom-2", &path_2],
@@ -903,5 +903,85 @@ fn which() {
903903
&["rustup", "which", "--toolchain=custom-2", "rustc"],
904904
"/toolchains/custom-2/bin/rustc",
905905
);
906+
#[cfg(windows)]
907+
expect_stdout_ok(
908+
config,
909+
&["rustup", "--toolchain=custom-2", "which", "rustc"],
910+
"\\toolchains\\custom-2\\bin\\rustc",
911+
);
912+
#[cfg(not(windows))]
913+
expect_stdout_ok(
914+
config,
915+
&["rustup", "--toolchain=custom-2", "which", "rustc"],
916+
"/toolchains/custom-2/bin/rustc",
917+
);
918+
expect_ok(config, &["rustup", "default", "stable"]);
919+
#[cfg(windows)]
920+
expect_stdout_ok(
921+
config,
922+
&["rustup", "+stable", "which", "rustc"],
923+
"\\toolchains\\stable-x86_64-",
924+
);
925+
#[cfg(windows)]
926+
expect_stdout_ok(
927+
config,
928+
&["rustup", "+stable", "which", "rustc"],
929+
"\\bin\\rustc",
930+
);
931+
#[cfg(not(windows))]
932+
expect_stdout_ok(
933+
config,
934+
&["rustup", "+stable", "which", "rustc"],
935+
"/toolchains/stable-x86_64-",
936+
);
937+
#[cfg(not(windows))]
938+
expect_stdout_ok(
939+
config,
940+
&["rustup", "+stable", "which", "rustc"],
941+
"/bin/rustc",
942+
);
943+
expect_ok(config, &["rustup", "default", "nightly"]);
944+
#[cfg(windows)]
945+
expect_stdout_ok(
946+
config,
947+
&["rustup", "+nightly", "which", "rustc"],
948+
"\\toolchains\\nightly-x86_64-",
949+
);
950+
#[cfg(windows)]
951+
expect_stdout_ok(
952+
config,
953+
&["rustup", "+nightly", "which", "rustc"],
954+
"\\bin\\rustc",
955+
);
956+
#[cfg(not(windows))]
957+
expect_stdout_ok(
958+
config,
959+
&["rustup", "+nightly", "which", "rustc"],
960+
"/toolchains/nightly-x86_64-",
961+
);
962+
#[cfg(not(windows))]
963+
expect_stdout_ok(
964+
config,
965+
&["rustup", "+nightly", "which", "rustc"],
966+
"/bin/rustc",
967+
);
968+
969+
expect_err(
970+
config,
971+
&["rustup", "+foo", "which", "rustc"],
972+
"binary not found",
973+
);
974+
const WHICH_SUBCOMMAND_WARNING: &str = "warning: Both the toolchain keyword (e.g. \"+nightly\" or custom toolchain) and the \"--toolchain\" option are provided.\n\
975+
The toolchain keyword would take precedence over the \"--toolchain\" option.";
976+
expect_stderr_ok(
977+
config,
978+
&["rustup", "+nightly", "--toolchain=stable", "which", "cargo"],
979+
WHICH_SUBCOMMAND_WARNING,
980+
);
981+
expect_stderr_ok(
982+
config,
983+
&["rustup", "+nightly", "which", "cargo", "--toolchain=stable"],
984+
WHICH_SUBCOMMAND_WARNING,
985+
);
906986
});
907987
}

0 commit comments

Comments
 (0)