use tracing::{debug, info};
use crate::io::storage::Storage;
use crate::lineage::DomainLineage;
use crate::paths;
use crate::uri::Namespace;
use crate::Error;
use crate::Res;
pub async fn uninstall_package(
mut lineage: DomainLineage,
paths: &paths::DomainPaths,
storage: &impl Storage,
namespace: Namespace,
) -> Res<DomainLineage> {
info!("âŗ Uninstalling package {}", namespace);
debug!("đ Checking if package exists in lineage");
lineage
.packages
.remove(&namespace)
.ok_or(Error::PackageNotInstalled(namespace.to_owned()))?;
debug!("âī¸ Package removed from lineage");
debug!("âŗ Removing installed manifests");
let manifest_path = paths.installed_manifests(&namespace);
storage.remove_dir_all(&manifest_path).await?;
debug!("âī¸ Removed manifests at: {}", manifest_path.display());
debug!("âŗ Removing working directory");
let working_dir = paths.working_dir(&namespace);
storage.remove_dir_all(&working_dir).await?;
debug!("âī¸ Removed working directory: {}", working_dir.display());
debug!("âšī¸ Skipping object files cleanup - may be used by other packages");
info!("âī¸ Successfully uninstalled package {}", namespace);
Ok(lineage)
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use super::*;
use crate::lineage::PackageLineage;
use crate::mocks;
#[tokio::test]
async fn test_panic_if_no_installed_package() {
let lineage = DomainLineage::default();
let storage = mocks::storage::MockStorage::default();
let paths = paths::DomainPaths::default();
let result = uninstall_package(lineage, &paths, &storage, ("foo", "bar").into()).await;
assert_eq!(
result.unwrap_err().to_string(),
"The given package is not installed: foo/bar"
)
}
#[tokio::test]
async fn test_uninstall_package() -> Res {
let lineage = DomainLineage {
packages: BTreeMap::from([(("foo", "bar").into(), PackageLineage::default())]),
};
let paths = paths::DomainPaths::default();
let storage = mocks::storage::MockStorage::default();
let namespace = Namespace::from(("foo", "bar"));
storage
.create_dir_all(paths.installed_manifests(&namespace))
.await?;
storage
.create_dir_all(paths.working_dir(&namespace))
.await?;
let lineage = uninstall_package(lineage, &paths, &storage, namespace).await?;
assert!(lineage.packages.is_empty());
Ok(())
}
}