use std::process::Command;
#[derive(Debug, Clone, PartialEq, Eq)]
struct InstanceConfig {
key: &'static str,
value: String,
}
impl InstanceConfig {
fn new(key: &'static str, value: &str) -> Self {
Self {
key,
value: value.to_string(),
}
}
}
#[test]
fn independently_configured_instances_do_not_cross_talk() {
let first = InstanceConfig::new("TERRAPHIM_ISOLATION_A", "value-a");
let second = InstanceConfig::new("TERRAPHIM_ISOLATION_B", "value-b");
assert_eq!(first.value, "value-a");
assert_eq!(first.key, "TERRAPHIM_ISOLATION_A");
assert_eq!(second.value, "value-b");
assert_eq!(second.key, "TERRAPHIM_ISOLATION_B");
assert_ne!(first.value, second.value);
}
#[test]
fn child_environment_override_does_not_leak_into_the_parent() {
let var = "TERRAPHIM_CHILD_SCOPED_VAR";
assert!(
std::env::var_os(var).is_none(),
"test precondition: {var} must not be set in the parent"
);
let output = if cfg!(windows) {
Command::new("cmd")
.args(["/C", "echo ok"])
.env(var, "child-only-value")
.output()
.expect("failed to spawn child command")
} else {
Command::new("sh")
.args(["-c", "echo ok"])
.env(var, "child-only-value")
.output()
.expect("failed to spawn child command")
};
assert!(output.status.success());
assert_eq!(
std::env::var_os(var),
None,
"child environment override must not leak into the parent"
);
}
#[test]
fn child_environment_removal_is_scoped_to_the_child() {
let output = Command::new("sh")
.args(["-c", "exit 0"])
.env_remove("PATH")
.output()
.expect("failed to spawn child command");
assert!(output.status.success());
assert!(
std::env::var_os("PATH").is_some(),
"removing a variable for a child must not remove it from the parent"
);
}