Skip to content

Commit 796a20d

Browse files
committed
Add toolchain keyword override support
1 parent 4741579 commit 796a20d

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

src/cli/rustup_mode.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub fn main() -> Result<()> {
3434
let quiet = matches.is_present("quiet");
3535
let cfg = &common::set_globals(verbose, quiet)?;
3636

37+
if matches.is_present("keyword") {
38+
override_by_keyword(cfg, &matches)?;
39+
}
40+
3741
if maybe_upgrade_data(cfg, &matches)? {
3842
return Ok(());
3943
}
@@ -130,10 +134,8 @@ pub fn cli() -> App<'static, 'static> {
130134
.long("quiet"),
131135
)
132136
.arg(
133-
Arg::with_name("toolchain")
134-
.help(TOOLCHAIN_ARG_HELP)
135-
.long("toolchain")
136-
.takes_value(true),
137+
Arg::with_name("keyword")
138+
.help("keyword for release channel (e.g. +stable) or custom toolchain to set override"),
137139
)
138140
.subcommand(
139141
SubCommand::with_name("dump-testament")
@@ -819,8 +821,7 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
819821

820822
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
821823
let binary = m.value_of("command").expect("");
822-
let toolchain_provided = m.is_present("toolchain");
823-
let binary_path = if toolchain_provided {
824+
let binary_path = if m.is_present("toolchain") {
824825
let toolchain = m.value_of("toolchain").expect("");
825826
cfg.which_binary_by_toolchain(toolchain, binary)?
826827
.expect("binary not found")
@@ -1148,6 +1149,29 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11481149
Ok(())
11491150
}
11501151

1152+
fn override_by_keyword(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
1153+
let keyword = m.value_of("keyword").expect("");
1154+
let toolchain = cfg.get_toolchain(&keyword[1..], false)?;
1155+
1156+
let status = if !toolchain.is_custom() {
1157+
Some(toolchain.install_from_dist_if_not_installed()?)
1158+
} else if !toolchain.exists() {
1159+
return Err(ErrorKind::ToolchainNotInstalled(toolchain.name().to_string()).into());
1160+
} else {
1161+
None
1162+
};
1163+
1164+
let path = utils::current_dir()?;
1165+
toolchain.make_override(&path)?;
1166+
1167+
if let Some(status) = status {
1168+
println!();
1169+
common::show_channel_update(cfg, toolchain.name(), Ok(status))?;
1170+
}
1171+
1172+
Ok(())
1173+
}
1174+
11511175
fn override_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11521176
let toolchain = m.value_of("toolchain").expect("");
11531177
let toolchain = cfg.get_toolchain(toolchain, false)?;

tests/cli-misc.rs

+69-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],
@@ -905,3 +905,68 @@ fn which() {
905905
);
906906
});
907907
}
908+
909+
#[test]
910+
fn override_by_keyword_arg_with_which() {
911+
setup(&|config| {
912+
#[cfg(windows)]
913+
expect_stdout_ok(
914+
config,
915+
&["rustup", "+stable", "which", "rustc"],
916+
"\\toolchains\\stable-x86_64-",
917+
);
918+
#[cfg(windows)]
919+
expect_stdout_ok(
920+
config,
921+
&["rustup", "+stable", "which", "rustc"],
922+
"\\bin\\rustc",
923+
);
924+
#[cfg(not(windows))]
925+
expect_stdout_ok(
926+
config,
927+
&["rustup", "+stable", "which", "rustc"],
928+
"/toolchains/stable-x86_64-",
929+
);
930+
#[cfg(not(windows))]
931+
expect_stdout_ok(
932+
config,
933+
&["rustup", "+stable", "which", "rustc"],
934+
"/bin/rustc",
935+
);
936+
expect_ok(config, &["rustup", "default", "nightly"]);
937+
#[cfg(windows)]
938+
expect_stdout_ok(
939+
config,
940+
&["rustup", "+nightly", "which", "rustc"],
941+
"\\toolchains\\nightly-x86_64-",
942+
);
943+
#[cfg(windows)]
944+
expect_stdout_ok(
945+
config,
946+
&["rustup", "+nightly", "which", "rustc"],
947+
"\\bin\\rustc",
948+
);
949+
#[cfg(not(windows))]
950+
expect_stdout_ok(
951+
config,
952+
&["rustup", "+nightly", "which", "rustc"],
953+
"/toolchains/nightly-x86_64-",
954+
);
955+
#[cfg(not(windows))]
956+
expect_stdout_ok(
957+
config,
958+
&["rustup", "+nightly", "which", "rustc"],
959+
"/bin/rustc",
960+
);
961+
expect_stderr_ok(
962+
config,
963+
&["rustup", "+stable", "which", "rustc"],
964+
"set to 'stable-x86_64-",
965+
);
966+
expect_err(
967+
config,
968+
&["rustup", "+foo", "which", "rustc"],
969+
"toolchain 'foo' is not installed",
970+
);
971+
});
972+
}

0 commit comments

Comments
 (0)