use std::collections::HashMap;
use rattler_conda_types::Platform;
#[derive(Debug, Clone)]
pub struct RuntimeEnv {
env: HashMap<String, String>,
platform: Platform,
}
impl RuntimeEnv {
pub fn current() -> Self {
Self {
env: std::env::vars().collect(),
platform: Platform::current(),
}
}
pub fn for_test(platform: Platform) -> Self {
Self {
env: HashMap::new(),
platform,
}
}
pub fn platform(&self) -> Platform {
self.platform
}
pub fn var(&self, name: &str) -> Option<&str> {
self.env.get(name).map(String::as_str)
}
pub fn path(&self) -> &str {
self.var("PATH").unwrap_or_default()
}
pub(crate) fn exe_suffix(&self) -> &'static str {
if self.platform.is_windows() {
".exe"
} else {
""
}
}
pub fn vars(&self) -> impl Iterator<Item = (&str, &str)> {
self.env.iter().map(|(k, v)| (k.as_str(), v.as_str()))
}
#[must_use]
pub fn with_var(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.env.insert(name.into(), value.into());
self
}
#[must_use]
pub fn with_platform(mut self, platform: Platform) -> Self {
self.platform = platform;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exe_suffix_follows_the_platform() {
assert_eq!(RuntimeEnv::for_test(Platform::Win64).exe_suffix(), ".exe");
assert_eq!(RuntimeEnv::for_test(Platform::Linux64).exe_suffix(), "");
assert_eq!(RuntimeEnv::for_test(Platform::OsxArm64).exe_suffix(), "");
}
}