1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
use std::{ cell::RefCell, io::Write, path::{Path, PathBuf}, rc::Rc, str, }; use crate::{output::Output, shell::Shell}; pub fn mkpath(path: &str) -> PathBuf { Path::new(path).canonicalize().unwrap() } pub struct TestShell; impl Shell for TestShell { fn name(&self) -> &'static str { "test" } fn init(&self, binary: &str, autoload_hook: bool) -> String { if autoload_hook { format!("{} {}", binary, "--autoload") } else { binary.to_string() } } fn set_var(&self, var: &str, value: &str) -> String { format!("export {}={}", var, value) } fn set_alias(&self, var: &str, value: &str) -> String { format!("alias {}={}", var, value) } fn set_function(&self, var: &str, value: &str) -> String { format!("function {}={}", var, value) } fn unset_var(&self, var: &str) -> String { format!("unset {}", var) } fn unset_alias(&self, var: &str) -> String { format!("unalias {}", var) } fn unset_function(&self, var: &str) -> String { format!("unset {}", var) } } #[derive(Clone)] pub struct MockWriter(Rc<RefCell<Vec<u8>>>); impl Default for MockWriter { fn default() -> Self { Self { 0: Rc::new(RefCell::new(Vec::new())), } } } impl MockWriter { pub fn value(&self) -> String { let data = &self.0.borrow(); String::from_utf8(data.to_vec()).unwrap() } } impl Write for MockWriter { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { self.0.borrow_mut().write(buf) } fn flush(&mut self) -> std::io::Result<()> { self.0.borrow_mut().flush() } } pub fn setup() -> (MockWriter, MockWriter, Output) { let out = MockWriter::default(); let err = MockWriter::default(); let output = Output::new(Box::new(out.clone()), Box::new(err.clone())); (out, err, output) }