use crate::ports::diagnostics::{
CheckCategory, CheckResult, CryptoDiagnosticProvider, DiagnosticError, GitDiagnosticProvider,
};
pub struct FakeGitDiagnosticProvider {
version_passes: bool,
version_string: String,
configs: Vec<(String, Option<String>)>,
}
impl FakeGitDiagnosticProvider {
pub fn new(version_passes: bool, configs: Vec<(&str, Option<&str>)>) -> Self {
Self {
version_passes,
version_string: "git version 2.40.0".to_string(),
configs: configs
.into_iter()
.map(|(k, v)| (k.to_string(), v.map(str::to_string)))
.collect(),
}
}
pub fn with_version_string(mut self, version: &str) -> Self {
self.version_string = version.to_string();
self
}
}
impl GitDiagnosticProvider for FakeGitDiagnosticProvider {
fn check_git_version(&self) -> Result<CheckResult, DiagnosticError> {
Ok(CheckResult {
name: "Git installed".to_string(),
passed: self.version_passes,
message: if self.version_passes {
Some(self.version_string.clone())
} else {
Some("git not found".to_string())
},
config_issues: vec![],
category: CheckCategory::Advisory,
})
}
fn get_git_config(&self, key: &str) -> Result<Option<String>, DiagnosticError> {
Ok(self
.configs
.iter()
.find(|(k, _)| k == key)
.and_then(|(_, v)| v.clone()))
}
}
pub struct FakeCryptoDiagnosticProvider {
ssh_keygen_passes: bool,
ssh_version_string: String,
}
impl FakeCryptoDiagnosticProvider {
pub fn new(ssh_keygen_passes: bool) -> Self {
Self {
ssh_keygen_passes,
ssh_version_string: "OpenSSH_9.6p1, LibreSSL 3.3.6".to_string(),
}
}
pub fn with_ssh_version(mut self, version: &str) -> Self {
self.ssh_version_string = version.to_string();
self
}
}
impl CryptoDiagnosticProvider for FakeCryptoDiagnosticProvider {
fn check_ssh_keygen_available(&self) -> Result<CheckResult, DiagnosticError> {
Ok(CheckResult {
name: "ssh-keygen installed".to_string(),
passed: self.ssh_keygen_passes,
message: None,
config_issues: vec![],
category: CheckCategory::Advisory,
})
}
fn check_ssh_version(&self) -> Result<String, DiagnosticError> {
Ok(self.ssh_version_string.clone())
}
}
#[cfg(test)]
mod tests {
use crate::testing::fakes::diagnostics::{
FakeCryptoDiagnosticProvider, FakeGitDiagnosticProvider,
};
crate::git_diagnostic_provider_contract_tests!(fake_git, {
(FakeGitDiagnosticProvider::new(true, vec![]), ())
},);
crate::crypto_diagnostic_provider_contract_tests!(fake_crypto, {
(FakeCryptoDiagnosticProvider::new(true), ())
},);
}