Skip to content

Commit 001c71e

Browse files
committed
Auto merge of #1189 - michaelfletchercgy:show-when-active-not-installed, r=alexcrichton
Fix 'show' so it displays helpful information if the active toolchain is not installed. Fixed 'show' so that it displays helpful information if the active toolchain is not installed. ``` $ export RUSTUP_TOOLCHAIN=beta $ rustup show Default host: x86_64-unknown-linux-gnu installed toolchains -------------------- stable-x86_64-unknown-linux-gnu nightly-x86_64-unknown-linux-gnu active toolchain ---------------- (error: override toolchain 'beta' is not installed, the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain) ``` Fixes #1178
2 parents 5a98092 + bc99cc0 commit 001c71e

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

src/rustup-cli/rustup_mode.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use self_update;
1010
use std::path::Path;
1111
use std::process::Command;
1212
use std::iter;
13+
use std::error::Error;
1314
use term2;
1415
use std::io::{self, Write};
1516
use help::*;
@@ -496,15 +497,21 @@ fn show(cfg: &Cfg) -> Result<()> {
496497

497498
let ref cwd = try!(utils::current_dir());
498499
let installed_toolchains = try!(cfg.list_toolchains());
499-
let active_toolchain = try!(cfg.find_override_toolchain_or_default(cwd));
500-
let active_targets = if let Some((ref t, _)) = active_toolchain {
501-
match t.list_components() {
502-
Ok(cs_vec) => cs_vec
503-
.into_iter()
504-
.filter(|c| c.component.pkg == "rust-std")
505-
.filter(|c| c.installed)
506-
.collect(),
507-
Err(_) => vec![]
500+
let active_toolchain = cfg.find_override_toolchain_or_default(cwd);
501+
502+
// active_toolchain will carry the reason we don't have one in its detail.
503+
let active_targets = if let Ok(ref at) = active_toolchain {
504+
if let Some((ref t, _)) = *at {
505+
match t.list_components() {
506+
Ok(cs_vec) => cs_vec
507+
.into_iter()
508+
.filter(|c| c.component.pkg == "rust-std")
509+
.filter(|c| c.installed)
510+
.collect(),
511+
Err(_) => vec![]
512+
}
513+
} else {
514+
vec![]
508515
}
509516
} else {
510517
vec![]
@@ -523,9 +530,7 @@ fn show(cfg: &Cfg) -> Result<()> {
523530

524531
if show_installed_toolchains {
525532
if show_headers { print_header("installed toolchains") }
526-
let default = try!(cfg.find_default());
527-
let default_name = default.map(|t| t.name().to_string())
528-
.unwrap_or("".into());
533+
let default_name = try!(cfg.get_default());
529534
for t in installed_toolchains {
530535
if default_name == t {
531536
println!("{} (default)", t);
@@ -550,16 +555,27 @@ fn show(cfg: &Cfg) -> Result<()> {
550555
if show_headers { print_header("active toolchain") }
551556

552557
match active_toolchain {
553-
Some((ref toolchain, Some(ref reason))) => {
554-
println!("{} ({})", toolchain.name(), reason);
555-
println!("{}", common::rustc_version(toolchain));
556-
}
557-
Some((ref toolchain, None)) => {
558-
println!("{} (default)", toolchain.name());
559-
println!("{}", common::rustc_version(toolchain));
558+
Ok(atc) => {
559+
match atc {
560+
Some((ref toolchain, Some(ref reason))) => {
561+
println!("{} ({})", toolchain.name(), reason);
562+
println!("{}", common::rustc_version(toolchain));
563+
}
564+
Some((ref toolchain, None)) => {
565+
println!("{} (default)", toolchain.name());
566+
println!("{}", common::rustc_version(toolchain));
567+
}
568+
None => {
569+
println!("no active toolchain");
570+
}
571+
}
560572
}
561-
None => {
562-
println!("no active toolchain");
573+
Err(err) => {
574+
if let Some(cause) = err.cause() {
575+
println!("(error: {}, {})", err, cause);
576+
} else {
577+
println!("(error: {})", err);
578+
}
563579
}
564580
}
565581

src/rustup/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ impl Cfg {
326326
})
327327
}
328328

329+
pub fn get_default(&self) -> Result<String> {
330+
self.settings_file.with(|s| {
331+
Ok(s.default_toolchain.clone().unwrap())
332+
})
333+
}
334+
335+
336+
329337
pub fn list_toolchains(&self) -> Result<Vec<String>> {
330338
if utils::is_directory(&self.toolchains_dir) {
331339
let mut toolchains: Vec<_> = try!(utils::read_dir("toolchains", &self.toolchains_dir))

tests/cli-rustup.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate rustup_dist;
44
extern crate rustup_utils;
55
extern crate rustup_mock;
66
extern crate tempdir;
7+
extern crate regex;
78

89
use std::fs;
910
use std::env::consts::EXE_SUFFIX;
@@ -15,6 +16,7 @@ use rustup_mock::clitools::{self, Config, Scenario,
1516
expect_err,
1617
set_current_dist_date,
1718
this_host_triple};
19+
use regex::Regex;
1820

1921
macro_rules! for_host { ($s: expr) => (&format!($s, this_host_triple())) }
2022

@@ -624,11 +626,10 @@ fn show_toolchain_override_not_installed() {
624626
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
625627
clitools::env(config, &mut cmd);
626628
let out = cmd.output().unwrap();
627-
assert!(!out.status.success());
628-
let stderr = String::from_utf8(out.stderr).unwrap();
629-
assert!(stderr.starts_with(
630-
for_host!("error: override toolchain 'nightly-{0}' is not installed")));
631-
assert!(!stderr.contains("info: caused by: not a directory: "));
629+
assert!(out.status.success());
630+
let stdout = String::from_utf8(out.stdout).unwrap();
631+
assert!(!stdout.contains("not a directory"));
632+
assert!(Regex::new(r"error: override toolchain 'nightly.*' is not installed, the directory override for '.*' specifies an uninstalled toolchain").unwrap().is_match(&stdout))
632633
});
633634
}
634635

@@ -659,9 +660,9 @@ fn show_toolchain_env_not_installed() {
659660
let out = cmd.output().unwrap();
660661
// I'm not sure this should really be erroring when the toolchain
661662
// is not installed; just capturing the behavior.
662-
assert!(!out.status.success());
663-
let stderr = String::from_utf8(out.stderr).unwrap();
664-
assert!(stderr.starts_with("error: override toolchain 'nightly' is not installed\n"));
663+
assert!(out.status.success());
664+
let stdout = String::from_utf8(out.stdout).unwrap();
665+
assert!(stdout.contains("override toolchain 'nightly' is not installed, the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain"));
665666
});
666667
}
667668

0 commit comments

Comments
 (0)