Skip to content

Commit 38f4479

Browse files
authored
Merge pull request #2831 from jyn514/default-none
Add "rustup default none" as a way to unset the default toolchain
2 parents 511e6c8 + 73118b2 commit 38f4479

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

src/cli/rustup_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
853853
let status = if !toolchain.is_custom() {
854854
let distributable = DistributableToolchain::new(&toolchain)?;
855855
Some(distributable.install_from_dist_if_not_installed()?)
856-
} else if !toolchain.exists() {
856+
} else if !toolchain.exists() && toolchain.name() != "none" {
857857
return Err(RustupError::ToolchainNotInstalled(toolchain.name().to_string()).into());
858858
} else {
859859
None

src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,11 @@ impl Cfg {
382382

383383
pub fn set_default(&self, toolchain: &str) -> Result<()> {
384384
self.settings_file.with_mut(|s| {
385-
s.default_toolchain = Some(toolchain.to_owned());
385+
s.default_toolchain = if toolchain == "none" {
386+
None
387+
} else {
388+
Some(toolchain.to_owned())
389+
};
386390
Ok(())
387391
})?;
388392
(self.notify_handler)(Notification::SetDefaultToolchain(toolchain));

src/notifications.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl<'a> Display for Notification<'a> {
9696
Install(n) => n.fmt(f),
9797
Utils(n) => n.fmt(f),
9898
Temp(n) => n.fmt(f),
99+
SetDefaultToolchain("none") => write!(f, "default toolchain unset"),
99100
SetDefaultToolchain(name) => write!(f, "default toolchain set to '{}'", name),
100101
SetOverrideToolchain(path, name) => write!(
101102
f,

tests/cli-exact.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
pub mod mock;
55

66
use crate::mock::clitools::{
7-
self, check_update_setup, expect_err_ex, expect_ok, expect_ok_ex, expect_stdout_ok,
8-
self_update_setup, set_current_dist_date, Config, Scenario,
7+
self, check_update_setup, expect_err_ex, expect_ok, expect_ok_ex, expect_stderr_ok,
8+
expect_stdout_ok, self_update_setup, set_current_dist_date, Config, Scenario,
99
};
1010
use rustup::for_host;
1111
use rustup::test::this_host_triple;
@@ -587,6 +587,23 @@ error: target '2016-03-1' not found in channel. Perhaps check https://doc.rust-
587587
});
588588
}
589589

590+
#[test]
591+
fn default_none() {
592+
setup(&|config| {
593+
expect_stderr_ok(
594+
config,
595+
&["rustup", "default", "none"],
596+
"info: default toolchain unset",
597+
);
598+
expect_err_ex(
599+
config,
600+
&["rustc", "--version"],
601+
"",
602+
"error: no override and no default toolchain set\n",
603+
);
604+
})
605+
}
606+
590607
#[test]
591608
fn list_targets() {
592609
setup(&|config| {

tests/cli-self-upd.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ fn uninstall_works_if_some_bins_dont_exist() {
193193
.cargodir
194194
.join(&format!("bin/rust-lldb{}", EXE_SUFFIX));
195195
let rust_gdb = config.cargodir.join(&format!("bin/rust-gdb{}", EXE_SUFFIX));
196-
let rust_gdbgui = config.cargodir.join(&format!("bin/rust-gdbgui{}", EXE_SUFFIX));
196+
let rust_gdbgui = config
197+
.cargodir
198+
.join(&format!("bin/rust-gdbgui{}", EXE_SUFFIX));
197199

198200
fs::remove_file(&rustc).unwrap();
199201
fs::remove_file(&cargo).unwrap();

tests/mock/clitools.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,15 @@ pub fn expect_err_ex(config: &Config, args: &[&str], stdout: &str, stderr: &str)
384384
println!("expected.ok: false");
385385
print_indented("expected.stdout", stdout);
386386
print_indented("expected.stderr", stderr);
387-
panic!();
387+
if out.ok {
388+
panic!("expected command to fail");
389+
} else if out.stdout != stdout {
390+
panic!("expected stdout to match");
391+
} else if out.stderr != stderr {
392+
panic!("expected stderr to match");
393+
} else {
394+
unreachable!()
395+
}
388396
}
389397
}
390398

@@ -445,10 +453,16 @@ fn print_command(args: &[&str], out: &SanitizedOutput) {
445453
}
446454

447455
fn print_indented(heading: &str, text: &str) {
456+
let mut lines = text.lines().count();
457+
// The standard library treats `a\n` and `a` as both being one line.
458+
// This is confusing when the test fails because of a missing newline.
459+
if !text.is_empty() && !text.ends_with('\n') {
460+
lines -= 1;
461+
}
448462
println!(
449463
"{} ({} lines):\n {}",
450464
heading,
451-
text.lines().count(),
465+
lines,
452466
text.replace("\n", "\n ")
453467
);
454468
}

0 commit comments

Comments
 (0)