Skip to content

Commit

Permalink
Fix 'show' so it displays helpful information if the active toolchain…
Browse files Browse the repository at this point in the history
… is not installed.

Fixes rust-lang#1178
  • Loading branch information
michaelfletchercgy committed Jul 1, 2017
1 parent 92d0d1e commit 039299e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
58 changes: 37 additions & 21 deletions src/rustup-cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use self_update;
use std::path::Path;
use std::process::Command;
use std::iter;
use std::error::Error;
use term2;
use std::io::{self, Write};
use help::*;
Expand Down Expand Up @@ -545,15 +546,21 @@ fn show(cfg: &Cfg) -> Result<()> {

let ref cwd = try!(utils::current_dir());
let installed_toolchains = try!(cfg.list_toolchains());
let active_toolchain = try!(cfg.find_override_toolchain_or_default(cwd));
let active_targets = if let Some((ref t, _)) = active_toolchain {
match t.list_components() {
Ok(cs_vec) => cs_vec
.into_iter()
.filter(|c| c.component.pkg == "rust-std")
.filter(|c| c.installed)
.collect(),
Err(_) => vec![]
let active_toolchain = cfg.find_override_toolchain_or_default(cwd);

// active_toolchain will carry the reason we don't have one in its detail.
let active_targets = if let Ok(ref at) = active_toolchain {
if let Some((ref t, _)) = *at {
match t.list_components() {
Ok(cs_vec) => cs_vec
.into_iter()
.filter(|c| c.component.pkg == "rust-std")
.filter(|c| c.installed)
.collect(),
Err(_) => vec![]
}
} else {
vec![]
}
} else {
vec![]
Expand All @@ -572,9 +579,7 @@ fn show(cfg: &Cfg) -> Result<()> {

if show_installed_toolchains {
if show_headers { print_header("installed toolchains") }
let default = try!(cfg.find_default());
let default_name = default.map(|t| t.name().to_string())
.unwrap_or("".into());
let default_name = try!(cfg.get_default());
for t in installed_toolchains {
if default_name == t {
println!("{} (default)", t);
Expand All @@ -599,16 +604,27 @@ fn show(cfg: &Cfg) -> Result<()> {
if show_headers { print_header("active toolchain") }

match active_toolchain {
Some((ref toolchain, Some(ref reason))) => {
println!("{} ({})", toolchain.name(), reason);
println!("{}", common::rustc_version(toolchain));
}
Some((ref toolchain, None)) => {
println!("{} (default)", toolchain.name());
println!("{}", common::rustc_version(toolchain));
Ok(atc) => {
match atc {
Some((ref toolchain, Some(ref reason))) => {
println!("{} ({})", toolchain.name(), reason);
println!("{}", common::rustc_version(toolchain));
}
Some((ref toolchain, None)) => {
println!("{} (default)", toolchain.name());
println!("{}", common::rustc_version(toolchain));
}
None => {
println!("no active toolchain");
}
}
}
None => {
println!("no active toolchain");
Err(err) => {
if let Some(cause) = err.cause() {
println!("(error: {}, {})", err, cause);
} else {
println!("(error: {})", err);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/rustup/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ impl Cfg {
})
}

pub fn get_default(&self) -> Result<String> {
self.settings_file.with(|s| {
Ok(s.default_toolchain.clone().unwrap())
})
}



pub fn list_toolchains(&self) -> Result<Vec<String>> {
if utils::is_directory(&self.toolchains_dir) {
let mut toolchains: Vec<_> = try!(utils::read_dir("toolchains", &self.toolchains_dir))
Expand Down
17 changes: 9 additions & 8 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate rustup_dist;
extern crate rustup_utils;
extern crate rustup_mock;
extern crate tempdir;
extern crate regex;

use std::fs;
use std::env::consts::EXE_SUFFIX;
Expand All @@ -15,6 +16,7 @@ use rustup_mock::clitools::{self, Config, Scenario,
expect_err,
set_current_dist_date,
this_host_triple, change_dir};
use regex::Regex;

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

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

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

Expand Down

0 comments on commit 039299e

Please sign in to comment.