Skip to content

Commit 924aac0

Browse files
committed
fix(cargo-codspeed): throw an error when a bench process fails
1 parent 600f1a1 commit 924aac0

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed

crates/bencher_compat/src/compat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ macro_rules! benchmark_group {
6464
pub fn $group_name(bencher: &mut $crate::Bencher) {
6565
bencher.push_group(stringify!($group_name));
6666
$(
67-
bencher.set_current_file(codspeed::abs_file!());
67+
bencher.set_current_file($crate::abs_file!());
6868
bencher.set_current_bench_path(stringify!($function));
6969
$function(bencher);
7070
)+

crates/bencher_compat/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mod compat_bencher {
66
#[cfg(codspeed)]
77
#[path = "."]
88
mod compat_bencher {
9+
pub use codspeed::abs_file;
10+
911
mod compat;
1012
pub use compat::*;
1113
}

crates/cargo-codspeed/src/run.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ pub fn run_benches(
7676
std::process::Command::new(bench)
7777
.env("CODSPEED_CARGO_WORKSPACE_ROOT", workspace_root.as_ref())
7878
.status()
79-
.map_err(|_| anyhow!("failed to execute the benchmark process"))?;
79+
.map_err(|_| anyhow!("failed to execute the benchmark process"))
80+
.and_then(|status| {
81+
if status.success() {
82+
Ok(())
83+
} else {
84+
Err(anyhow!(
85+
"failed to execute the benchmark process, exit code: {}",
86+
status.code().unwrap_or(1)
87+
))
88+
}
89+
})?;
8090
ws.config().shell().status_with_color(
8191
"Done",
8292
format!("running {}", bench_name),

crates/codspeed/src/macros.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#[macro_export]
22
macro_rules! abs_file {
33
() => {
4-
std::path::PathBuf::from(std::env::var("CODSPEED_CARGO_WORKSPACE_ROOT").unwrap())
5-
.join(file!())
6-
.to_string_lossy()
4+
std::path::PathBuf::from(
5+
std::env::var("CODSPEED_CARGO_WORKSPACE_ROOT")
6+
.expect("Could not find CODSPEED_CARGO_WORKSPACE_ROOT env variable, make sure you are using the latest version of cargo-codspeed")
7+
)
8+
.join(file!())
9+
.to_string_lossy()
710
};
811
}
912

crates/codspeed/src/utils.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::io;
22
use std::path::{Path, PathBuf};
3-
use std::process::Command;
43

54
fn get_parent_git_repo_path(abs_path: &Path) -> io::Result<PathBuf> {
65
if abs_path.join(".git").exists() {
@@ -28,32 +27,6 @@ where
2827
}
2928
}
3029

31-
/// Returns the path to the root of the cargo workspace.
32-
/// This is needed since file! returns the path relatively to the workspace root
33-
/// while CARGO_MANIFEST_DIR returns the path to the sub package
34-
pub fn get_cargo_workspace_root() -> PathBuf {
35-
let output = Command::new("cargo")
36-
.args([
37-
"metadata",
38-
"--format-version",
39-
"1",
40-
"--no-deps",
41-
"--quiet",
42-
"--offline",
43-
])
44-
.current_dir(env!("CARGO_MANIFEST_DIR"))
45-
.output()
46-
.expect("Failed to execute `cargo metadata`");
47-
48-
let metadata: serde_json::Value =
49-
serde_json::from_slice(&output.stdout).expect("Failed to parse `cargo metadata` output");
50-
51-
let workspace_root = metadata["workspace_root"]
52-
.as_str()
53-
.expect("`workspace_root` field is missing or not a string");
54-
PathBuf::from(workspace_root)
55-
}
56-
5730
/// Fixes spaces around `::` created by stringify!($function).
5831
pub fn get_formated_function_path(function_path: impl Into<String>) -> String {
5932
let function_path = function_path.into();

crates/criterion_compat/src/compat/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro_rules! criterion_group {
44
pub fn $name(criterion: &mut $crate::Criterion) {
55
let mut criterion = &mut criterion.with_patched_measurement($config);
66
$(
7-
criterion.set_current_file(codspeed::abs_file!());
7+
criterion.set_current_file($crate::abs_file!());
88
criterion.set_macro_group(format!("{}::{}", stringify!($name), stringify!($target)));
99
$target(criterion);
1010
)+

crates/criterion_compat/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ mod compat_criterion {
88
#[cfg(codspeed)]
99
#[path = "."]
1010
mod compat_criterion {
11+
pub use codspeed::abs_file;
12+
1113
mod compat;
1214
pub use compat::*;
1315

0 commit comments

Comments
 (0)