#![allow(unsafe_code, dead_code)]
use std::path::PathBuf;
use tempfile::TempDir;
#[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();
std::fs::write(
path.join("defaults.yaml"),
r#"
log_level: debug
database:
host: localhost
port: 5432
"#,
)
.expect("failed to write defaults.yaml");
std::fs::write(
path.join("settings.yaml"),
r#"
app_name: test_app
database:
username: testuser
"#,
)
.expect("failed to write settings.yaml");
std::fs::write(
path.join("settings.development.yaml"),
r#"
debug: true
database:
password: devpassword
"#,
)
.expect("failed to write settings.development.yaml");
(dir, path)
}
pub struct EnvGuard {
vars: Vec<String>,
}
impl EnvGuard {
pub fn new(vars: &[(&str, &str)]) -> Self {
let var_names: Vec<String> = vars
.iter()
.map(|(k, v)| {
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 {
unsafe { std::env::remove_var(var) };
}
}
}