use crate::context::Host;
pub struct Env<'a> {
host: &'a dyn Host,
}
impl<'a> Env<'a> {
#[must_use]
pub(crate) fn new(host: &'a dyn Host) -> Self {
Self { host }
}
#[must_use]
pub fn var(&self, name: &str) -> Option<String> {
self.host.env_var(name)
}
#[must_use]
pub fn var_or(&self, name: &str, default: impl Into<String>) -> String {
self.host.env_var(name).unwrap_or_else(|| default.into())
}
#[must_use]
pub fn vars(&self) -> Vec<(String, String)> {
self.host.env_vars()
}
}
#[cfg(test)]
mod tests {
use crate::Context;
use crate::testing::MockHost;
#[test]
fn env_reads_var_vars_and_default() {
let host = MockHost::new().with_env("TOKEN", "abc");
let ctx = Context::new(&host, "/w".into(), "s".into());
assert_eq!(ctx.env().var("TOKEN"), Some("abc".to_string()));
assert_eq!(ctx.env().var("MISSING"), None);
assert_eq!(ctx.env().var_or("MISSING", "def"), "def");
assert_eq!(ctx.env().vars(), vec![("TOKEN".to_string(), "abc".to_string())]);
}
}