Skip to content

Commit

Permalink
Merge pull request #2831 from jyn514/default-none
Browse files Browse the repository at this point in the history
Add "rustup default none" as a way to unset the default toolchain
  • Loading branch information
kinnison authored Sep 4, 2021
2 parents 511e6c8 + 73118b2 commit 38f4479
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
let status = if !toolchain.is_custom() {
let distributable = DistributableToolchain::new(&toolchain)?;
Some(distributable.install_from_dist_if_not_installed()?)
} else if !toolchain.exists() {
} else if !toolchain.exists() && toolchain.name() != "none" {
return Err(RustupError::ToolchainNotInstalled(toolchain.name().to_string()).into());
} else {
None
Expand Down
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ impl Cfg {

pub fn set_default(&self, toolchain: &str) -> Result<()> {
self.settings_file.with_mut(|s| {
s.default_toolchain = Some(toolchain.to_owned());
s.default_toolchain = if toolchain == "none" {
None
} else {
Some(toolchain.to_owned())
};
Ok(())
})?;
(self.notify_handler)(Notification::SetDefaultToolchain(toolchain));
Expand Down
1 change: 1 addition & 0 deletions src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl<'a> Display for Notification<'a> {
Install(n) => n.fmt(f),
Utils(n) => n.fmt(f),
Temp(n) => n.fmt(f),
SetDefaultToolchain("none") => write!(f, "default toolchain unset"),
SetDefaultToolchain(name) => write!(f, "default toolchain set to '{}'", name),
SetOverrideToolchain(path, name) => write!(
f,
Expand Down
21 changes: 19 additions & 2 deletions tests/cli-exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
pub mod mock;

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

#[test]
fn default_none() {
setup(&|config| {
expect_stderr_ok(
config,
&["rustup", "default", "none"],
"info: default toolchain unset",
);
expect_err_ex(
config,
&["rustc", "--version"],
"",
"error: no override and no default toolchain set\n",
);
})
}

#[test]
fn list_targets() {
setup(&|config| {
Expand Down
4 changes: 3 additions & 1 deletion tests/cli-self-upd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ fn uninstall_works_if_some_bins_dont_exist() {
.cargodir
.join(&format!("bin/rust-lldb{}", EXE_SUFFIX));
let rust_gdb = config.cargodir.join(&format!("bin/rust-gdb{}", EXE_SUFFIX));
let rust_gdbgui = config.cargodir.join(&format!("bin/rust-gdbgui{}", EXE_SUFFIX));
let rust_gdbgui = config
.cargodir
.join(&format!("bin/rust-gdbgui{}", EXE_SUFFIX));

fs::remove_file(&rustc).unwrap();
fs::remove_file(&cargo).unwrap();
Expand Down
18 changes: 16 additions & 2 deletions tests/mock/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,15 @@ pub fn expect_err_ex(config: &Config, args: &[&str], stdout: &str, stderr: &str)
println!("expected.ok: false");
print_indented("expected.stdout", stdout);
print_indented("expected.stderr", stderr);
panic!();
if out.ok {
panic!("expected command to fail");
} else if out.stdout != stdout {
panic!("expected stdout to match");
} else if out.stderr != stderr {
panic!("expected stderr to match");
} else {
unreachable!()
}
}
}

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

fn print_indented(heading: &str, text: &str) {
let mut lines = text.lines().count();
// The standard library treats `a\n` and `a` as both being one line.
// This is confusing when the test fails because of a missing newline.
if !text.is_empty() && !text.ends_with('\n') {
lines -= 1;
}
println!(
"{} ({} lines):\n {}",
heading,
text.lines().count(),
lines,
text.replace("\n", "\n ")
);
}
Expand Down

0 comments on commit 38f4479

Please sign in to comment.