sqlserver-mcp 0.7.1

SQL Server 2025/2022/2019/2017 - master/msdb/model combined catalog MCP server, generated by mcpify.
Documentation
//! Test-only helpers for mutating process environment variables safely.
//!
//! `std::env::set_var`/`remove_var` are `unsafe` in edition 2024 because the
//! environment is process-global state that every thread shares, and Rust's
//! default test runner executes `#[test]` functions concurrently within one
//! process. Every test that reads or writes an env var this crate consults
//! (`HOME`, `SQLSERVER_*`) goes through [`EnvGuard`], so all such mutations
//! are serialized by one lock and undone when the test ends -- this is the
//! only place in the test suite that calls the `unsafe` env setters.

use std::ffi::{OsStr, OsString};

/// Serializes every test that touches process env vars.
///
/// `tokio::sync::Mutex` rather than `std::sync::Mutex`: an async test that
/// awaits something reading env vars must be able to hold the guard across
/// that `.await` (holding a `std` guard there is `clippy::await_holding_lock`).
/// [`EnvGuard::set`] takes it via `.blocking_lock()`, so it must be called
/// from a sync test (not from inside a Tokio runtime).
static ENV_TEST_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// Holds [`ENV_TEST_LOCK`] and restores every variable it changed on drop.
pub(crate) struct EnvGuard {
    saved: Vec<(String, Option<OsString>)>,
    _lock: tokio::sync::MutexGuard<'static, ()>,
}

impl EnvGuard {
    /// Only takes the lock, for a test that reads env-derived state and
    /// must not observe another test's override mid-flight.
    pub(crate) fn lock() -> Self {
        Self::set(&[])
    }

    /// Locks the env, then applies `vars` (`Some` sets, `None` removes).
    pub(crate) fn set(vars: &[(&str, Option<&OsStr>)]) -> Self {
        let lock = ENV_TEST_LOCK.blocking_lock();
        let saved = vars
            .iter()
            .map(|(name, _)| (name.to_string(), std::env::var_os(name)))
            .collect();
        for (name, value) in vars {
            // SAFETY: every test that reads or writes process env vars holds
            // ENV_TEST_LOCK (acquired above and kept in the returned guard),
            // so no other test thread touches the environment concurrently.
            unsafe {
                match value {
                    Some(value) => std::env::set_var(name, value),
                    None => std::env::remove_var(name),
                }
            }
        }
        Self { saved, _lock: lock }
    }
}

impl Drop for EnvGuard {
    fn drop(&mut self) {
        for (name, previous) in self.saved.drain(..).rev() {
            // SAFETY: `self._lock` still holds ENV_TEST_LOCK here (fields are
            // dropped only after this body runs), so the same exclusion as in
            // `apply` applies.
            unsafe {
                match previous {
                    Some(previous) => std::env::set_var(&name, previous),
                    None => std::env::remove_var(&name),
                }
            }
        }
    }
}