Skip to content

Commit 6ef832f

Browse files
committed
Add toolchain override support
1 parent 66fdf0f commit 6ef832f

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

src/cli/rustup_mode.rs

Lines changed: 7 additions & 6 deletions
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 = &mut common::set_globals(verbose, quiet)?;
3737

38+
if let Some(t) = matches.value_of("+toolchain") {
39+
cfg.set_toolchain_override(&t[1..]);
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")
@@ -852,8 +854,7 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
852854

853855
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
854856
let binary = m.value_of("command").expect("");
855-
let toolchain_provided = m.is_present("toolchain");
856-
let binary_path = if toolchain_provided {
857+
let binary_path = if m.is_present("toolchain") {
857858
let toolchain = m.value_of("toolchain").expect("");
858859
cfg.which_binary_by_toolchain(toolchain, binary)?
859860
.expect("binary not found")

src/config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct Cfg {
4040
pub download_dir: PathBuf,
4141
pub temp_cfg: temp::Cfg,
4242
pub gpg_key: Cow<'static, str>,
43+
pub toolchain_override: Option<String>,
4344
pub env_override: Option<String>,
4445
pub dist_root_url: String,
4546
pub dist_root_server: String,
@@ -104,6 +105,7 @@ impl Cfg {
104105
temp_cfg,
105106
gpg_key,
106107
notify_handler,
108+
toolchain_override: None,
107109
env_override,
108110
dist_root_url: dist_root,
109111
dist_root_server,
@@ -144,6 +146,10 @@ impl Cfg {
144146
Ok(())
145147
}
146148

149+
pub fn set_toolchain_override(&mut self, toolchain_override: &str) {
150+
self.toolchain_override = Some(toolchain_override.to_owned());
151+
}
152+
147153
// Returns a profile, if one exists in the settings file.
148154
//
149155
// Returns `Err` if the settings file could not be read or the profile is
@@ -281,7 +287,12 @@ impl Cfg {
281287
pub fn find_override(&self, path: &Path) -> Result<Option<(Toolchain<'_>, OverrideReason)>> {
282288
let mut override_ = None;
283289

284-
// First check RUSTUP_TOOLCHAIN
290+
// First check toolchain override from command
291+
if let Some(ref name) = self.toolchain_override {
292+
override_ = Some((name.to_string(), OverrideReason::Environment));
293+
}
294+
295+
// Check RUSTUP_TOOLCHAIN
285296
if let Some(ref name) = self.env_override {
286297
override_ = Some((name.to_string(), OverrideReason::Environment));
287298
}

tests/cli-misc.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,69 @@ 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_err(
962+
config,
963+
&["rustup", "+foo", "which", "rustc"],
964+
"toolchain 'foo' is not installed",
965+
);
966+
expect_stderr_ok(
967+
config,
968+
&["rustup", "+stable", "set", "profile", "minimal"],
969+
"profile set to 'minimal'",
970+
);
971+
expect_stdout_ok(config, &["rustup", "default"], "nightly-x86_64-");
972+
});
973+
}

0 commit comments

Comments
 (0)