From 47d35a1b391bf2f4c1e73b190ecacdca53de7bc6 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Wed, 3 Apr 2024 18:13:51 -0400 Subject: [PATCH] fix(cargo-codspeed): fix build when package is included in its deps Fix #37 --- crates/cargo-codspeed/src/build.rs | 23 ++++++++++++------- crates/cargo-codspeed/tests/helpers.rs | 8 +++++++ .../tests/package_in_deps.in/.gitignore | 2 ++ .../tests/package_in_deps.in/Cargo.toml | 17 ++++++++++++++ .../benches/bencher_example.rs | 16 +++++++++++++ .../cargo-codspeed/tests/package_in_deps.rs | 17 ++++++++++++++ 6 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 crates/cargo-codspeed/tests/package_in_deps.in/.gitignore create mode 100644 crates/cargo-codspeed/tests/package_in_deps.in/Cargo.toml create mode 100644 crates/cargo-codspeed/tests/package_in_deps.in/benches/bencher_example.rs create mode 100644 crates/cargo-codspeed/tests/package_in_deps.rs diff --git a/crates/cargo-codspeed/src/build.rs b/crates/cargo-codspeed/src/build.rs index 9f57cd4..654dbbd 100644 --- a/crates/cargo-codspeed/src/build.rs +++ b/crates/cargo-codspeed/src/build.rs @@ -18,9 +18,14 @@ fn get_compile_options( features: &Option>, package: &Package, benches: Vec<&str>, + is_root_package: bool, ) -> Result { let mut compile_opts = CompileOptions::new(config, CompileMode::Build)?; - compile_opts.spec = Packages::Packages(vec![package.name().to_string()]); + + // if the package is not the root package, we need to specify the package to build + if !is_root_package { + compile_opts.spec = Packages::Packages(vec![package.name().to_string()]); + } if let Some(features) = features { compile_opts.cli_features.features = Rc::new( features @@ -51,14 +56,15 @@ pub fn build_benches( package_name: Option, features: Option>, ) -> Result<()> { + let is_root_package = package_name.is_none(); let package = match package_name.as_ref() { - Some(package_name) => ws - .members() - .find(|p| p.name().to_string() == *package_name) - .ok_or_else(|| anyhow!("Package {} not found", package_name))?, - None => ws.current().map_err(|_| anyhow!("No package found. If working with a workspace please use the -p option to specify a member."))?, - }; + Some(package_name) => ws.members() + .find(|p| p.name().to_string() == *package_name) + .ok_or_else(|| anyhow!("Package {} not found", package_name))? + , + None => ws.current().map_err(|_| anyhow!("No package found. If working with a workspace please use the -p option to specify a member."))? + }; let all_benches = package .targets() .iter() @@ -99,7 +105,8 @@ pub fn build_benches( ws.config() .shell() .status_with_color("Building", benches_names_str.clone(), Color::Yellow)?; - let compile_opts = get_compile_options(config, &features, package, benches_names)?; + let compile_opts = + get_compile_options(config, &features, package, benches_names, is_root_package)?; let result = cargo::ops::compile(ws, &compile_opts)?; let built_benches = result .tests diff --git a/crates/cargo-codspeed/tests/helpers.rs b/crates/cargo-codspeed/tests/helpers.rs index b68267b..9e0e849 100644 --- a/crates/cargo-codspeed/tests/helpers.rs +++ b/crates/cargo-codspeed/tests/helpers.rs @@ -20,6 +20,7 @@ pub enum Project { Simple, Features, Workspace, + PackageInDeps, } pub fn setup(dir: &str, project: Project) -> String { @@ -55,6 +56,13 @@ pub fn setup(dir: &str, project: Project) -> String { workspace_root.join("crates").to_str().unwrap(), ); } + Project::PackageInDeps => { + replace_in_file( + tmp_dir.join("Cargo.toml").to_str().unwrap(), + "../../..", + workspace_root.join("crates").to_str().unwrap(), + ); + } } tmp_dir.to_str().unwrap().to_string() } diff --git a/crates/cargo-codspeed/tests/package_in_deps.in/.gitignore b/crates/cargo-codspeed/tests/package_in_deps.in/.gitignore new file mode 100644 index 0000000..1e7caa9 --- /dev/null +++ b/crates/cargo-codspeed/tests/package_in_deps.in/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ diff --git a/crates/cargo-codspeed/tests/package_in_deps.in/Cargo.toml b/crates/cargo-codspeed/tests/package_in_deps.in/Cargo.toml new file mode 100644 index 0000000..1187671 --- /dev/null +++ b/crates/cargo-codspeed/tests/package_in_deps.in/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "blit" +version = "0.8.5" +edition = "2021" +publish = false + +[dependencies] +bencher = "0.1.5" +codspeed = { path = "../../../codspeed" } +codspeed-bencher-compat = { path = "../../../bencher_compat" } + +[dev-dependencies] +blit = "*" + +[[bench]] +name = "bencher_example" +harness = false diff --git a/crates/cargo-codspeed/tests/package_in_deps.in/benches/bencher_example.rs b/crates/cargo-codspeed/tests/package_in_deps.in/benches/bencher_example.rs new file mode 100644 index 0000000..8613da5 --- /dev/null +++ b/crates/cargo-codspeed/tests/package_in_deps.in/benches/bencher_example.rs @@ -0,0 +1,16 @@ +use codspeed::codspeed::black_box; +use codspeed_bencher_compat::{benchmark_group, benchmark_main, Bencher}; + +pub fn a(bench: &mut Bencher) { + bench.iter(|| (0..100).fold(0, |x, y| black_box(x + y))) +} + +pub fn b(bench: &mut Bencher) { + const N: usize = 1024; + bench.iter(|| vec![0u8; N]); + + bench.bytes = N as u64; +} + +benchmark_group!(benches, a, b); +benchmark_main!(benches); diff --git a/crates/cargo-codspeed/tests/package_in_deps.rs b/crates/cargo-codspeed/tests/package_in_deps.rs new file mode 100644 index 0000000..1d14260 --- /dev/null +++ b/crates/cargo-codspeed/tests/package_in_deps.rs @@ -0,0 +1,17 @@ +use predicates::str::contains; + +mod helpers; +use helpers::*; + +const DIR: &str = "tests/package_in_deps.in"; + +#[test] +fn test_package_in_deps_build() { + let dir = setup(DIR, Project::PackageInDeps); + cargo_codspeed(&dir) + .arg("build") + .assert() + .success() + .stderr(contains("Finished built 1 benchmark suite(s)")); + teardown(dir); +}