use std::cell::RefCell;
use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::Mutex;
use crate::environment::ExecutionError;
use crate::environment::prelude::*;
#[derive(Debug, Serialize, Deserialize)]
pub struct Mock {
#[serde(skip)]
queue: Mutex<RefCell<VecDeque<Result<String, ExecutionError>>>>,
}
impl PartialEq for Mock {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for Mock {}
#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for Mock {
fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
Some(std::cmp::Ordering::Equal)
}
}
impl Ord for Mock {
fn cmp(&self, _other: &Self) -> std::cmp::Ordering {
std::cmp::Ordering::Equal
}
}
impl Default for Mock {
fn default() -> Self {
Self {
queue: Mutex::new(RefCell::new(VecDeque::new())),
}
}
}
impl Mock {
pub fn new() -> Self {
Self::default()
}
pub fn push_raw(&self, entry: Result<String, ExecutionError>) {
self.queue.lock().unwrap().borrow_mut().push_back(entry);
}
pub fn pop_raw(&self) -> Result<String, ExecutionError> {
self.queue
.lock()
.unwrap()
.borrow_mut()
.pop_front()
.expect("requested more entries from mock env than were previously created")
}
pub fn to_env(self) -> Arc<Environment> {
Arc::new(Environment::Mock(self))
}
}
impl fmt::Display for Mock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MOCK (test-only)")
}
}
#[async_trait]
impl environment::IsEnvironment for Mock {
type Err = std::convert::Infallible;
async fn exists(&self) -> bool {
true
}
async fn execute(&self, _command: CommandLine) -> Result<Command, Self::Err> {
let mut cmd = Command::new("rustc");
cmd.arg("--version");
Ok(cmd)
}
}