use std::path::Path;
use std::path::PathBuf;
use crate::Env;
use crate::EnvVars;
use crate::env::EnvArgs;
use crate::env::EnvImpl;
#[cfg(any(feature = "fs", feature = "process", feature = "request"))]
use crate::env::SharedEnv;
use crate::test_platform::ROOT;
pub struct TestEnv(EnvImpl);
impl TestEnv {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let env = EnvImpl::new();
env.shared.replace_current_dir(PathBuf::from(ROOT));
Self(env)
}
pub fn with_args<T: Into<String>>(mut self, args: Vec<T>) -> Self {
self.0.args = args.into_iter().map(Into::into).collect();
self
}
pub fn with_current_dir<P: Into<PathBuf>>(self, current_dir: P) -> Self {
self.0.shared.replace_current_dir(current_dir.into());
self
}
pub fn with_current_exe<P: Into<PathBuf>>(mut self, current_exe: P) -> Self {
self.0.current_exe = current_exe.into();
self
}
pub fn with_home_dir<P: Into<PathBuf>>(mut self, home_dir: P) -> Self {
self.0.home_dir = home_dir.into();
self
}
pub fn with_tmp_dir<P: Into<PathBuf>>(mut self, tmp_dir: P) -> Self {
self.0.tmp_dir = tmp_dir.into();
self
}
pub fn with_vars<K: Into<String>, V: Into<String>>(self, vars: Vec<(K, V)>) -> Self {
self.0.shared.replace_vars(
vars.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
);
self
}
#[cfg(any(feature = "fs", feature = "process", feature = "request"))]
pub(crate) fn shared(&self) -> &SharedEnv {
&self.0.shared
}
}
impl Env for TestEnv {
fn args(&self) -> EnvArgs<'_> {
self.0.args()
}
fn current_dir(&self) -> PathBuf {
self.0.current_dir()
}
fn current_exe(&self) -> &Path {
self.0.current_exe()
}
fn home_dir(&self) -> &Path {
self.0.home_dir()
}
fn remove_var<T: AsRef<str>>(&self, key: T) -> Option<String> {
self.0.remove_var(key)
}
fn set_current_dir<P: Into<PathBuf>>(&self, path: P) -> PathBuf {
self.0.set_current_dir(path)
}
fn set_var<T: Into<String>, U: Into<String>>(&self, key: T, value: U) -> Option<String> {
self.0.set_var(key, value)
}
fn tmp_dir(&self) -> &Path {
self.0.tmp_dir()
}
fn var<T: AsRef<str>>(&self, key: T) -> Option<String> {
self.0.var(key)
}
fn vars(&self) -> EnvVars {
self.0.vars()
}
}