Skip to content

Commit

Permalink
Add rustup run --install, fix #1293
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Nov 21, 2017
1 parent ab75525 commit 9d095e2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/rustup-cli/proxy_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn main() -> Result<()> {
fn direct_proxy(cfg: &Cfg, arg0: &str, toolchain: Option<&str>, args: &[OsString]) -> Result<()> {
let cmd = match toolchain {
None => try!(cfg.create_command_for_dir(&try!(utils::current_dir()), arg0)),
Some(tc) => try!(cfg.create_command_for_toolchain(tc, arg0)),
Some(tc) => try!(cfg.create_command_for_toolchain(tc, false, arg0)),
};
Ok(try!(run_command_for_dir(cmd, arg0, args, &cfg)))
}
5 changes: 4 additions & 1 deletion src/rustup-cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ pub fn cli() -> App<'static, 'static> {
.about("Run a command with an environment configured for a given toolchain")
.after_help(RUN_HELP)
.setting(AppSettings::TrailingVarArg)
.arg(Arg::with_name("install")
.help("Install the requested toolchain if needed")
.long("install"))
.arg(Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true))
Expand Down Expand Up @@ -482,7 +485,7 @@ fn run(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let args = m.values_of("command").unwrap();
let args: Vec<_> = args.collect();
let cmd = try!(cfg.create_command_for_toolchain(toolchain, args[0]));
let cmd = try!(cfg.create_command_for_toolchain(toolchain, m.is_present("install"), args[0]));

Ok(try!(command::run_command_for_dir(cmd, args[0], &args[1..], &cfg)))
}
Expand Down
6 changes: 5 additions & 1 deletion src/rustup/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,12 @@ impl Cfg {
}
}

pub fn create_command_for_toolchain(&self, toolchain: &str, binary: &str) -> Result<Command> {
pub fn create_command_for_toolchain(&self, toolchain: &str, install_if_missing: bool,
binary: &str) -> Result<Command> {
let ref toolchain = try!(self.get_toolchain(toolchain, false));
if install_if_missing && !toolchain.exists() {
try!(toolchain.install_from_dist());
}

if let Some(cmd) = try!(self.maybe_do_cargo_fallback(toolchain, binary)) {
Ok(cmd)
Expand Down
18 changes: 18 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,24 @@ fn rustup_failed_path_search() {
});
}

#[test]
fn rustup_run_not_installed() {
setup(&|config| {
expect_ok(config, &["rustup", "install", "stable"]);
expect_err(config, &["rustup", "run", "nightly", "rustc", "--version"],
for_host!("toolchain 'nightly-{0}' is not installed"));
});
}

#[test]
fn rustup_run_install() {
setup(&|config| {
expect_ok(config, &["rustup", "install", "stable"]);
expect_stderr_ok(config, &["rustup", "run", "--install", "nightly", "cargo", "--version"],
"info: installing component 'rustc'");
});
}

#[test]
fn multirust_env_compat() {
setup(&|config| {
Expand Down

0 comments on commit 9d095e2

Please sign in to comment.