roboticus-cli 0.11.3

CLI commands and migration engine for the Roboticus agent runtime
Documentation
use std::ffi::OsString;

/// RAII guard that sets an env var and restores the old value on drop.
/// Tests using this guard MUST be annotated with `#[serial_test::serial]`
/// (or `#[serial_test::serial(env)]`) to prevent concurrent env mutation.
pub struct EnvGuard {
    key: &'static str,
    old: Option<OsString>,
}

impl EnvGuard {
    pub fn set(key: &'static str, value: &str) -> Self {
        let old = std::env::var_os(key);
        // SAFETY: callers must use #[serial_test::serial] to prevent races.
        unsafe { std::env::set_var(key, value) };
        Self { key, old }
    }
}

impl Drop for EnvGuard {
    fn drop(&mut self) {
        if let Some(v) = &self.old {
            unsafe { std::env::set_var(self.key, v) };
        } else {
            unsafe { std::env::remove_var(self.key) };
        }
    }
}