use std::collections::HashMap;
use std::path::Path;
use crate::error::GomodError;
use crate::interp::GoBuildEnv;
use crate::build_spec::TargetTuple;
#[derive(Clone, Debug, Default)]
pub struct MockGoBuildEnv {
pub list_by_tuple: HashMap<String, String>,
pub files: HashMap<String, Vec<u8>>,
}
impl MockGoBuildEnv {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_list(mut self, tuple: &TargetTuple, json: impl Into<String>) -> Self {
self.list_by_tuple.insert(tuple.suffix(), json.into());
self
}
#[must_use]
pub fn with_file(mut self, path: impl Into<String>, bytes: impl Into<Vec<u8>>) -> Self {
self.files.insert(path.into(), bytes.into());
self
}
}
impl GoBuildEnv for MockGoBuildEnv {
fn go_list(&self, _root: &Path, tuple: &TargetTuple) -> Result<String, GomodError> {
self.list_by_tuple
.get(&tuple.suffix())
.cloned()
.ok_or_else(|| GomodError::GoList(format!("mock: no go list registered for {}", tuple.suffix())))
}
fn read_file(&self, path: &Path) -> Result<Vec<u8>, GomodError> {
let key = path.to_string_lossy().to_string();
self.files.get(&key).cloned().ok_or_else(|| GomodError::Io {
path: path.to_path_buf(),
source: std::io::Error::new(std::io::ErrorKind::NotFound, "mock: no file registered"),
})
}
}