Skip to content

Commit 4ef8a1c

Browse files
committed
Add release channel & --toolchain support to rustup which
Update "which" test Refactor
1 parent 856da77 commit 4ef8a1c

File tree

2 files changed

+113
-8
lines changed

2 files changed

+113
-8
lines changed

src/cli/rustup_mode.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ 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("keyword") {
37+
if matches.is_present("toolchain") {
38+
warn!(
39+
"Both toolchain keyword (e.g. \"+nightly\") and \"--toolchain\" option are provided.\
40+
The toolchain keyword would take precedence.",
41+
);
42+
}
43+
&channel[1..]
44+
} else {
45+
matches.value_of("toolchain").unwrap_or("")
46+
};
47+
3548
let cfg = &common::set_globals(verbose, quiet)?;
3649

3750
if maybe_upgrade_data(cfg, &matches)? {
@@ -79,7 +92,7 @@ pub fn main() -> Result<()> {
7992
(_, _) => unreachable!(),
8093
},
8194
("run", Some(m)) => run(cfg, m)?,
82-
("which", Some(m)) => which(cfg, m)?,
95+
("which", Some(m)) => which(cfg, m, toolchain)?,
8396
("doc", Some(m)) => doc(cfg, m)?,
8497
("man", Some(m)) => man(cfg, m)?,
8598
("self", Some(c)) => match c.subcommand() {
@@ -135,6 +148,10 @@ pub fn cli() -> App<'static, 'static> {
135148
.long("toolchain")
136149
.takes_value(true),
137150
)
151+
.arg(
152+
Arg::with_name("keyword")
153+
.help("provide the release channel (e.g. +stable) or custom toolchain"),
154+
)
138155
.subcommand(
139156
SubCommand::with_name("dump-testament")
140157
.about("Dump information about the build")
@@ -817,10 +834,18 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
817834
process::exit(c)
818835
}
819836

820-
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
837+
fn which(cfg: &Cfg, m: &ArgMatches<'_>, toolchain_by_root_cmd: &str) -> Result<()> {
821838
let binary = m.value_of("command").expect("");
822-
let toolchain_provided = m.is_present("toolchain");
823-
let binary_path = if toolchain_provided {
839+
let binary_path = if !toolchain_by_root_cmd.is_empty() {
840+
if m.is_present("toolchain") {
841+
warn!(
842+
"Toolchain options are provided before and after the \"which\" subcommand. \
843+
The first toolchain option would take precedence.",
844+
);
845+
}
846+
cfg.which_binary_by_toolchain(toolchain_by_root_cmd, binary)?
847+
.expect("binary not found")
848+
} else if m.is_present("toolchain") {
824849
let toolchain = m.value_of("toolchain").expect("");
825850
cfg.which_binary_by_toolchain(toolchain, binary)?
826851
.expect("binary not found")

tests/cli-misc.rs

+84-4
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+
expect_stderr_ok(
975+
config,
976+
&["rustup", "+nightly", "--toolchain=stable", "which", "cargo"],
977+
"Both toolchain keyword (e.g. \"+nightly\") and \"--toolchain\" option are provided.\
978+
The toolchain keyword would take precedence.",
979+
);
980+
expect_stderr_ok(
981+
config,
982+
&["rustup", "--toolchain=nightly", "which", "cargo", "--toolchain=stable"],
983+
"Toolchain options are provided before and after the \"which\" subcommand. \
984+
The first toolchain option would take precedence.",
985+
);
906986
});
907987
}

0 commit comments

Comments
 (0)