Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cargo-codspeed): fix build when package is included in its deps #38

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading