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-03-07 19:23:20 -08:00
|
|
|
current_dir: Option<PathBuf>,
|
2020-03-18 02:48:57 -07:00
|
|
|
err_style: bool,
|
|
|
|
input: Option<Box<dyn Read>>,
|
2020-02-04 10:54:41 -08:00
|
|
|
out_is_term: bool,
|
2020-02-14 00:12:49 -08:00
|
|
|
tempdir: Option<TempDir>,
|
2020-03-07 19:23:20 -08:00
|
|
|
use_color: bool,
|
2020-02-04 10:54:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestEnvBuilder {
|
|
|
|
pub(crate) fn new() -> TestEnvBuilder {
|
|
|
|
TestEnvBuilder {
|
|
|
|
args: Vec::new(),
|
2020-03-07 19:23:20 -08:00
|
|
|
current_dir: None,
|
2020-03-18 02:48:57 -07:00
|
|
|
err_style: false,
|
|
|
|
input: None,
|
2020-02-04 10:54:41 -08:00
|
|
|
out_is_term: false,
|
2020-02-14 00:12:49 -08:00
|
|
|
tempdir: None,
|
2020-03-07 19:23:20 -08:00
|
|
|
use_color: false,
|
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-15 22:41:47 -07:00
|
|
|
pub(crate) fn err_style(mut self, err_style: bool) -> Self {
|
|
|
|
self.err_style = err_style;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-18 02:48:57 -07:00
|
|
|
pub(crate) fn input(mut self, input: impl AsRef<[u8]>) -> Self {
|
|
|
|
self.input = Some(Box::new(io::Cursor::new(input.as_ref().to_owned())));
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:23:20 -08:00
|
|
|
pub(crate) fn current_dir(mut self, path: PathBuf) -> Self {
|
|
|
|
self.current_dir = Some(path);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-04 10:54:41 -08:00
|
|
|
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();
|
|
|
|
|
2020-03-07 19:23:20 -08:00
|
|
|
let tempdir = self.tempdir.unwrap_or_else(|| tempfile::tempdir().unwrap());
|
|
|
|
|
|
|
|
let current_dir = if let Some(current_dir) = self.current_dir {
|
|
|
|
tempdir.path().join(current_dir)
|
|
|
|
} else {
|
|
|
|
tempdir.path().to_owned()
|
|
|
|
};
|
|
|
|
|
2020-03-15 03:22:33 -07:00
|
|
|
let out_stream = OutputStream::new(
|
|
|
|
Box::new(out.clone()),
|
|
|
|
self.use_color && self.out_is_term,
|
2020-02-04 10:54:41 -08:00
|
|
|
self.out_is_term,
|
|
|
|
);
|
|
|
|
|
2020-03-15 22:41:47 -07:00
|
|
|
let err_stream = OutputStream::new(Box::new(err.clone()), self.err_style, false);
|
2020-03-15 03:22:33 -07:00
|
|
|
|
2020-03-18 02:48:57 -07:00
|
|
|
let env = Env::new(
|
|
|
|
current_dir,
|
|
|
|
self.args,
|
|
|
|
self.input.unwrap_or_else(|| Box::new(io::empty())),
|
|
|
|
out_stream,
|
|
|
|
err_stream,
|
|
|
|
);
|
2020-03-15 03:22:33 -07:00
|
|
|
|
2020-03-07 19:23:20 -08:00
|
|
|
TestEnv::new(tempdir, env, err, out)
|
2020-02-04 10:54:41 -08:00
|
|
|
}
|
|
|
|
}
|