Skip to content

Commit

Permalink
Support release channel & --toolchain in front of rustup which
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniCheni committed Oct 3, 2019
1 parent 2ce2428 commit 0aacf5f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
32 changes: 28 additions & 4 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub fn main() -> Result<()> {
let matches = cli().get_matches();
let verbose = matches.is_present("verbose");
let quiet = matches.is_present("quiet");
let toolchain = if let Some(channel) = matches.value_of("release-channel") {
&channel[1..]
} else {
matches.value_of("toolchain").unwrap_or("")
};

let cfg = &common::set_globals(verbose, quiet)?;

if maybe_upgrade_data(cfg, &matches)? {
Expand Down Expand Up @@ -79,7 +85,7 @@ pub fn main() -> Result<()> {
(_, _) => unreachable!(),
},
("run", Some(m)) => run(cfg, m)?,
("which", Some(m)) => which(cfg, m)?,
("which", Some(m)) => which(cfg, m, toolchain)?,
("doc", Some(m)) => doc(cfg, m)?,
("man", Some(m)) => man(cfg, m)?,
("self", Some(c)) => match c.subcommand() {
Expand Down Expand Up @@ -135,6 +141,12 @@ pub fn cli() -> App<'static, 'static> {
.long("toolchain")
.takes_value(true),
)
.arg(
Arg::with_name("release-channel")
.help("provide the release channel (e.g. +stable) for toolchain")
.index(1)
.possible_values(&["+stable", "+nightly", "+beta"]),
)
.subcommand(
SubCommand::with_name("dump-testament")
.about("Dump information about the build")
Expand Down Expand Up @@ -817,10 +829,22 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
process::exit(c)
}

fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
fn which(cfg: &Cfg, m: &ArgMatches<'_>, toolchain_by_root_cmd: &str) -> Result<()> {
if !toolchain_by_root_cmd.is_empty()
&& m.is_present("toolchain")
&& !m.value_of("toolchain").expect("").is_empty()
{
warn!(
"Both the <release-channel> arg (e.g. \"+nightly\") and the \"--toolchain\" option are provided.\n\
The <release-channel> arg would take precedence over the \"--toolchain\" option.",
);
}

let binary = m.value_of("command").expect("");
let toolchain_provided = m.is_present("toolchain");
let binary_path = if toolchain_provided {
let binary_path = if !toolchain_by_root_cmd.is_empty() {
cfg.which_binary_by_toolchain(toolchain_by_root_cmd, binary)?
.expect("binary not found")
} else if m.is_present("toolchain") {
let toolchain = m.value_of("toolchain").expect("");
cfg.which_binary_by_toolchain(toolchain, binary)?
.expect("binary not found")
Expand Down
57 changes: 53 additions & 4 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ fn add_remove_component() {
#[test]
fn which() {
setup(&|config| {
let path_1 = config.customdir.join("custom-1");
let path_1 = path_1.to_string_lossy();
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],
Expand All @@ -885,8 +885,8 @@ fn which() {
&["rustup", "which", "rustc"],
"/toolchains/custom-1/bin/rustc",
);
let path_2 = config.customdir.join("custom-2");
let path_2 = path_2.to_string_lossy();
let _path_2 = config.customdir.join("custom-2");
let path_2 = _path_2.to_string_lossy();
expect_ok(
config,
&["rustup", "toolchain", "link", "custom-2", &path_2],
Expand All @@ -903,5 +903,54 @@ fn which() {
&["rustup", "which", "--toolchain=custom-2", "rustc"],
"/toolchains/custom-2/bin/rustc",
);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "--toolchain=custom-2", "which", "rustc"],
"\\toolchains\\custom-2\\bin\\rustc",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "--toolchain=custom-2", "which", "rustc"],
"/toolchains/custom-2/bin/rustc",
);
expect_ok(config, &["rustup", "default", "stable"]);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"\\toolchains\\stable-x86_64-apple-darwin\\bin\\rustc",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "+stable", "which", "rustc"],
"/toolchains/stable-x86_64-apple-darwin/bin/rustc",
);
expect_ok(config, &["rustup", "default", "nightly"]);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "+nightly", "which", "rustc"],
"\\toolchains\\nightly-x86_64-apple-darwin\\bin\\rustc",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "+nightly", "which", "rustc"],
"/toolchains/nightly-x86_64-apple-darwin/bin/rustc",
);
expect_err(
config,
&["rustup", "+foo", "which", "rustc"],
"error: '+foo' isn't a valid value for '<release-channel>",
);
expect_stderr_ok(
config,
&["rustup", "+nightly", "which", "cargo", "--toolchain=stable"],
"warning: Both the <release-channel> arg (e.g. \"+nightly\") and the \"--toolchain\" option are provided.\n\
The <release-channel> arg would take precedence over the \"--toolchain\" option.",
);
});
}

0 comments on commit 0aacf5f

Please sign in to comment.