use std::path::PathBuf;
use anyhow::{Context, Result};
pub fn binary_path(name: &str) -> Result<PathBuf> {
let key = format!("CARGO_BIN_EXE_{name}");
std::env::var_os(&key)
.map(PathBuf::from)
.with_context(|| format!("Cargo binary environment variable {key} is not set"))
}
#[cfg(test)]
mod tests {
#[test]
fn binary_path_resolves_hyphenated_cargo_target_name() {
let _guard = crate::testing::env::TestContext::builder()
.env(
"CARGO_BIN_EXE_godmode-test-helper",
"/tmp/godmode-test-helper",
)
.build();
assert_eq!(
super::binary_path("godmode-test-helper").unwrap(),
std::path::PathBuf::from("/tmp/godmode-test-helper")
);
}
#[test]
fn binary_path_reports_missing_cargo_target() {
assert!(super::binary_path("missing-test-helper").is_err());
}
}