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

1
Cargo.lock generated
View file

@ -742,6 +742,7 @@ name = "testsuite"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cargo-minimize",
"fs_extra", "fs_extra",
"once_cell", "once_cell",
"rayon", "rayon",

View file

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

View file

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

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.70" anyhow = "1.0.70"
cargo-minimize = { path = ".." }
fs_extra = "1.3.0" fs_extra = "1.3.0"
once_cell = "1.17.1" once_cell = "1.17.1"
rayon = "1.7.0" rayon = "1.7.0"

10
testsuite/README.md Normal file
View file

@ -0,0 +1,10 @@
# testsuite
The test suite works the following way:
We have a bunch of files in `$WORKSPACE/full-tests`, every file is a test. We then run
`cargo-minimize` on that. `~MINIMIZE-ROOT` are required to be present in the minimization,
and we expect `~REQUIRE-DELETED` to be deleted by cargo-minimize.
We use `bin/regression_checked` as our custom script to verify whether it "reproduces", where
for us, "reproduces" means "all roots are present and the code compiles".

View file

@ -1,8 +1,10 @@
use anyhow::bail; use anyhow::bail;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let cargo = std::env::var("MINIMIZE_CARGO").expect("MINIMIZE_CARGO");
if std::env::var("MINIMIZE_LINTS").as_deref() == Ok("1") { if std::env::var("MINIMIZE_LINTS").as_deref() == Ok("1") {
std::process::Command::new("cargo") std::process::Command::new(&cargo)
.arg("check") .arg("check")
.spawn() .spawn()
.unwrap() .unwrap()
@ -18,7 +20,7 @@ fn main() -> anyhow::Result<()> {
testsuite::ensure_roots_kept(&proj_dir, roots)?; testsuite::ensure_roots_kept(&proj_dir, roots)?;
let check = std::process::Command::new("cargo") let check = std::process::Command::new(&cargo)
.arg("check") .arg("check")
.spawn() .spawn()
.unwrap() .unwrap()

View file

@ -33,7 +33,8 @@ pub fn ensure_roots_kept(
Ok(()) Ok(())
} }
fn run_build(command: &mut Command) -> Result<()> { fn run_build(cargo: &Path, command: &mut Command) -> Result<()> {
command.env("MINIMIZE_CARGO", cargo);
let exit = command let exit = command
.spawn() .spawn()
.context("failed to spawn command")? .context("failed to spawn command")?
@ -45,7 +46,11 @@ fn run_build(command: &mut Command) -> Result<()> {
} }
pub fn full_tests() -> Result<()> { pub fn full_tests() -> Result<()> {
run_build(Command::new("cargo").args([ let cargo = cargo_minimize::rustup_which("cargo")?;
run_build(
&cargo,
Command::new(&cargo).args([
"build", "build",
"-p", "-p",
"cargo-minimize", "cargo-minimize",
@ -55,7 +60,8 @@ pub fn full_tests() -> Result<()> {
"regression_checker", "regression_checker",
"--bin", "--bin",
"cargo-minimize", "cargo-minimize",
])) ]),
)
.context("running cargo build")?; .context("running cargo build")?;
let this_file = Path::new(file!()) let this_file = Path::new(file!())
@ -94,7 +100,7 @@ pub fn full_tests() -> Result<()> {
.map(|child| { .map(|child| {
let path = child.path(); let path = child.path();
build(&path, &regression_checker_path) build(&cargo, &path, &regression_checker_path)
.with_context(|| format!("building {:?}", path.file_name().unwrap())) .with_context(|| format!("building {:?}", path.file_name().unwrap()))
}) })
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
@ -102,7 +108,7 @@ pub fn full_tests() -> Result<()> {
for child in children { for child in children {
let path = child.path(); let path = child.path();
build(&path, &regression_checker_path) build(&cargo, &path, &regression_checker_path)
.with_context(|| format!("building {:?}", path.file_name().unwrap()))?; .with_context(|| format!("building {:?}", path.file_name().unwrap()))?;
} }
} }
@ -110,12 +116,12 @@ pub fn full_tests() -> Result<()> {
Ok(()) Ok(())
} }
fn setup_dir(path: &Path) -> Result<(TempDir, PathBuf)> { fn setup_dir(cargo: &Path, path: &Path) -> Result<(TempDir, PathBuf)> {
let tempdir = tempfile::tempdir()?; let tempdir = tempfile::tempdir()?;
let proj_name = path.file_name().unwrap().to_str().unwrap(); let proj_name = path.file_name().unwrap().to_str().unwrap();
let proj_name = if let Some(proj_name) = proj_name.strip_suffix(".rs") { let proj_name = if let Some(proj_name) = proj_name.strip_suffix(".rs") {
let out = Command::new("cargo") let out = Command::new(cargo)
.arg("new") .arg("new")
.arg(proj_name) .arg(proj_name)
.current_dir(tempdir.path()) .current_dir(tempdir.path())
@ -143,8 +149,8 @@ fn setup_dir(path: &Path) -> Result<(TempDir, PathBuf)> {
Ok((tempdir, proj_dir)) Ok((tempdir, proj_dir))
} }
fn build(path: &Path, regression_checker_path: &Path) -> Result<()> { fn build(cargo: &Path, path: &Path, regression_checker_path: &Path) -> Result<()> {
let (_tempdir, proj_dir) = setup_dir(path).context("setting up tempdir")?; let (_tempdir, proj_dir) = setup_dir(cargo, path).context("setting up tempdir")?;
let mut cargo_minimize_path = PathBuf::from("target/debug/cargo-minimize"); let mut cargo_minimize_path = PathBuf::from("target/debug/cargo-minimize");
if cfg!(windows) { if cfg!(windows) {
cargo_minimize_path.set_extension("exe"); cargo_minimize_path.set_extension("exe");
@ -168,6 +174,7 @@ fn build(path: &Path, regression_checker_path: &Path) -> Result<()> {
let minimize_roots = start_roots.join(","); let minimize_roots = start_roots.join(",");
cmd.env("MINIMIZE_RUNTEST_ROOTS", &minimize_roots); cmd.env("MINIMIZE_RUNTEST_ROOTS", &minimize_roots);
cmd.env("MINIMIZE_CARGO", cargo);
let out = cmd.output().context("spawning cargo-minimize")?; let out = cmd.output().context("spawning cargo-minimize")?;
let stderr = String::from_utf8(out.stderr).unwrap(); let stderr = String::from_utf8(out.stderr).unwrap();