2020-02-04 10:54:41 -08:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
pub(crate) struct TestEnvBuilder {
|
2020-03-05 21:44:20 -08:00
|
|
|
args: Vec<OsString>,
|
2020-02-04 10:54:41 -08:00
|
|
|
out_is_term: bool,
|
|
|
|
use_color: bool,
|
2020-02-14 00:12:49 -08:00
|
|
|
tempdir: Option<TempDir>,
|
2020-02-04 10:54:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestEnvBuilder {
|
|
|
|
pub(crate) fn new() -> TestEnvBuilder {
|
|
|
|
TestEnvBuilder {
|
|
|
|
args: Vec::new(),
|
|
|
|
out_is_term: false,
|
|
|
|
use_color: false,
|
2020-02-14 00:12:49 -08:00
|
|
|
tempdir: None,
|
2020-02-04 10:54:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn out_is_term(mut self) -> Self {
|
|
|
|
self.out_is_term = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:44:20 -08:00
|
|
|
pub(crate) fn arg(mut self, arg: impl Into<OsString>) -> Self {
|
2020-02-04 10:54:41 -08:00
|
|
|
self.args.push(arg.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn arg_slice(mut self, args: &[&str]) -> Self {
|
|
|
|
for arg in args.iter().cloned() {
|
2020-03-05 21:44:20 -08:00
|
|
|
self.args.push(arg.into());
|
2020-02-04 10:54:41 -08:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:12:49 -08:00
|
|
|
pub(crate) fn tempdir(mut self, tempdir: TempDir) -> Self {
|
|
|
|
self.tempdir = Some(tempdir);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-04 10:54:41 -08:00
|
|
|
pub(crate) fn build(self) -> TestEnv {
|
|
|
|
let err = Capture::new();
|
|
|
|
let out = Capture::new();
|
|
|
|
|
|
|
|
let env = Env::new(
|
2020-02-14 00:12:49 -08:00
|
|
|
self.tempdir.unwrap_or_else(|| tempfile::tempdir().unwrap()),
|
2020-02-04 10:54:41 -08:00
|
|
|
out.clone(),
|
|
|
|
if self.use_color && self.out_is_term {
|
|
|
|
Style::active()
|
|
|
|
} else {
|
|
|
|
Style::inactive()
|
|
|
|
},
|
|
|
|
self.out_is_term,
|
|
|
|
err.clone(),
|
|
|
|
Style::inactive(),
|
|
|
|
self.args,
|
|
|
|
);
|
|
|
|
|
|
|
|
TestEnv::new(env, err, out)
|
|
|
|
}
|
|
|
|
}
|