From 2c955198ff8b2d7a2698a7555eea9ee5aab1093c Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Fri, 3 Sep 2021 22:01:48 +0200 Subject: [PATCH] feat(package): add archiver tests --- espanso-package/src/archive/default.rs | 130 ++++++++++++++++++++++++- espanso-package/src/lib.rs | 13 +++ espanso-package/src/resolver.rs | 10 +- 3 files changed, 143 insertions(+), 10 deletions(-) diff --git a/espanso-package/src/archive/default.rs b/espanso-package/src/archive/default.rs index 882e30f..a00be55 100644 --- a/espanso-package/src/archive/default.rs +++ b/espanso-package/src/archive/default.rs @@ -105,5 +105,131 @@ impl Archiver for DefaultArchiver { } } -// TODO: test -// TODO: test what happens with "legacy" packages +#[cfg(test)] +mod tests { + use std::fs::{create_dir_all, write}; + use tempdir::TempDir; + + use crate::{manifest::Manifest, package::DefaultPackage, tests::run_with_temp_dir}; + + use super::*; + + fn create_fake_package(dest_dir: &Path) -> Box { + let package_dir = dest_dir.join("package1"); + create_dir_all(&package_dir).unwrap(); + + write( + package_dir.join("_manifest.yml"), + r#" +name: "package1" +title: "Dummy package" +description: A dummy package for testing +version: 0.1.0 +author: Federico Terzi + "#, + ) + .unwrap(); + + write( + package_dir.join("package.yml"), + r#" +matches: + - trigger: ":hello" + replace: "github"name: "package1" + "#, + ) + .unwrap(); + + write( + package_dir.join("README.md"), + r#" + A very dummy package + "#, + ) + .unwrap(); + + let package = DefaultPackage::new( + Manifest::parse(&package_dir.join("_manifest.yml")).unwrap(), + TempDir::new("fake-package").unwrap(), + package_dir, + ); + + Box::new(package) + } + + fn run_with_two_temp_dirs(action: impl FnOnce(&Path, &Path)) { + run_with_temp_dir(|base| { + let dir1 = base.join("dir1"); + let dir2 = base.join("dir2"); + create_dir_all(&dir1).unwrap(); + create_dir_all(&dir2).unwrap(); + action(&dir1, &dir2); + }); + } + + #[test] + fn test_package_saved_correctly() { + run_with_two_temp_dirs(|package_dir, dest_dir| { + let package = create_fake_package(package_dir); + + let archiver = DefaultArchiver::new(dest_dir); + let result = archiver.save( + &*package, + &PackageSpecifier { + name: "package1".to_string(), + git_repo_url: Some("https://github.com/espanso/dummy-package".to_string()), + git_branch: Some("main".to_string()), + ..Default::default() + }, + &SaveOptions::default(), + ); + + assert!(result.is_ok()); + + let package_out_dir = dest_dir.join("package1"); + assert!(package_out_dir.is_dir()); + assert!(package_out_dir.join("_manifest.yml").is_file()); + assert!(package_out_dir.join("README.md").is_file()); + assert!(package_out_dir.join("package.yml").is_file()); + assert!(package_out_dir.join("_pkgsource.yml").is_file()); + }); + } + + #[test] + fn test_package_already_present() { + run_with_two_temp_dirs(|package_dir, dest_dir| { + let package = create_fake_package(package_dir); + + create_dir_all(dest_dir.join("package1")).unwrap(); + + let archiver = DefaultArchiver::new(dest_dir); + let result = archiver.save( + &*package, + &PackageSpecifier { + name: "package1".to_string(), + git_repo_url: Some("https://github.com/espanso/dummy-package".to_string()), + git_branch: Some("main".to_string()), + ..Default::default() + }, + &SaveOptions::default(), + ); + + assert!(result.is_err()); + + let result = archiver.save( + &*package, + &PackageSpecifier { + name: "package1".to_string(), + git_repo_url: Some("https://github.com/espanso/dummy-package".to_string()), + git_branch: Some("main".to_string()), + ..Default::default() + }, + &SaveOptions { + overwrite_existing: true, + }, + ); + + assert!(result.is_ok()); + }); + } +} diff --git a/espanso-package/src/lib.rs b/espanso-package/src/lib.rs index 75b67dd..4f084a5 100644 --- a/espanso-package/src/lib.rs +++ b/espanso-package/src/lib.rs @@ -81,4 +81,17 @@ pub fn get_provider(package: &PackageSpecifier) -> Result Result> { Ok(Box::new(archive::default::DefaultArchiver::new(package_dir))) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use tempdir::TempDir; + + pub(crate) fn run_with_temp_dir(action: impl FnOnce(&Path)) { + let tmp_dir = TempDir::new("espanso-package").unwrap(); + let tmp_path = tmp_dir.path(); + + action(&tmp_path); + } } \ No newline at end of file diff --git a/espanso-package/src/resolver.rs b/espanso-package/src/resolver.rs index a7d5c4d..430e972 100644 --- a/espanso-package/src/resolver.rs +++ b/espanso-package/src/resolver.rs @@ -104,15 +104,9 @@ fn find_all_manifests(base_dir: &Path) -> Result> { mod tests { use std::fs::create_dir_all; + use crate::tests::run_with_temp_dir; + use super::*; - use tempdir::TempDir; - - fn run_with_temp_dir(action: impl FnOnce(&Path)) { - let tmp_dir = TempDir::new("espanso-package").unwrap(); - let tmp_path = tmp_dir.path(); - - action(&tmp_path); - } #[test] fn test_read_manifest_base_dir() {