speed up tests by doing rustup which

`cargo test` goes from 2.4s to 1.6s
This commit is contained in:
nora 2023-09-23 16:03:26 +02:00
parent ea5fb6e4ba
commit 50dc094ddd
7 changed files with 99 additions and 34 deletions

View file

@ -1,4 +1,4 @@
use anyhow::{bail, Context, Result};
use anyhow::{bail, ensure, Context, Result};
use rustfix::diagnostics::Diagnostic;
use serde::Deserialize;
use std::{
@ -48,11 +48,12 @@ struct BuildInner {
#[derive(Debug)]
enum BuildMode {
Cargo {
cargo_path: PathBuf,
/// May be something like `miri run`.
subcommand: Vec<String>,
},
Script(PathBuf),
Rustc,
Rustc(PathBuf),
}
impl Build {
@ -68,16 +69,22 @@ impl Build {
.unwrap_or_default();
let mode = if options.rustc {
BuildMode::Rustc
let rustc = rustup_which("rustc")?;
BuildMode::Rustc(rustc)
} else if let Some(script) = &options.script_path {
BuildMode::Script(script.clone())
} else {
let subcommand = split_args(&options.cargo_subcmd);
BuildMode::Cargo { subcommand }
let cargo_path = rustup_which("cargo")?;
BuildMode::Cargo {
cargo_path,
subcommand,
}
};
let lint_mode = if options.rustc {
BuildMode::Rustc
let rustc = rustup_which("rustc")?;
BuildMode::Rustc(rustc)
} else if let Some(script) = options
.script_path_lints
.as_ref()
@ -90,7 +97,12 @@ impl Build {
.as_deref()
.map(split_args)
.unwrap_or_else(|| split_args(&options.cargo_subcmd));
BuildMode::Cargo { subcommand }
let cargo_path = rustup_which("cargo")?;
BuildMode::Cargo {
cargo_path,
subcommand,
}
};
let verify = if options.no_verify {
@ -136,8 +148,11 @@ impl Build {
}
let (is_ice, cmd_status, output) = match &inner.mode {
BuildMode::Cargo { subcommand } => {
let mut cmd = self.cmd("cargo");
BuildMode::Cargo {
cargo_path,
subcommand,
} => {
let mut cmd = self.cmd(cargo_path);
cmd.args(subcommand);
@ -145,6 +160,8 @@ impl Build {
cmd.arg("--color=always");
}
extra_cargoflags(&mut cmd);
cmd.args(&inner.extra_args);
for env in &inner.env {
@ -162,8 +179,8 @@ impl Build {
output,
)
}
BuildMode::Rustc => {
let mut cmd = self.cmd("rustc");
BuildMode::Rustc(rustc) => {
let mut cmd = self.cmd(rustc);
cmd.args(["--edition", "2021"]);
cmd.arg(&inner.input_path);
@ -244,13 +261,18 @@ impl Build {
}
let diags = match &inner.lint_mode {
BuildMode::Cargo { subcommand } => {
let mut cmd = self.cmd("cargo");
BuildMode::Cargo {
cargo_path,
subcommand,
} => {
let mut cmd = self.cmd(cargo_path);
cmd.args(subcommand);
cmd.arg("--message-format=json");
extra_cargoflags(&mut cmd);
cmd.args(&inner.extra_args);
for env in &inner.env {
@ -262,8 +284,8 @@ impl Build {
grab_cargo_diags(&output)?
}
BuildMode::Rustc => {
let mut cmd = self.cmd("rustc");
BuildMode::Rustc(rustc) => {
let mut cmd = self.cmd(rustc);
cmd.args(["--edition", "2021", "--error-format=json"]);
cmd.arg(&inner.input_path);
@ -317,6 +339,26 @@ impl Build {
}
}
fn extra_cargoflags(cargo: &mut Command) {
cargo.arg("--offline");
}
pub fn rustup_which(tool: &str) -> Result<PathBuf> {
let output = Command::new("rustup")
.arg("which")
.arg(tool)
.output()
.context("running rustup which")?;
ensure!(output.status.success(), "rustup which failed");
Ok(String::from_utf8(output.stdout)
.context("rustup which returned invalid utf8")?
.trim()
.to_owned()
.into())
}
#[derive(Debug)]
pub struct BuildResult {
reproduces_issue: bool,

View file

@ -13,6 +13,8 @@ mod formatting;
mod passes;
mod processor;
pub use build::rustup_which;
#[cfg(this_pulls_in_cargo_which_is_a_big_dep_i_dont_like_it)]
mod expand;