hyperi-rustlib 2.6.0

Opinionated Rust framework for high-throughput data pipelines at PB scale. Auto-wiring config, logging, metrics, tracing, health, and graceful shutdown — built from many years of production infrastructure experience.
// Project:   hyperi-rustlib
// File:      tests/common/mod.rs
// Purpose:   Shared test fixtures and utilities
// Language:  Rust
//
// License:   FSL-1.1-ALv2
// Copyright: (c) 2026 HYPERI PTY LIMITED

#![allow(unsafe_code, dead_code)]

//! Shared test fixtures and utilities.

use std::path::PathBuf;
use tempfile::TempDir;

/// Create a temporary directory with config files for testing.
#[allow(dead_code)]
pub fn create_test_config_dir() -> (TempDir, PathBuf) {
    let dir = TempDir::new().expect("failed to create temp dir");
    let path = dir.path().to_path_buf();

    // Create defaults.yaml
    std::fs::write(
        path.join("defaults.yaml"),
        r#"
log_level: debug
database:
  host: localhost
  port: 5432
"#,
    )
    .expect("failed to write defaults.yaml");

    // Create settings.yaml
    std::fs::write(
        path.join("settings.yaml"),
        r#"
app_name: test_app
database:
  username: testuser
"#,
    )
    .expect("failed to write settings.yaml");

    // Create settings.development.yaml
    std::fs::write(
        path.join("settings.development.yaml"),
        r#"
debug: true
database:
  password: devpassword
"#,
    )
    .expect("failed to write settings.development.yaml");

    (dir, path)
}

/// Set environment variables for testing, returning a guard that clears them on drop.
pub struct EnvGuard {
    vars: Vec<String>,
}

impl EnvGuard {
    /// Create a new environment guard with the given variables.
    pub fn new(vars: &[(&str, &str)]) -> Self {
        let var_names: Vec<String> = vars
            .iter()
            .map(|(k, v)| {
                // SAFETY: single-threaded test setup, ENV_LOCK held by caller
                unsafe { std::env::set_var(k, v) };
                k.to_string()
            })
            .collect();

        Self { vars: var_names }
    }
}

impl Drop for EnvGuard {
    fn drop(&mut self) {
        for var in &self.vars {
            // SAFETY: single-threaded test teardown, ENV_LOCK held by caller
            unsafe { std::env::remove_var(var) };
        }
    }
}