#[cfg(test)]
use std::collections::HashMap;
pub trait EnvProvider: Send + Sync {
fn var(&self, key: &str) -> Option<String>;
}
pub struct RealEnv;
impl EnvProvider for RealEnv {
fn var(&self, key: &str) -> Option<String> {
std::env::var(key).ok()
}
}
#[cfg(test)]
#[derive(Default, Clone)]
pub struct MockEnv {
vars: HashMap<String, String>,
}
#[cfg(test)]
impl MockEnv {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_var(mut self, key: &str, value: &str) -> Self {
let _ = self.vars.insert(key.to_string(), value.to_string());
self
}
#[must_use]
pub fn without_var(mut self, key: &str) -> Self {
let _ = self.vars.remove(key);
self
}
}
#[cfg(test)]
impl EnvProvider for MockEnv {
fn var(&self, key: &str) -> Option<String> {
self.vars.get(key).cloned()
}
}