use std::path::Path;
pub const FORCE_ENV: &str = "TRUSTY_TEST_HARNESS";
pub const ALLOW_PRODUCTION_ENV: &str = "TRUSTY_ALLOW_PRODUCTION_STATE";
pub fn running_under_test_harness() -> bool {
detect(
std::env::var(ALLOW_PRODUCTION_ENV).ok().as_deref(),
std::env::var(FORCE_ENV).ok().as_deref(),
cfg!(test),
std::env::current_exe().ok().as_deref(),
)
}
fn detect(
allow_production: Option<&str>,
force: Option<&str>,
compiled_as_test: bool,
current_exe: Option<&Path>,
) -> bool {
if is_truthy(allow_production) {
return false;
}
if is_truthy(force) || compiled_as_test {
return true;
}
current_exe.is_some_and(is_cargo_test_binary)
}
fn is_truthy(value: Option<&str>) -> bool {
value.is_some_and(|v| {
matches!(
v.trim().to_ascii_lowercase().as_str(),
"1" | "true" | "yes" | "on"
)
})
}
fn is_cargo_test_binary(exe: &Path) -> bool {
if exe.parent().and_then(Path::file_name) != Some(std::ffi::OsStr::new("deps")) {
return false;
}
let Some(stem) = exe.file_stem().and_then(|s| s.to_str()) else {
return false;
};
stem.rsplit_once('-').is_some_and(|(name, hash)| {
!name.is_empty() && hash.len() >= 8 && hash.chars().all(|c| c.is_ascii_hexdigit())
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn test_exe() -> PathBuf {
PathBuf::from("/repo/target/debug/deps/integration_tests-f54141ba75b48011")
}
#[test]
fn running_under_test_harness_is_true_in_this_test_binary() {
let _guard = crate::data_dir::ENV_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
assert!(
running_under_test_harness(),
"a cargo test process must be detected as a test harness"
);
}
#[test]
fn detect_allow_production_outranks_everything() {
assert!(
!detect(Some("1"), Some("1"), true, Some(&test_exe())),
"TRUSTY_ALLOW_PRODUCTION_STATE must win over force, cfg(test) and the path check"
);
}
#[test]
fn detect_force_env_wins_over_path_check() {
let installed = PathBuf::from("/Users/me/.cargo/bin/trusty-search");
assert!(detect(None, Some("true"), false, Some(&installed)));
}
#[test]
fn detect_falls_back_to_exe_path() {
assert!(detect(None, None, false, Some(&test_exe())));
}
#[test]
fn detect_is_false_for_a_real_invocation() {
for exe in [
"/Users/me/.cargo/bin/trusty-search",
"/opt/homebrew/bin/trusty-search",
"/repo/target/debug/trusty-search",
"/repo/target/release/trusty-search",
] {
assert!(
!detect(None, None, false, Some(Path::new(exe))),
"{exe} must not be mistaken for a test binary"
);
}
}
#[test]
fn detect_is_false_when_exe_is_unresolvable() {
assert!(!detect(None, None, false, None));
}
#[test]
fn is_cargo_test_binary_accepts_every_target_kind() {
for exe in [
"/repo/target/debug/deps/trusty_search-88c47ed536c1e550",
"/repo/target/debug/deps/integration_tests-f54141ba75b48011",
"/repo/target/release/deps/registry_isolation-0802fde7a9957161",
] {
assert!(
is_cargo_test_binary(Path::new(exe)),
"{exe} is a cargo test binary"
);
}
}
#[test]
fn is_cargo_test_binary_requires_the_metadata_hash() {
assert!(!is_cargo_test_binary(Path::new("/repo/deps/trusty-search")));
assert!(!is_cargo_test_binary(Path::new(
"/repo/target/debug/deps/trusty-search-notahash"
)));
assert!(!is_cargo_test_binary(Path::new(
"/repo/target/debug/deps/thing-abc"
)));
}
#[test]
fn is_truthy_accepts_the_documented_spellings_only() {
for yes in ["1", "true", "TRUE", "Yes", " on "] {
assert!(is_truthy(Some(yes)), "{yes:?} must be truthy");
}
for no in ["0", "false", "", "no", "off", "maybe"] {
assert!(!is_truthy(Some(no)), "{no:?} must not be truthy");
}
assert!(!is_truthy(None));
}
}