use std::path::PathBuf;
use crate::connector::{ServiceConnector, ServiceInfo};
use super::helpers::detect_service;
pub struct ReviewConnector {
home_dir: Option<PathBuf>,
}
impl ReviewConnector {
pub fn new() -> Self {
Self { home_dir: None }
}
#[cfg(test)]
pub fn with_home(home_dir: PathBuf) -> Self {
Self {
home_dir: Some(home_dir),
}
}
fn addr_file_path(&self) -> PathBuf {
let home = self
.home_dir
.clone()
.or_else(dirs::home_dir)
.unwrap_or_else(|| PathBuf::from("/tmp"));
home.join(".trusty-review").join("http_addr")
}
}
impl Default for ReviewConnector {
fn default() -> Self {
Self::new()
}
}
impl ServiceConnector for ReviewConnector {
fn id(&self) -> &'static str {
"trusty-review"
}
fn display_name(&self) -> &'static str {
"Trusty Review"
}
fn detect(&self) -> ServiceInfo {
detect_service(
self.id(),
self.display_name(),
"trusty-review",
self.addr_file_path(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::connector::ServiceStatus;
use std::fs;
use tempfile::TempDir;
fn make_home_with_addr(rel_path: &str, content: &str) -> TempDir {
let tmp = TempDir::new().expect("tempdir");
let path = tmp.path().join(rel_path);
fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
fs::write(&path, content).expect("write addr file");
tmp
}
#[test]
fn test_review_connector_with_stale_addr_file() {
let tmp = make_home_with_addr(".trusty-review/http_addr", "127.0.0.1:14995");
let connector = ReviewConnector::with_home(tmp.path().to_path_buf());
let info = connector.detect();
let binary_present = which::which("trusty-review").is_ok();
if binary_present {
assert_eq!(
info.status,
ServiceStatus::Available,
"binary present, stale TCP → Available"
);
} else {
assert_eq!(info.status, ServiceStatus::Absent, "binary absent → Absent");
}
assert_eq!(info.id, "trusty-review");
assert_eq!(info.display_name, "Trusty Review");
}
#[test]
fn test_review_connector_no_addr_file() {
let tmp = TempDir::new().expect("tempdir");
let connector = ReviewConnector::with_home(tmp.path().to_path_buf());
let info = connector.detect();
let binary_present = which::which("trusty-review").is_ok();
if binary_present {
assert_eq!(
info.status,
ServiceStatus::Available,
"binary present, no addr file → Available"
);
} else {
assert_eq!(info.status, ServiceStatus::Absent, "binary absent → Absent");
}
assert!(info.url.is_none());
}
}