Skip to content

Commit 1d44198

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

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

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();

0 commit comments

Comments
 (0)