Skip to content

Commit 05d3413

Browse files
committed
Correct the verbose option of the "toolchain list" command
1 parent 9c7f313 commit 05d3413

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

src/cli/common.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use lazy_static::lazy_static;
88
use rustup::utils::notify::NotificationLevel;
99
use rustup::utils::utils;
1010
use rustup::{Cfg, Notification, Toolchain, UpdateStatus};
11+
use std::fs;
1112
use std::io::{BufRead, BufReader, ErrorKind, Write};
1213
use std::path::Path;
1314
use std::process::{Command, Stdio};
@@ -424,14 +425,28 @@ pub fn list_installed_components(toolchain: &Toolchain<'_>) -> Result<()> {
424425
Ok(())
425426
}
426427

427-
pub fn list_toolchains(cfg: &Cfg, is_verbose: bool) -> Result<()> {
428-
let toolchains = cfg.list_toolchains()?;
429-
let toolchain_info = if is_verbose {
430-
format!("\t{}", &cfg.rustup_dir.display())
428+
fn print_toolchain_path(cfg: &Cfg, toolchain: &str, if_default: &str, verbose: bool) -> Result<()> {
429+
let toolchain_path = {
430+
let mut t_path = cfg.toolchains_dir.clone();
431+
t_path.push(&toolchain);
432+
t_path
433+
};
434+
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
435+
let toolchain_path = if verbose {
436+
if toolchain_meta.is_dir() {
437+
format!("\t{}", toolchain_path.display())
438+
} else {
439+
format!("\t{:?}", fs::read_link(toolchain_path)?)
440+
}
431441
} else {
432-
String::from("")
442+
String::new()
433443
};
444+
println!("{}{}{}", &toolchain, if_default, toolchain_path);
445+
Ok(())
446+
}
434447

448+
pub fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<()> {
449+
let toolchains = cfg.list_toolchains()?;
435450
if toolchains.is_empty() {
436451
println!("no installed toolchains");
437452
} else if let Ok(Some(def_toolchain)) = cfg.find_default() {
@@ -441,11 +456,13 @@ pub fn list_toolchains(cfg: &Cfg, is_verbose: bool) -> Result<()> {
441456
} else {
442457
""
443458
};
444-
println!("{}{}{}", &toolchain, if_default, toolchain_info);
459+
print_toolchain_path(cfg, &toolchain, if_default, verbose)
460+
.expect("Failed to list toolchains' directories");
445461
}
446462
} else {
447463
for toolchain in toolchains {
448-
println!("{}{}", &toolchain, toolchain_info);
464+
print_toolchain_path(cfg, &toolchain, "", verbose)
465+
.expect("Failed to list toolchains' directories");
449466
}
450467
}
451468
Ok(())

tests/cli-misc.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,3 +981,19 @@ fn override_by_toolchain_on_the_command_line() {
981981
expect_stdout_ok(config, &["rustup", "default"], "nightly-x86_64-");
982982
});
983983
}
984+
985+
#[test]
986+
fn toolchain_link_then_list_verbose() {
987+
setup(&|config| {
988+
let path_1 = config.customdir.join("custom-1");
989+
let path_1 = path_1.to_string_lossy();
990+
expect_ok(
991+
config,
992+
&["rustup", "toolchain", "link", "custom-1", &path_1],
993+
);
994+
#[cfg(windows)]
995+
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "\\custom-1");
996+
#[cfg(not(windows))]
997+
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "/custom-1");
998+
});
999+
}

tests/cli-v1.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,31 @@ fn list_toolchains() {
104104
&["rustup", "toolchain", "list", "-v"],
105105
"(default)\t",
106106
);
107+
#[cfg(windows)]
107108
expect_stdout_ok(
108109
config,
109-
&["rustup", "toolchain", "list", "--verbose"],
110-
"(default)\t",
110+
&["rustup", "toolchain", "list", "-v"],
111+
"\\toolchains\\nightly-x86",
112+
);
113+
#[cfg(not(windows))]
114+
expect_stdout_ok(
115+
config,
116+
&["rustup", "toolchain", "list", "-v"],
117+
"/toolchains/nightly-x86",
111118
);
112119
expect_stdout_ok(config, &["rustup", "toolchain", "list"], "beta-2015-01-01");
113-
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "\t");
114-
expect_stdout_ok(config, &["rustup", "toolchain", "list", "--verbose"], "\t");
120+
#[cfg(windows)]
121+
expect_stdout_ok(
122+
config,
123+
&["rustup", "toolchain", "list", "-v"],
124+
"\\toolchains\\beta-2015-01-01",
125+
);
126+
#[cfg(not(windows))]
127+
expect_stdout_ok(
128+
config,
129+
&["rustup", "toolchain", "list", "-v"],
130+
"/toolchains/beta-2015-01-01",
131+
);
115132
});
116133
}
117134

tests/cli-v2.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,31 @@ fn list_toolchains() {
148148
&["rustup", "toolchain", "list", "-v"],
149149
"(default)\t",
150150
);
151+
#[cfg(windows)]
151152
expect_stdout_ok(
152153
config,
153-
&["rustup", "toolchain", "list", "--verbose"],
154-
"(default)\t",
154+
&["rustup", "toolchain", "list", "-v"],
155+
"\\toolchains\\nightly-x86",
156+
);
157+
#[cfg(not(windows))]
158+
expect_stdout_ok(
159+
config,
160+
&["rustup", "toolchain", "list", "-v"],
161+
"/toolchains/nightly-x86",
155162
);
156163
expect_stdout_ok(config, &["rustup", "toolchain", "list"], "beta-2015-01-01");
157-
expect_stdout_ok(config, &["rustup", "toolchain", "list", "-v"], "\t");
158-
expect_stdout_ok(config, &["rustup", "toolchain", "list", "--verbose"], "\t");
164+
#[cfg(windows)]
165+
expect_stdout_ok(
166+
config,
167+
&["rustup", "toolchain", "list", "-v"],
168+
"\\toolchains\\beta-2015-01-01",
169+
);
170+
#[cfg(not(windows))]
171+
expect_stdout_ok(
172+
config,
173+
&["rustup", "toolchain", "list", "-v"],
174+
"/toolchains/beta-2015-01-01",
175+
);
159176
});
160177
}
161178

0 commit comments

Comments
 (0)