Skip to content

Commit 4471a6c

Browse files
committed
Merge pull request #156 from brson/show
Add `rustup show`
2 parents 50d8b21 + a5ef44e commit 4471a6c

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/multirust-cli/rustup_mode.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn main() -> Result<()> {
2424
("update", Some(m)) => try!(update(cfg, m)),
2525
("run", Some(m)) => try!(run(cfg, m)),
2626
("which", Some(m)) => try!(which(cfg, m)),
27+
("show", Some(_)) => try!(show(cfg)),
2728
("target", Some(c)) => {
2829
match c.subcommand() {
2930
("list", Some(_)) => try!(target_list(cfg)),
@@ -98,6 +99,8 @@ pub fn cli() -> App<'static, 'static> {
9899
.about("Display which binary will be run for a given command")
99100
.arg(Arg::with_name("command")
100101
.required(true)))
102+
.subcommand(SubCommand::with_name("show")
103+
.about("Show the active toolchain"))
101104
.subcommand(SubCommand::with_name("target")
102105
.about("Modify a toolchain's supported targets")
103106
.setting(AppSettings::SubcommandRequired)
@@ -113,7 +116,7 @@ pub fn cli() -> App<'static, 'static> {
113116
.required(true))
114117
.arg(Arg::with_name("toolchain"))))
115118
.subcommand(SubCommand::with_name("toolchain")
116-
.about("Modify the installed toolchains")
119+
.about("Modify or query the installed toolchains")
117120
.setting(AppSettings::SubcommandRequired)
118121
.subcommand(SubCommand::with_name("list")
119122
.about("List installed toolchains"))
@@ -236,6 +239,26 @@ fn which(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
236239
try!(utils::assert_is_file(&binary_path));
237240

238241
println!("{}", binary_path.display());
242+
243+
Ok(())
244+
}
245+
246+
fn show(cfg: &Cfg) -> Result<()> {
247+
let ref cwd = try!(utils::current_dir());
248+
let override_ = try!(cfg.find_override(cwd));
249+
if let Some((toolchain, reason)) = override_ {
250+
println!("{} ({})", toolchain.name(), reason);
251+
return Ok(());
252+
}
253+
254+
let toolchain = try!(cfg.find_default());
255+
if let Some(toolchain) = toolchain {
256+
println!("{} (default toolchain)", toolchain.name());
257+
return Ok(());
258+
}
259+
260+
println!("no active toolchain");
261+
239262
Ok(())
240263
}
241264

tests/cli-rustup.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate tempdir;
88
use multirust_mock::clitools::{self, Config, Scenario,
99
expect_ok, expect_ok_ex,
1010
expect_stdout_ok,
11+
expect_err_ex,
1112
set_current_dist_date};
1213

1314
pub fn setup(f: &Fn(&Config)) {
@@ -222,3 +223,79 @@ fn link() {
222223
"hash-c-1");
223224
});
224225
}
226+
227+
#[test]
228+
fn show_toolchain_none() {
229+
setup(&|config| {
230+
expect_ok_ex(config, &["rustup", "show"],
231+
r"no active toolchain
232+
",
233+
r"");
234+
});
235+
}
236+
237+
#[test]
238+
fn show_toolchain_default() {
239+
setup(&|config| {
240+
expect_ok(config, &["rustup", "default", "nightly"]);
241+
expect_ok_ex(config, &["rustup", "show"],
242+
r"nightly (default toolchain)
243+
",
244+
r"");
245+
});
246+
}
247+
248+
#[test]
249+
fn show_toolchain_override() {
250+
setup(&|config| {
251+
let cwd = ::std::env::current_dir().unwrap();
252+
expect_ok(config, &["rustup", "override", "add", "nightly"]);
253+
expect_ok_ex(config, &["rustup", "show"],
254+
&format!(r"nightly (directory override for '{}')
255+
", cwd.display()),
256+
r"");
257+
});
258+
}
259+
260+
#[test]
261+
fn show_toolchain_override_not_installed() {
262+
setup(&|config| {
263+
expect_ok(config, &["rustup", "override", "add", "nightly"]);
264+
expect_ok(config, &["rustup", "toolchain", "remove", "nightly"]);
265+
// I'm not sure this should really be erroring when the toolchain
266+
// is not installed; just capturing the behavior.
267+
expect_err_ex(config, &["rustup", "show"],
268+
r"",
269+
r"error: toolchain 'nightly' is not installed
270+
");
271+
});
272+
}
273+
274+
#[test]
275+
fn show_toolchain_env() {
276+
setup(&|config| {
277+
expect_ok(config, &["rustup", "default", "nightly"]);
278+
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
279+
clitools::env(config, &mut cmd);
280+
cmd.env("MULTIRUST_TOOLCHAIN", "nightly");
281+
let out = cmd.output().unwrap();
282+
assert!(out.status.success());
283+
let stdout = String::from_utf8(out.stdout).unwrap();
284+
assert!(stdout == "nightly (environment override by MULTIRUST_TOOLCHAIN)\n");
285+
});
286+
}
287+
288+
#[test]
289+
fn show_toolchain_env_not_installed() {
290+
setup(&|config| {
291+
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
292+
clitools::env(config, &mut cmd);
293+
cmd.env("MULTIRUST_TOOLCHAIN", "nightly");
294+
let out = cmd.output().unwrap();
295+
// I'm not sure this should really be erroring when the toolchain
296+
// is not installed; just capturing the behavior.
297+
assert!(!out.status.success());
298+
let stderr = String::from_utf8(out.stderr).unwrap();
299+
assert!(stderr == "error: toolchain 'nightly' is not installed\n");
300+
});
301+
}

0 commit comments

Comments
 (0)