Skip to content

Commit

Permalink
fix(cargo-codspeed): fix build when package is included in its deps
Browse files Browse the repository at this point in the history
Fix #37
  • Loading branch information
adriencaccia committed Apr 10, 2024
1 parent 4cf6291 commit 47d35a1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
23 changes: 15 additions & 8 deletions crates/cargo-codspeed/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ fn get_compile_options(
features: &Option<Vec<String>>,
package: &Package,
benches: Vec<&str>,
is_root_package: bool,
) -> Result<CompileOptions> {
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
Expand Down Expand Up @@ -51,14 +56,15 @@ pub fn build_benches(
package_name: Option<String>,
features: Option<Vec<String>>,
) -> 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()
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions crates/cargo-codspeed/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Project {
Simple,
Features,
Workspace,
PackageInDeps,
}

pub fn setup(dir: &str, project: Project) -> String {
Expand Down Expand Up @@ -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()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/cargo-codspeed/tests/package_in_deps.in/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cargo.lock
target/
17 changes: 17 additions & 0 deletions crates/cargo-codspeed/tests/package_in_deps.in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions crates/cargo-codspeed/tests/package_in_deps.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 47d35a1

Please sign in to comment.