winbrew_engines\archive/
remove.rs

1use anyhow::Result;
2use std::fs;
3use std::io::ErrorKind;
4
5use crate::models::install::installed::InstalledPackage;
6
7pub(crate) fn remove(package: &InstalledPackage) -> Result<()> {
8    match fs::remove_dir_all(&package.install_dir) {
9        Ok(()) => Ok(()),
10        Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
11        Err(err) => Err(err.into()),
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use super::remove;
18    use crate::models::install::engine::EngineKind;
19    use crate::models::install::installed::{InstalledPackage, PackageStatus};
20    use crate::models::install::installer::InstallerType;
21    use std::fs;
22    use tempfile::tempdir;
23
24    fn package(name: &str, install_dir: &std::path::Path) -> InstalledPackage {
25        InstalledPackage {
26            name: name.to_string(),
27            version: "1.0.0".to_string(),
28            kind: InstallerType::Zip,
29            deployment_kind: InstallerType::Zip.deployment_kind(),
30            engine_kind: EngineKind::Zip,
31            engine_metadata: None,
32            install_dir: install_dir.to_string_lossy().into_owned(),
33            dependencies: Vec::new(),
34            status: PackageStatus::Ok,
35            installed_at: "2026-04-05T00:00:00Z".to_string(),
36        }
37    }
38
39    #[test]
40    fn remove_deletes_existing_directory() {
41        let temp_root = tempdir().expect("temp root");
42        let install_dir = temp_root.path().join("packages").join("Contoso.Zip");
43
44        fs::create_dir_all(&install_dir).expect("create install dir");
45        fs::create_dir_all(install_dir.join("bin")).expect("create bin dir");
46        fs::write(install_dir.join("bin").join("tool.exe"), b"binary").expect("write file");
47
48        remove(&package("Contoso.Zip", &install_dir)).expect("remove directory");
49
50        assert!(!install_dir.exists());
51    }
52
53    #[test]
54    fn remove_allows_missing_directory() {
55        let temp_root = tempdir().expect("temp root");
56        let install_dir = temp_root.path().join("packages").join("Contoso.Missing");
57
58        remove(&package("Contoso.Missing", &install_dir))
59            .expect("missing directory should be ignored");
60    }
61}