Skip to content

Commit 48b5837

Browse files
committed
Add toolchain override support
1 parent b9e968b commit 48b5837

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

src/cli/rustup_mode.rs

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

38+
if matches.is_present("+toolchain") {
39+
override_by_toolchain(cfg, &matches)?;
40+
}
41+
3842
if maybe_upgrade_data(cfg, &matches)? {
3943
return Ok(());
4044
}
@@ -131,10 +135,8 @@ pub fn cli() -> App<'static, 'static> {
131135
.long("quiet"),
132136
)
133137
.arg(
134-
Arg::with_name("toolchain")
135-
.help(TOOLCHAIN_ARG_HELP)
136-
.long("toolchain")
137-
.takes_value(true),
138+
Arg::with_name("+toolchain")
139+
.help("release channel (e.g. +stable) or custom toolchain to set override"),
138140
)
139141
.subcommand(
140142
SubCommand::with_name("dump-testament")
@@ -824,8 +826,7 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
824826

825827
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
826828
let binary = m.value_of("command").expect("");
827-
let toolchain_provided = m.is_present("toolchain");
828-
let binary_path = if toolchain_provided {
829+
let binary_path = if m.is_present("toolchain") {
829830
let toolchain = m.value_of("toolchain").expect("");
830831
cfg.which_binary_by_toolchain(toolchain, binary)?
831832
.expect("binary not found")
@@ -1153,6 +1154,29 @@ fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11531154
Ok(())
11541155
}
11551156

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

tests/cli-misc.rs

+71
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,74 @@ fn which() {
905905
);
906906
});
907907
}
908+
909+
#[test]
910+
fn override_by_toolchain_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+
expect_stderr_ok(
972+
config,
973+
&["rustup", "+stable", "set", "profile", "minimal"],
974+
"profile set to 'minimal'",
975+
);
976+
expect_stdout_ok(config, &["rustup", "default"], "nightly-x86_64-");
977+
});
978+
}

0 commit comments

Comments
 (0)