use std::process::Command;
pub(crate) const WRAP_MARKER: &str = "LEAN_CTX_WRAPPED";
pub(crate) const ACTIVE_MARKER: &str = "LEAN_CTX_ACTIVE";
#[must_use]
pub(crate) fn should_pass_through() -> bool {
std::env::var(WRAP_MARKER).is_ok() || std::env::var("LEAN_CTX_DISABLED").is_ok()
}
pub(crate) fn mark_child(cmd: &mut Command) {
cmd.env(ACTIVE_MARKER, "1").env(WRAP_MARKER, "1");
}
#[cfg(test)]
mod tests {
use super::{ACTIVE_MARKER, WRAP_MARKER, mark_child, should_pass_through};
#[test]
fn wrap_marker_triggers_passthrough() {
let _lock = crate::core::data_dir::test_env_lock();
crate::test_env::remove_var("LEAN_CTX_DISABLED");
crate::test_env::remove_var(ACTIVE_MARKER);
crate::test_env::set_var(WRAP_MARKER, "1");
assert!(should_pass_through());
crate::test_env::remove_var(WRAP_MARKER);
}
#[test]
fn disabled_triggers_passthrough() {
let _lock = crate::core::data_dir::test_env_lock();
crate::test_env::remove_var(WRAP_MARKER);
crate::test_env::remove_var(ACTIVE_MARKER);
crate::test_env::set_var("LEAN_CTX_DISABLED", "1");
assert!(should_pass_through());
crate::test_env::remove_var("LEAN_CTX_DISABLED");
}
#[test]
fn inherited_active_does_not_trigger_passthrough() {
let _lock = crate::core::data_dir::test_env_lock();
crate::test_env::remove_var(WRAP_MARKER);
crate::test_env::remove_var("LEAN_CTX_DISABLED");
crate::test_env::set_var(ACTIVE_MARKER, "1");
assert!(
!should_pass_through(),
"inherited LEAN_CTX_ACTIVE must not disable compression (#533)"
);
crate::test_env::remove_var(ACTIVE_MARKER);
}
#[test]
fn mark_child_sets_both_markers() {
let mut cmd = std::process::Command::new("true");
mark_child(&mut cmd);
let envs: std::collections::HashMap<String, String> = cmd
.get_envs()
.filter_map(|(k, v)| Some((k.to_str()?.to_string(), v?.to_str()?.to_string())))
.collect();
assert_eq!(envs.get(WRAP_MARKER).map(String::as_str), Some("1"));
assert_eq!(envs.get(ACTIVE_MARKER).map(String::as_str), Some("1"));
}
}