Skip to content

Commit

Permalink
Merge pull request #2084 from BeniCheni/correct-toolchain-list-verbose
Browse files Browse the repository at this point in the history
Correct "toolchain list" command verbose option
  • Loading branch information
kinnison authored Oct 30, 2019
2 parents 9c7f313 + 571fffb commit e44868f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
31 changes: 24 additions & 7 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use lazy_static::lazy_static;
use rustup::utils::notify::NotificationLevel;
use rustup::utils::utils;
use rustup::{Cfg, Notification, Toolchain, UpdateStatus};
use std::fs;
use std::io::{BufRead, BufReader, ErrorKind, Write};
use std::path::Path;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -424,14 +425,28 @@ pub fn list_installed_components(toolchain: &Toolchain<'_>) -> Result<()> {
Ok(())
}

pub fn list_toolchains(cfg: &Cfg, is_verbose: bool) -> Result<()> {
let toolchains = cfg.list_toolchains()?;
let toolchain_info = if is_verbose {
format!("\t{}", &cfg.rustup_dir.display())
fn print_toolchain_path(cfg: &Cfg, toolchain: &str, if_default: &str, verbose: bool) -> Result<()> {
let toolchain_path = {
let mut t_path = cfg.toolchains_dir.clone();
t_path.push(&toolchain);
t_path
};
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
let toolchain_path = if verbose {
if toolchain_meta.is_dir() {
format!("\t{}", toolchain_path.display())
} else {
format!("\t{}", fs::read_link(toolchain_path)?.display())
}
} else {
String::from("")
String::new()
};
println!("{}{}{}", &toolchain, if_default, toolchain_path);
Ok(())
}

pub fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<()> {
let toolchains = cfg.list_toolchains()?;
if toolchains.is_empty() {
println!("no installed toolchains");
} else if let Ok(Some(def_toolchain)) = cfg.find_default() {
Expand All @@ -441,11 +456,13 @@ pub fn list_toolchains(cfg: &Cfg, is_verbose: bool) -> Result<()> {
} else {
""
};
println!("{}{}{}", &toolchain, if_default, toolchain_info);
print_toolchain_path(cfg, &toolchain, if_default, verbose)
.expect("Failed to list toolchains' directories");
}
} else {
for toolchain in toolchains {
println!("{}{}", &toolchain, toolchain_info);
print_toolchain_path(cfg, &toolchain, "", verbose)
.expect("Failed to list toolchains' directories");
}
}
Ok(())
Expand Down
16 changes: 16 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,3 +981,19 @@ fn override_by_toolchain_on_the_command_line() {
expect_stdout_ok(config, &["rustup", "default"], "nightly-x86_64-");
});
}

#[test]
fn toolchain_link_then_list_verbose() {
setup(&|config| {
let path_1 = config.customdir.join("custom-1");
let path_1 = path_1.to_string_lossy();
expect_ok(
config,
&["rustup", "toolchain", "link", "custom-1", &path_1],
);
#[cfg(windows)]
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "\\custom-1");
#[cfg(not(windows))]
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "/custom-1");
});
}
25 changes: 21 additions & 4 deletions tests/cli-v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,31 @@ fn list_toolchains() {
&["rustup", "toolchain", "list", "-v"],
"(default)\t",
);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "--verbose"],
"(default)\t",
&["rustup", "toolchain", "list", "-v"],
"\\toolchains\\nightly-x86",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "-v"],
"/toolchains/nightly-x86",
);
expect_stdout_ok(config, &["rustup", "toolchain", "list"], "beta-2015-01-01");
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "\t");
expect_stdout_ok(config, &["rustup", "toolchain", "list", "--verbose"], "\t");
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "-v"],
"\\toolchains\\beta-2015-01-01",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "-v"],
"/toolchains/beta-2015-01-01",
);
});
}

Expand Down
25 changes: 21 additions & 4 deletions tests/cli-v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,31 @@ fn list_toolchains() {
&["rustup", "toolchain", "list", "-v"],
"(default)\t",
);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "--verbose"],
"(default)\t",
&["rustup", "toolchain", "list", "-v"],
"\\toolchains\\nightly-x86",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "-v"],
"/toolchains/nightly-x86",
);
expect_stdout_ok(config, &["rustup", "toolchain", "list"], "beta-2015-01-01");
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "\t");
expect_stdout_ok(config, &["rustup", "toolchain", "list", "--verbose"], "\t");
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "-v"],
"\\toolchains\\beta-2015-01-01",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "toolchain", "list", "-v"],
"/toolchains/beta-2015-01-01",
);
});
}

Expand Down

0 comments on commit e44868f

Please sign in to comment.